Python Forum
Serial communication with a board - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Serial communication with a board (/thread-6759.html)



Serial communication with a board - Bokka - Dec-06-2017



Hello!

I have to write a simple program that send a hex string to an electronic instrument through the COM port of Pc and read the response.

I had wrote that code:

from binascii import unhexlify
import serial


stringa = "F5010001010008FC"
port = "COM1"
ser = serial.Serial(port,9600,timeout=0.5)

ser.write(unhexlify(stringa))

out = ser.read()
for byte in out:
   print(byte)
The program convert the string in hex value and send it to the board. The signal is correctly received from the board, because there is a small led that indicate it.

But the problem is I can't see the board response, I think is because I don't use an async communication (and I don't know how to do it simply).

The string that i receive FROM the board must be  "F5 00 01 01 01 00 08 FC"

In that screenshot there is the communication parameters:

[Image: Immagine.png]

Have any idea how can I do that? Thanks a lot in advance!


RE: Serial communication with a board - hshivaraj - Dec-06-2017

I think your problem is here

out = ser.read()
for byte in out:
   print(byte)
By default ser.read() function reads just 1 bytes of the port. You need to specify the total number of bytes you want to read before reading. read function take an argument, argument to specify number of bytes to read or use readlines().

out = ser.read(20)   # make sure you have timeout set or it make block forever
for byte in out:
   print(byte)
See documentation for more details
http://pythonhosted.org/pyserial/shortintro.html#readline


RE: Serial communication with a board - Bokka - Dec-07-2017

Thanks a lot for the reply!

Unofrtunately still don't work, I had tried to insert the parity paramenter on my project but without success.....

from binascii import unhexlify
import serial


stringa = "F5010001010008FC"

port = "COM1"
ser = serial.Serial(port,9600,timeout=0.5)
ser.PARITY_EVEN = 'E'


stringa_byte = unhexlify(stringa)

ser.write(stringa_byte)


out = ser.read(20)
for byte in out:
   print(byte)
Now I don't know what to try Huh