I was trying to read data from smart power meter using ttl rs485 converter and ft232 serial converter connected to PC. I've tried to read the data from serial port using the Pyserial python module and used the code below but the only output I've got was ("b, "b, ",b), I was wondering if there is any mistake in my code!
Thanks.
The code I used
import serial
ser = serial.Serial(
port= 'COM5',
baudrate= 9600,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout= 2
)
print("connected to: " + ser.portstr, "Please wait" )
print('COM5 is now Open!')
ser.flushInput()
ser.flushOutput()
while True:
data = ser.readline()
print(data)
The output I got
connected to: COM5 Please wait
COM5 is now Open!
b''
b''
b''
b''
b''
Related
So I'm receiving data from a serial port using pySerial. I've a very simple code that reads the first byte, check if it's the start byte ( 0x02 in my case) and then read until it finds the end byte ( 0x03 in my case).
Config the serial communciation
port = 'COM3'
baud = 38400
ser = serial.Serial(port, baud, timeout=0)
if ser.isOpen():
ser.close()
ser.open()
ser.reset_input_buffer()
ser.reset_output_buffer()
The main loop is inside a while True staement as below.
while True:
data = ""
data_raw = ser.read(1)
if data_raw == b'\x02':
data_raw = ser.read_until(b'\x03')
print(str(data_raw))
ser.reset_input_buffer()
ser.reset_output_buffer()
time.sleep(.5)
The issue is that, for some reason, the read_until() actually reads only the first bye while the data I'm receiving from the serial port are actually b'\x02\xff\x9c\x81E1\x03\'
After reading correctly the \x02 the read_until() statement just read only the next \xff and I can't understand why
It seems a bug never fixed by the pySerial module itself https://github.com/pyserial/pyserial/issues/181
Using timeout=None in serial.Serial() resolved the issue
If I use Putty to connect with serial to COM1, 1200 baud I get a black screen but when I type Ctlr-E on the keyboard I get the value that I'm looking to record. If I use serial.tools.miniterm and I type Ctrl-E on the keyboard I get the proper value. When I use the following code it seems as though I connect and there is nothing waiting in the buffer:
import serial
ser = serial.Serial(
port='com1',\
baudrate=1200,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=0)
print(ser.flush())
print(ser.flushInput())
print(ser.reset_input_buffer())
print(ser.flushOutput())
print(ser.reset_output_buffer())
print(ser.in_waiting)
print(ser.out_waiting)
print(ser.readline())
print(ser.read())
print(ser.read(1))
ser.close()
I get back on screen:
None
None
None
None
0
0
b''
b''
b''
I was expecting one of these results to be the value I get in Putty and Miniterm.
What am I doing wrong? After connecting with Python can I send that same key sequence that is sent by Putty when I type Ctrl-E?
I figured out what was needed / missing partially due to a post from here on Oct 2nd, 2013 by Chaosphere2112. I need to both send an encapsulated \x05 (thanks barny) but also wait and then read the value in the buffer.
here is the code that worked in the end:
import serial
import time
ser = serial.Serial(
port='com1',\
baudrate=1200,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=0)
ser.write('\x05'.encode('utf-8'))
time.sleep(1)
read_val = ser.read(size=64)
if read_val != '':
print(read_val)
ser.close()
Thank you.
I am using a kunbus RevPi connect + to control a pump with a USB serial converter. To control it, I need to send ASCII command so I try to use the Pyserial library.
import serial
import time
ser = serial.Serial('/dev/ttyUSB2', baudrate = 9600, bytesize=8, parity='N', stopbits=1, timeout=1)
print (ser.portstr)
ser.write(b"1H[CR]")
I haven't error message but the pump doesn' start.
Can you help me please?
I'm using raspberry pi 3 and this code to send a request to a device and receive the response from.
#!/usr/bin/python3.7
import socket # Import socket module
import thread
import time
import serial
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
input = '5A03010d0a75'
print "Sending request... "+input
ser.write(input.decode("hex"))
print "Request sent."
output=""
while True:
output += ser.read(1)
#time.sleep(0.1)
print "Reading... "+output.encode('hex')
It handles the response but there are missing bytes, it should receive a 56 bytes length string instead of 53.
This is the output:
a5030119010000010001000a20120118180130090100020505030117501701051421000301040120010516039833630004060104c200007d
There are 3 missing bytes
The serial configuration is what the manufacturer says in the documentation.
This device works well with my other application made in Delphi.
EXTRA
This is a comparison from my delphi app and this py script:
Delphi app
A5030119010000010001000A20120118180130090100020505030117501701051421000301040120010516039833630004060104C200007D
Python script
a503011901000001000100 1201181801300901000205050301175017010514210003010401 010516039833630004060104c200007d
The solution was to set the max byte to the serial.read() method
This should be related to the device work behavior
#!/usr/bin/python3.7
#sudo python /home/testing.py
import serial
import time
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=5
)
input = '5A03010d0a75'
print "Sending request... "+input
ser.write(input.decode("hex"))
print "Request sent."
output=""
time.sleep(1)
while ser.inWaiting() > 0:
output += ser.read(10) #setting it to 10 will fix this problem
print "Reading... "+output.encode('hex')
I am trying to open a serial port via Python, write the incoming data stream into a file and close the file when the data stream stops.
My program is unable to detect that the port is no longer receiving any data. So the file remains open. What do I do?
import serial
ser = serial.Serial(port='COM8', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0)
print("Connected to Audio(COM8) port")
try:
f = open('C:\Users\user\Desktop\final.raw', 'w')
while 1:
ser_bytes = ser.readline()
if ser_bytes:
f.write(ser_bytes)
else:
break
finally:
f.close()
print("output file closed")
ser.close()
Specify timeout while using ser.readline()