This is my first post here so forgive me if some questions might be out of scope, but basically I'm trying to write my own program to push messages from a Raspberry Pi to a LED scrolling sign via serial communication (USB on Pi -> RS232 adapter -> LED sign).
Im not very familiar with serial communication in general, but am attempting to use the PySerial library on the Pi (http://pyserial.sourceforge.net/pyserial.html) in the format of Moving Sign Protocol V1.2 (http://www.brgprecision.com/pdffiles/Protocol12.pdf).
Here is my python code thus far, error free, yet the plugged in sign doesnt receive any data.
import serial
#default port is /dev/tty/USB0
#portname, baudrate, timeout
port = serial.Serial('/dev/ttyUSB0', 9600)
port.open() port.write('0x00\0x01\"FF"\"03"\0x02\'A'\'A'\'A'\'2'\'2'\'7F'\'0100'\'1200'\'000'\'1'\"OMFG"\0x03\"0564"\0x04')
port.close()
Basically, I dont know how to parse the message Im trying to write into proper serial data packages. Do I send the protocol and text message all in one go like above? Or must I parse each of the fields and send them separately like:
port.write('01') # start of head
port.write('46') # pc address
port.write('46') # number 1 display
ect...
I should mention that I also have sniffed the USB serial communication on my PC and can confirm this serial information is correct, I just have no real idea how to use it on a RPi. Any help will be much appreciated!
you want to use bytes
0x00 == 0 == "\x00"
which is not the same as "0x00" which is really "\x30\x78\x30\x30" which is 4 characters not the single byte that you are hoping to achieve
so to send the part A
ser.write("\x00"*5) # 5 null bytes
to send the SOH part
ser.write("\x01")
Im not sure what the address stuff is... but that hopefully gives you an idea
Related
First of all, I'm a noob. I'm doing my best here. I really do.
I have a pressure sensor called KITA KP70. This is it's manual:
https://drive.google.com/file/d/1ED1kr3cW1mmgM_-hSxhoo-Cbr2zI3ZSo/view
I'm trying to read anything from it using python without any luck. All I get back is an empty response after the code times out.
This is the code I'm using:
import serial
port = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=2)
while True:
port.write("30H30H31H32H".encode())
rcv = port.read(10)
print (rcv)
Now the thing is connected as this:
The sensor itself is connected to UART to RS485 Converter. This is the device:
https://www.amazon.com/HiLetgo-Reciprocal-Hardware-Automatic-Converter/dp/B082Y19KV9
The converter is connected to c232hm-ddhsl-0. This is the product:
https://ftdichip.com/products/c232hm-ddhsl-0-2/
And the c232hm-ddhsl-0 is connected using USB to my PC.
I can see the device when checked using the terminal:
ahmad#Ahmad-PC:~/Desktop$ sudo dmesg | grep tty
[ 0.073387] printk: console [tty0] enabled
[ 13.131371] usb 3-1: FTDI USB Serial Device converter now attached to ttyUSB0
When I execute the command above, the write LED blinks, but nothing else happens.
I've checked the cables and everything seems to be correctly connected and to the right color.
Can anyone help me read anything from this device? The list of codes this device accept are listed in the manual.
Thank you in advanced!
You have been fooled by poor documentation. When they say "30H30H31H32H", they are using an antique method of indicating hex digits. What they mean is the four-byte sequence 0x30 0x30 0x31 0x32, which happens to be the string "0012".
For what it's worth, that number style comes from Microsoft's MASM assembler.
I was trying to control a Servo with Python but I have only Arduino to control a Servo. I was found how to control servo with writing numbers but I can't send int to arduino.I was trying to send this
first = 680/ a
last = 180/ first
I was trying to send 'last' but I can't do it. Please help me and sorry for my bad English.
Here is code that connects to an Arduino and sends it data:
import serial
port = serial.Serial("/path/to/your/arduino/port", 9600)
port.write(bytes("180", "utf-8")) # This is just an example; you can use any string
# Close the port when you're done talking to the Arduino
port.close()
Notice that you need to convert the string to bytes before sending it, and that you need to specify the encoding. Just use whatever encoding your program uses. Here, it's UTF-8.
So, basically I have a file on my pc which contains some statements (string). I want to send those strings character by character to arduino uno. fopen() doesn't work in arduino (if that's what i read is correct).
I don't know if serial port will work as taking input from a file on my pc.
Is there a way to send character by character data to arduino?If yess, then please guide me.
If data can be sent through python and/or command prompt(terminal) please tell me how to do it.
Thanks in advance.
There are countless ways to communicate with an Arduino, given you have the respective shields. Wifi, Ethernet, Bluetooth, ...
You can use a USB to Serial converter, connected to the Arduinos Rx/Tx pins. Most if not all Arduinos come with an integrated USB to serial converter onboard. Connect the Arduino to your PC with a USB cable. Make sure you have the drivers for that USB-serial converter installed and you're ready to go.
All you need to know is how to send data from your PC through the virtual COM port of that USB-serial device and how to read that data within your Arduino sketch.
Then you open a file on your computer and send it to the Arduino byte by byte.
There are countless tutorials available. Just search the web for "Arduino serial" and "Python serial" in case you want to do the PC part in Python.
i am having an issue i cannot seem to resolve. i am using python on the raspberry pi to read from a usb connection on the pi (that is being converted from serial).
i am able to connect to the usb port and start receiving data with the code
ser = serial.Serial("myUsbPortID", 9600)
bytes = ser.inWaiting()
print ser.read(bytes)
i know that the baudrate is 9600 (hardware manufacturers docs) but for some reason when i try to read the stream of data i get a lot of gibberish in the form of different languages and characters. After i kill the program my screen still replaces my characters with the gibberish data as i type.
i'm sure this isn't the stream of data the hardware is sending. something somewhere is converting things but i have no idea what it may be.
when i boot up the device and it is initializing then i get readable information. but when the device start operating i only get this weird characters
is there a way to convert these characters to the data that it is actually coming in as?
example pic:
output screen
so i was able to solve the issue of the gibberish with this line on python code (for anyone else having this issue).
data = ":".join("{:02x}".format(ord(c)) for c in bytes)
where bytes is the raw data, i'm making it ':' delimited.
seems the connection and baud rate was ok. luckily the data was able to convert to hex ok.
How can I read the data I am sending to my XBee connected to my Windows machine?
I want to see if the data is being sent correctly, because my code is compiling correctly in IDLE, but if I try to read the serial console in XCTU it says the port is currently being occupied. Any ideas on how to read the data I'm sending?
import serial
i = 'A'
ser = serial.Serial('com3',9600,timeout =1)
ser.write(i)
ser.close()
You have both XBees connected, while communicating to one in the python code, you have the other in the serial console in XCTU. Writing to the port will display the message.
Are you trying to open COM3 in XCTU? You won't be able to do that since you have it open in Python. Is that XBee module paired with one on another serial port where you'd be able to see the output?
You might want to add a delay between the ser.write() and ser.close() calls to ensure you're giving it time to send the data. It's possible that the first call just queues the data to send.
Have you considered using the Python-Xbee library also? It makes decoding packets easier:
https://github.com/nioinnovation/python-xbee
This library also has support for Zigbee.
Jim