Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Serial communication with a board
#1


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!
Quote
#2
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/shortin...l#readline
Quote
#3
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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Serial communication with raspberry pi 3B and Xbee kj7 0 550 Mar-25-2019, 03:39 AM
Last Post: kj7
  Serial Communication Error AlphyOuseph 1 695 Feb-07-2019, 07:16 AM
Last Post: DeaD_EyE
  pi3 serial communication tony1812 0 735 Sep-23-2018, 10:29 PM
Last Post: tony1812
  [Help] A function that generates an n x n sized board? vanicci 5 1,257 Aug-14-2018, 02:26 PM
Last Post: vanicci
  serial communication write(comand) and read(result) ricardons 1 1,026 Mar-01-2018, 04:26 PM
Last Post: mpd
  Serial port communication FatimaSameer 1 1,275 Oct-03-2017, 09:47 AM
Last Post: Larz60+
  snakes and ladders board creation help teddyben123 1 2,606 Aug-25-2017, 01:31 PM
Last Post: ichabod801
  Communicating C++ and Python in Jetson TK1 board. hlfan1234567 1 1,318 Jul-09-2017, 04:22 PM
Last Post: hlfan1234567
  SPI Communication Raudert 0 1,468 Apr-24-2017, 01:30 PM
Last Post: Raudert

Forum Jump:


Users browsing this thread: 1 Guest(s)