KMB
KMB

Reputation: 133

PySerial - cannot send STX

Ok, so I am a complete noob to pySerial. I am trying to communicate to a piece of lab equipment, but am having trouble just sending the STX (Start of Text) command. So far, my basic code looks like:

ser = serial.Serial(0, 19200, timeout=1,parity=serial.PARITY_ODD, rtscts=0)
ser.write(0x02) #ASCII STX is 0x2 in hex

But when I look at the 232 data on my scope, the STX I'm trying to send, doesn't look anything like a STX command sent in Hperterminal.

Any ideas? I am sure this is incredibly straight forward and I am just overlooking something trivial.

Thanks!

Upvotes: 2

Views: 4872

Answers (2)

Ferran
Ferran

Reputation: 14993

The write function in serial class accepts bytes or strings. You are passing an integer so the result is unknown, maybe is casting it to str so you are sending the char '2'.

The correct way to do it is :

ser = serial.Serial(0, 19200, timeout=1,parity=serial.PARITY_ODD, rtscts=0)
ser.write(chr(0x02)) #ASCII STX is 0x2 in hex

Upvotes: 6

kgiannakakis
kgiannakakis

Reputation: 104168

Are you sure that the same configuration is used for HyperTerminal and PySerial. You should make sure that the following are same:

  • Baudrate (you are using 19200)
  • Parity (you are using PARITY_ODD)
  • Number of data bits (pySerial default 8)
  • Stop bits (pySerial default 1)

Upvotes: 0

Related Questions