Reputation: 355
I am trying to read the temperature of a 1-wire device by issuing ascii commands to the 1-wire adapter. The problem is the ser.write('W0144') requires a carriage return but the code isn't sending it for some reason. The command ser.read(32) returns A69000001CFD7E328 when it should return 44 (from HA7E ascii commands/manual. If I enter the two ser.write commands (without the /r) in Windows XP Hyper Terminal, it works fine.
I've been on this a week (yes I'm a newbie) and I'm stumped. I've tried different timeouts and time.sleeps but no joy. Can anyone make a suggestion?
import serial
import time
ser = serial.Serial(port = 'COM1', baudrate=9600, bytesize=8, parity=serial.PARITY_NONE, stopbits=1, timeout=0)
#show the port is open
print ser.isOpen()
ser.write('A69000001CFD7E328')
time.sleep(1)
ser.write('WO144/r')
ser.read(32)
ser.close()
Upvotes: 1
Views: 907
Reputation: 1122512
Escape codes in python need a backslash:
>>> ord('\r')
13
You are sending two characters instead, a '/' and the letter 'r':
>>> len('\r')
1
>>> len('/r')
2
>>> list('/r')
['/', 'r']
Upvotes: 2