Weird Output Value: b'[value]\r\n' - Python Serial Read - python

I'm reading a value through Python from the serial port of a sensor of Arduino.
My code(Python):
arduino = serial.Serial(2, 9600, timeout=1)
print("Message from arduino: ")
while True:
msg = arduino.readline()
print(msg)
I don't know why the output result is something like b'[sensor-value]\r\n'.
So, I get something like b'758\r\n' b'534\r\n' b'845\r\n' etc (regarding to sensor change value).
How I convert this?

You need to decode it.
print(msg.decode('utf-8'))
Please check Lexical Analysis on Python 3 documentation to see what string prefixes means

Encountered a similar problem with a Raspberry Pi Pico where I needed to both decode and get rid of the extra characters. That can all be achieved with a one-liner. This relies on the pySerial package.
msg = ser.readline().decode('utf-8').rstrip()
For the above example, serial.Serial has been named arduino instead of ser, so the solution there would simply be:
msg = arduino.readline().decode('utf-8').rstrip()
Found the hint in this blog post.

Related

Is there some special encoding required when sending ASCII commands over Serial (USB-RS232) port in Python3.8?

Firstly, I have also researched an seen that this question is answered alot of places, but the answers don't work for me.
I am trying to send ASCII command over serial port to a PCBA and then attempting to receive the response.
HW setup:
Computer
USB-RS232 Cable
PCBA
SW:
Python 3.8
I have sent commands over RS232 terminals (realterm) and they work perfectly (LED turns on) but can't implement the same with python. The command in question is "led_r_on".
And yes, I get "COM5 is open" prompt, meaning COM5 is alive.
Thanks for helps!
code as follows:
import serial
import time
from time import sleep
from serial import Serial
port = 'COM5'
baud = 115200
ser = serial.Serial(port, baud, timeout=2)
if ser.isOpen():
print(ser.name + ' is open...')
sleep(1)
ser.write(b'led_r_on')
print(ser.readline(1000))
Since you didn't give any information on the pcba you are using. I will just assume, that you are missing some kind of end delimiter in your bytesequence. Now there are many ways of ending a transmission over serial. The most frequent ones I ran across are these two:b'led_r_on\r\n' or just b'led_r_on\n'. Try changing your bytes in the ser.write() function to this. If that does not work you might need to use this: b'led_r_on\x04' this will send the EOT (End of Transmission) ASCII character. But I don't think this will be needed.

Parse Data received from Pyserial read into string

