James LeBaron
James LeBaron

Reputation: 11

Having trouble with pyserial

I've been trying to communicate with a piece of machinery. I verified the COM port and baud rate and 8N1 setup and open port, etc. using manual CMD. The manual indicates to use \ for start byte and / for end byte of telegram.

When I run it basically just hangs up, not sure what I'm doing wrong.

import serial char
ser = serial.Serial('COM6' , 115200)
ser.is_open
ser.write(b'\p/')
s = ser.read(9)
print(s)

Code

Upvotes: 1

Views: 530

Answers (3)

James LeBaron
James LeBaron

Reputation: 11

Amazingly helpful = Saiprasad Balasubramanian + Mike67 + Bukzor + SuzukiBKing I was successful with your advice. I plan to add the conditional later, thx SB ;).

    import serial
    import time
    ser = serial.Serial('COM6' , 115200)
    ser.write(bytearray([47, 112, 92]))
    time.sleep(2)
    s = ser.read_all()
    print(s) 

Upvotes: 0

SuzukiBKing
SuzukiBKing

Reputation: 178

From the manual it shows the returned codes for the "p" command as one or two characters. If you are trying to read 9 bytes with no timeout set it will hang until 9 bytes are received. Try using ser.read_all() after a short delay.

Upvotes: 0

You could try it out this way. I don't have a device to verify it

import serial
ser = serial.Serial('COM6' , 115200)

if ser.isOpen(): # Check is Serial is Open
    ser.write(b'\p/') # Write to Serial
    sleep(2) # Sleep for 2 seconds
    s = ser.read(9) # Read from Serial
else:
   print("Serial is not open")

Upvotes: 1

Related Questions