Python Forum
Serial communication with a board
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!
Reply
#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
Reply
#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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  RPi and smartphone Bluetooth communication GigiG 0 335 Mar-18-2025, 09:54 AM
Last Post: GigiG
  pyserial/serial "has no attribute 'Serial' " gowb0w 11 21,210 Sep-27-2024, 12:18 PM
Last Post: NigelHalse
  Trying to make a board with turtle, nothing happens when running script Quascia 3 1,681 Nov-01-2023, 03:11 PM
Last Post: deanhystad
Photo HOW FIX MY BOARD GAME LAZABI 3 2,273 Apr-01-2022, 04:23 PM
Last Post: BashBedlam
  The game should now run and terminate once the board is full, but still can’t identif rango 0 1,872 Jul-22-2021, 03:24 AM
Last Post: rango
  Enabling interrupt on Adafruits button/led board Moris526 0 2,583 Apr-30-2021, 03:29 PM
Last Post: Moris526
  High-Precision Board Voltage Reading from Python into PD Liam484 1 2,688 Mar-29-2021, 02:57 PM
Last Post: Marbelous
  Interrupt for Adafruits Neotrellis button/led board Moris526 0 2,313 Dec-28-2020, 05:42 AM
Last Post: Moris526
  packet radio communication EmpireAndrew 1 2,841 Nov-01-2019, 06:35 PM
Last Post: micseydel
  WiFi communication Milad 2 3,099 Sep-07-2019, 11:53 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020