FOR TIPS, gUIDES & TUTORIALS

subscribe to our Youtube

GO TO YOUTUBE

14455 questions

17168 answers

28195 comments

0 members

We are migrating to our new platform at https://community.teltonika.lt. Moving forward, you can continue discussions on this new platform. This current platform will be temporarily maintained for reference purposes.
0 votes
488 views 0 comments
by anonymous

Good day, 

I am trying to read HEX data streams via RS232 on my TRB245 router. I am specifically trying to use pySerial to achieve this. I have installed all of the necessary packages and have a python sample script running on the router that is able to partially read the incoming data. I am however experiencing issues with the consistency of the received strings, when I try to read more bytes in the string (for example > 200 bytes) it never registers a read. The ser.inWaiting() also function also seems to always remain at 0, even if it does receive and print out a string. Currently I am only a reading incoming bytes. I have tried to write a command and then read the response but with no success. Any possible suggestions on what might be causing the reading issues? I have attached my code below (the functions that are commented out are all of the variations that I have tried already.

import time

import serial

print('Ready...')

#command =b'\xaa\x41\xa1\x06\x11\xa3'

ser = serial.Serial(

  port ='/dev/rs232,

  baudrate = 9600,

  parity = serial.STOPBITS_ONE,

  bytesize = serial.EIGHTBITS,

  timeout = None #Some suggest increasing this timeout

)

ser.isOpen()

ser.setRTS(False)

ser.setDTR(False)

ser.flushInput()

ser.flushOutput()

#ser.write(command)

#print('sent')

#time.sleep(0.1)

#response=ser.read(6)

#print(response)

while 1:

#BytesToRead = ser.inWaiting()  #Read amount of Bytes

#y = ser.read_until(size=None) #Read until byte string empty

x = ser.read().hex() #Read one byte

#z = ser.read(100).hex() #Read up to 100 bytes

#time.sleep(1) #Optional delay

print(x)                                             #Print x, y or z       

if BytesToRead > 0: #Test if ser.inWaiting sees multiple bytes

             print('Hello')

1 Answer

0 votes
by anonymous

Hi,

Have you tried to read data via RS232 directly, without Python? Is it working properly and the issue is only in the code?

Sorry, but I will not be able to provide much assistance with the code. 

However, when it comes to RS232 serial communication in general, it is important to ensure that the settings, such as baud rate, data bits, parity, and others, are identical at both ends of the serial connection. Seems that the settings are defined within the code, but I do not see any flow control settings configured (seems that RTS/CTS and Xon/Xoff are off). You might want to take a look at that too. You can find some information about RS232 HERE.

Kind Regards,

Andzej