user3903403
user3903403

Reputation: 51

Sending serial message in python

I am trying to send this command ":01050801FF00F2" over serial in python 2.7 , with no success. The code that i use is :

import serial, sys ,socket
from time import sleep
port = "COM9"
baudRate = 9600
try:
     ser = serial.Serial(port, baudRate, timeout=1)
     if not ser.isOpen():
            ser.open()
except Exception, e:
   print("Error opening com port. Quitting."+str(e))
   sys.exit(0)
print("Opening " + ser.portstr) 

#this is few ways i am trying to send with
c = '01050801FF00F2'
ser.write(c.encode('utf-8'))
sleep(3)
ser.flushInput() #flush input buffer, discarding all its contents
ser.flushOutput()#flush output buffer, aborting current output
                 #and discard all that is in buffer
c = ':01050801FF00F2'
ser.write(c.encode('hex'))

sleep(3)
ser.write(':01050801FF00F2')

Upvotes: 3

Views: 2433

Answers (2)

J. P. Petersen
J. P. Petersen

Reputation: 5031

If those are hex values, this should work:

c = '\x01\x05\x08\x01\xFF\x00\xF2'
ser.write(c)

Upvotes: 1

skyhammer
skyhammer

Reputation: 117

Have you tried sending the command using bytearray?

Perhaps you could try:

ser.write(b':01050801FF00F2')

or

ser.write(':01050801FF00F2'.encode())

Upvotes: 0

Related Questions