I am reading data from a serial port using python (pyserial) I am able to read the data but when I try using it, it seems like this $*%\xff\x06$*%\xff\x02 referred to few resources on stack overflow and found that it needs to be decoded, I tried decoding it to ascii using processed = (binascii.b2a_qp(raw))using the binascii library but received the following output $*%=FF=00$*%=FF=08 I have also tried decoding the same to UTF-8 but still no success. Any suggestions about how to process the input received from the read() function. I also tried using the readline() but the program then goes blank or infinite execution seems there is no EOL marker in the serial output.
The Demo Code snip is as follows :
with serial.Serial('/dev/cu.usbserial-Device',9600) as ser:
ser.flushInput()
ser.flushOutput()
ser.write('S')
inputVal=[]
while(len(inputVal)<10000):
val = ser.read(10)
inputVal.append(binascii.b2a_qp(val))
Any suggestions ? Thanks in advance.
You can try to encode whilst writing to the serial port.
ser.write(str.encode('S\r')
While, to read, I would use something like
a = ser.readline()
b = v.rstrip()
c = b.decode('utf-8')

How can I read my data correctly?

I have my Arduino connected to an OLIMEX-SHIELD-EKG-EMG, and there is this Arduino example for EKG capture and interface to Electric Guru for OLIMEXINO-328/Arduino boards.
I ran the Arduino software and then I made sure it's working using the Guru and it's all fine.
I tried to read the data from the serial port using Python and saving it to a text file, so I can use it for other stuff (Windows commands in my case). When I open the file the data is all garbage.
So what did I do wrong and how can I fix it?
Thank you all in advance.
This is my code:
import serial
from time import sleep
f = open("data2.txt", "w")
port = "\\.\COM4"
ser = serial.Serial(port, 38400, timeout=0)
while True:
data = ser.read(9999)
if len(data) > 0:
print ('Got:',data)
sleep(0.5)
print ('not blocked')
f.write( str(data) + "\n\n" )
f.close()
ser.close()
And this the data I get:
b'\x05\r\x1c\x1e\x10K\x12\x8a\x01\x0e\x14&\x1b\x1bW\x12\x80\x00\x7f\x11 \rZC:\x82\x12_.\x1a?\x10^\x12\x8a\x1a__\\x01:Q\x12\x8a\x15\x01\x1a\x1f\x1c6L\x1a\x82\x00t\x1e\x0c\x18\x19s:\x8a\x05]-\x01\x0e[G\x12\x8a\x15_\x0b9\x17>R\x12\x8a\x08__\x7f\x08:S\x1a\x82\x01\r\x18\x1d4\x10j:\x8a\n_0\x06\x10\x12Y\x10\x8a\nRH\x1c\x0bw#\x12\x80\x08\x7f\x0f7\x1f\x1bT:\x8a+\x7fU%\x18P:\x8a\x00\x0b}\x006\x11K\x12\x8a\x00Z\x19&\x16>\x1a\x82\x01\x02D\x1d\nYb:\x8a*$\x15\x12\x1cX:\x8a\x1b\x7f[\x06\x1bP\x12\x8a+\x0c\x1f?\x1b-F\x1a\x82\x05Z\x13\x0f7\x14}:\x8a\nUb\x19\n7^\x10\x8a\x15_\n\x14\x132_\x12\x80\x1d\x7f_x\x05\x15N:\x8a\x05<`\x025\x11I:\x8a\x0bZ1\x0f4\x18S\x12\x8a\x0bQK8\x0bvB\x1a\x82\x11\x7f\x0e\x1a\x18\x14q\x1a\x82\x07\xc1\xe1\x05\x14\x13q:\x8a+Sb\x02\x1b\x0cM\x12\x8a\x01\x1f\x1b%\x15\x15]\x1a\x82\x01B\x01.(\x1d\x7f\x1a\x82\xc2\xc1\x05,9\x1at\x10\x8a,__X\x024N\x1a\x80(xD!\x15\x10I\x1a\x82\x08|.
That looks to me like binary data. So i guess either each 16 or each 32 bit are a single numeric. You would have to cast the data you have and then format it into a text file.
It looks like ser.read() returns bytes not string. You need to decode that to if you want string. Use data.decode(your_encoding). You need to know the encoding.
For more see docs.
Decoding in UTF-16 could be promising...
data=b'\x05\r\x1c\x1e\x10K\x12\x8a\x01\x0e\x14&\x1b\x1bW\x12\x80\x00\x7f\x11 \rZC:\x82\x12_.\x1a?\x10^\x12\x8a\x1a__\\x01:Q\x12\x8a\x15\x01\x1a\x1f\x1c6L\x1a\x82\x00t\x1e\x0c\x18\x19s:\x8a\x05]-\x01\x0e[G\x12\x8a\x15_\x0b9\x17>R\x12\x8a\x08__\x7f\x08:S\x1a\x82\x01\r\x18\x1d4\x10j:\x8a\n_0\x06\x10\x12Y\x10\x8a\nRH\x1c\x0bw#\x12\x80\x08\x7f\x0f7\x1f\x1bT:\x8a+\x7fU%\x18P:\x8a\x00\x0b}\x006\x11K\x12\x8a\x00Z\x19&\x16>\x1a\x82\x01\x02D\x1d\nYb:\x8a*$\x15\x12\x1cX:\x8a\x1b\x7f[\x06\x1bP\x12\x8a+\x0c\x1f?\x1b-F\x1a\x82\x05Z\x13\x0f7\x14}:\x8a\nUb\x19\n7^\x10\x8a\x15_\n\x14\x132_\x12\x80\x1d\x7f_x\x05\x15N:\x8a\x05<`\x025\x11I:\x8a\x0bZ1\x0f4\x18S\x12\x8a\x0bQK8\x0bvB\x1a\x82\x11\x7f\x0e\x1a\x18\x14q\x1a\x82\x07\xc1\xe1\x05\x14\x13q:\x8a+Sb\x02\x1b\x0cM\x12\x8a\x01\x1f\x1b%\x15\x15]\x1a\x82\x01B\x01.(\x1d\x7f\x1a\x82\xc2\xc1\x05,9\x1at\x10\x8a,__X\x024N\x1a\x80(xD!\x15\x10I\x1a\x82\x08|.'
data.decode('UTF-16')
u'\u0d05\u1e1c\u4b10\u8a12\u0e01\u2614\u1b1b\u1257\x80\u117f\u0d20\u435a\u823a\u5f12\u1a2e\u103f\u125e\u1a8a\u5f5f\u785c\u3130\u513a\u8a12\u0115\u1f1a\u361c\u1a4c\x82\u1e74\u180c\u7319\u8a3a\u5d05\u012d\u5b0e\u1247\u158a\u0b5f\u1739\u523e\u8a12\u5f08\u7f5f\u3a08\u1a53\u0182\u180d\u341d\u6a10\u8a3a\u5f0a\u0630\u1210\u1059\u0a8a\u4852\u0b1c\u4077\u8012\u7f08\u370f\u1b1f\u3a54\u2b8a\u557f\u1825\u3a50\x8a\u7d0b\u3600\u4b11\u8a12\u5a00\u2619\u3e16\u821a\u0201\u1d44\u590a\u3a62\u2a8a\u1524\u1c12\u3a58\u1b8a\u5b7f\u1b06\u1250\u2b8a\u1f0c\u1b3f\u462d\u821a\u5a05\u0f13\u1437\u3a7d\u0a8a\u6255\u0a19\u5e37\u8a10\u5f15\u140a\u3213\u125f\u1d80\u5f7f\u0578\u4e15\u8a3a\u3c05\u0260\u1135\u3a49\u0b8a\u315a\u340f\u5318\u8a12\u510b\u384b\u760b\u1a42\u1182\u0e7f\u181a\u7114\u821a\uc107\u05e1\u1314\u3a71\u2b8a\u6253\u1b02\u4d0c\u8a12\u1f01\u251b\u1515\u1a5d\u0182\u0142\u282e\u7f1d\u821a\uc1c2\u2c05\u1a39\u1074\u2c8a\u5f5f\u0258\u4e34\u801a\u7828\u2144\u1015\u1a49\u0882\u2e7c'

'Drunk' input from readline, OK from other programs (reading smart meters P1 port)

I'm new to Python and want to read my smart meters P1 port using a Raspberry Pi and Python. Problem: the input looks like some component is drunk.
I'm sure it's pretty simple to fix, but after several hours of searching and trying, had to seek help.
When reading the P1 port with CU etc. everything is fine so the hardware etc. is OK. Using a serial to USB converter from dx.com (this one)
Command and (part of) the output: cu -l /dev/ttyUSB0 -s 9600 --parity=none
0-0:96.1.1(205A414246303031363631323463949271)
1-0:1.8.1(03118.000*kWh)
However, when trying to read it from Python, the input becomes gibberish (but at least sort of consistant):
0-0:96.±.±(²05A´±´²´630303±39363±3²3´639·3±3²©
±-0:±.¸.±(03±±¸.000ªë×è©
How to fix this? The code I'm using is:
import serial
ser = serial.Serial()
ser.baudrate = 9600
ser.bytesize=serial.SEVENBITS
ser.parity=serial.PARITY_EVEN
ser.stopbits=serial.STOPBITS_ONE
ser.xonxoff=0
ser.rtscts=0
ser.timeout=20
ser.port="/dev/ttyUSB0"
ser.close()
ser.open()
print ("Waiting for P1 output on " + ser.portstr)
counter=0
#read 20 lines
while counter < 20:
print ser.readline()
counter=counter+1
try:
ser.close()
print ("Closed serial port.")
except:
sys.exit ("Couldn't close serial port.")
Have already tried messing with baudrate etc. but that doesn't make any difference.
I'm not very familiar with the serial module, but I noticed that your cu command assumes there is no parity bit (--parity=none), but your python script assumes there is an even parity bit (ser.parity=serial.PARITY_EVEN). I would try
ser.parity=serial.PARITY_NONE
And if there's no parity bit, you'll also probably want
ser.bytesize=serial.EIGHTBITS
UPDATE: found a workaround by replacing the naughty characters.
This may work for others with the same problem, but I dont know if the bad characters are exactly the same. So the replacement part may need some work to make it work for others.
It's not exactly a solution as the incoming telegram is still messed up, but the following code will work around that. My telegram is completely clean now.
Relevant part of the code I'm using now:
#Define 2 variables
P1_numbers = {'±':'1', '²':'2', '´':'4', '·':'7', '¸':'8'}
P1_rest = {'¯':'/', 'ª':'*', '©':')', 'Æ':'F', 'ë':'k', '×':'W', 'è':'h', 'í':'m'}
# Define function to read the telegram. Calls a function to clean it.
def P1_read(stack):
counter = 0
while counter < TelegramLength:
stack.append(P1_clean(ser.readline()))
counter=counter+1
return stack
# Define function to clean up P1 output
def P1_clean(line):
for i, j in P1_numbers.iteritems():
line = line.replace(i, j)
for i, j in P1_rest.iteritems():
line = line.replace(i, j)
return line
I quess you have a smart meter with P1 protocol: DSMR 3.0?
Then these are the correct serial port settings, which you already had:
serialport = serial.Serial( # Configure Serial communication port
baudrate = 9600,
timeout = 11,
bytesize = serial.SEVENBITS,
parity = serial.PARITY_EVEN,
stopbits = serial.STOPBITS_ONE )
Probably some encoding or interpretation of the data is going wrong at your side. Here is an other method the read the smart meter:
To make the readout of the p1 protocol as easy as possible I'd suggest to use TextIOWrapper, this way you can read the serial port with the readline method. The "!" always ends the P1 telegram, so that can be used to detect the end of the message. when a full telegram has been received, the telegram can be processed. Example:
import io
p1port = io.TextIOWrapper(io.BufferedReader(serialport, buffer_size=1), newline='\n', encoding='ascii')
P1Message = []
while True:
try:
rawline = self.p1port.readline()
except UnicodeDecodeError:
print "Encode error on readline"
if '!' in rawline:
# Process your P1Message here
P1Message = [] # Clear message, wait for new one
else:
P1Message.append(rawline)
The OP is looooong gone, but the problem is of sufficiently general interest, so here's a fresh answer. User #Brionius is right: A look at the bit patterns involved shows that it's definitely a parity problem. Here's how to inspect the bit pattern of the characters "1" and "±":
>>> "{0:b}".format(ord("1"))
'110001'
>>> "{0:b}".format(ord("±"))
'10110001'
Get it? The characters are getting corrupted by having their high (8th) bit turned on. Or you can see this by setting the high bit of ascii "1":
>>> chr(ord("1") | 0b10000000)
'±'
Now, "1", "2" and "4" have three bits set (odd parity), and are corrupted. "0", "3", "5", etc. have even parity (2 or 4 bits set), and are preserved. So the communication channel is using even parity, which is not decoded properly at the receiving end.
I had the same problem, also in the context of the P1 Smart Meter port, and it took me quite a while to find it.
'cu' was displaying the data correctly, but Python wasn't (nor were some other programs). Apparently the parity bit is somehow not handled correctly. The following solves this problem:
p1_raw = ser.readline()
print 'raw:', p1_raw
decoded = ''.join(chr(ord(ch) & 0x7f) for ch in p1_raw)
print 'decoded:', decoded
I still find it strange that this is happening, because this was actually happening when I was trying to read the output of a Smart Meter for the second time. I already had a script successfully monitoring another Smart Meter at a different house for a couple of years and I never ran into this problem.
Perhaps there's a small difference in the USB-Serial adapters that causes this?!?

Pyserial testing

I am new to Pyserial and Hardware area. I am trying to run the sample applications given in http://pyserial.sourceforge.net/shortintro.html#opening-serial-ports
import serial
ser = serial.Serial(0) # open first serial port
print ser.portstr # check which port was really used
ser.write("hello") # write a string
ser.close()
I have written this program in a python file and running it. Now if I want to test this application to check if I am sending correct string (eg: Hyperterminal or something) how can I do this. Can anyone guide me?
Use virtual serial port for your test.
For Windows I use com0com and socat for Linux.
Then, use Putty for visualization of your send.
Another quick way to test a physical serial port is to take a wire/screwdriver, crocodile clamp or anything that you have in hand and bridge the RX and TX (receive and transmit) together. At that point, everything that you send out will be looped back at you. YOu can receive it afterward by using this code snippet here:
import serial
ser = serial.Serial(0, timeout = 1) # open first serial port
print ser.portstr # check which port was really used
ser.write("hello") # write a string
msg = ser.read("100") #read the content of the input buffer until you get 100 byte or a timeout event
print(msg) #print the content you might need to decode it print(decode(msg))
ser.close()
The key aspect again for this code to work properly is to bridge RX and TX together. A lot of tutorial will show you how to do this.

Categories

Resources