Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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')
like image 460
user3903403 Avatar asked Jul 01 '26 16:07

user3903403


1 Answers

If those are hex values, this should work:

c = '\x01\x05\x08\x01\xFF\x00\xF2'
ser.write(c)
like image 79
J. P. Petersen Avatar answered Jul 04 '26 04:07

J. P. Petersen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!