USB Serial data sending gibberish - 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.

Related

How to send int to arduino with Python

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.

Python barcode scanner serial trigger

I'm messing around with a Honeywell 4600 barcode scanner in python, configured as a serial device. All is well and I can read barcodes with it, but I would like to test the serial trigger option instead of pressing the trigger all the time.
The manual is very brief on this feature and only states "SYN T CR" has to be written to the device to activate the serial trigger
ser.write('SYN T CR')
does not seem to do much.
Can someone point me in the right direction? Thank you!
This happens because you coded the abstract expression written in the document as raw output data.
The document represents 3 bytes of data transmission.
'SYN' and 'CR' are the following hexadecimal numbers.
'SYN' = \x16
'CR' = \x0d or escape sequence \r
'T' is an ordinary ASCII character.
Whitespace is used to separate the data in the document, not data to send.
You should write like this. Please try it.
ser.write(b'\x16T\r')
Alternatively, perhaps even you may need to prefix it.
Send data to Honeywell Xenon 1902 barcode reader via virtual com port
In that case, please try the following transmission.
ser.write(b'\x16M\r\x16T\r')

Moving Sign Communication Protocol v1.2 with Raspberry Pi

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

Encoding for ZMQ

I am currently doing project which requires communication from a PC to the device, so far I've decided on socket comms. and have written some code. I am also using ZMQ for ipc on the device itself.
My script works by sending data as text across. I was trying to encode my data to utf-8 so that it can be easily read on the device and displays in a frame and performs the tasks as needed. However, I can't seem to get the encoding working right, I've tried searching for examples or tutorials online but can't seem to find any.
I've tried using socket.send (msg.encode("UTF-8")) to encode my data, and message = socket.recv() to recv & print the data on the server. This works but the server would print out the exact text data which is not what I wanted. I'm unsure whether this is the correct way, and hope that someone could point me in the correct direction for encoding & printing the encoded data without decoding back to text.
You are receiving the text as encoded UTF8 data. It is all working correctly.
However, if you are printing the data on the receiving end directly to a terminal that happens to be configured to display UTF-8, you won't see any difference.
Print the representation instead:
print repr(message)
to see the string literal representation including any non-printable, non-ASCII bytes displayed as escape strings.

Decoding Simulink's UDP Packets in Python

I have a Simulink model sending data via UDP to another program (Blender) where I can receive the packets, but I have not been able to figure out how to correctly decode them.
In the Simulink model I just have it sending a value that is based upon a sine wave, nothing fancy, just a single value like 1.452 or something. In Blender I have it spitting out the data it receives from the packet, and I'm receiving stuff like:
b'<\xa6ya\x05\x93\xe3?'
I have no idea how to decode this. It seems to have some hex values, but beyond that I'm lost. I'm not even sure what all this data contains. Is it just the value from Simulink, or does it contain information regarding things like the sender and receiver IP addresses, ports, etc...?
UPDATE:
I updated the Simulink model to transmit a constant value via UDP for debugging/investigation. The value is 0.5234, and the data that my Python script is spitting out is:
b'\xab>W[\xb1\xbf\xe0?'
Which, when converted into hexadecimal reads (using hexlify):
b'ab3e575bb1bfe03f'
How would I extract/decode 0.5234 out of that?
Thanks for any help!
You can use struct to decode your binary data, in this case it seems to be a double value:
>>> import struct
>>> struct.unpack('d', b'\xab>W[\xb1\xbf\xe0?')
(0.5234,)

Categories