I got project to make on my University two weeks ago. I had to read temperature from METEX device connected on COM port. I decided to use Python's pyserial. I found on Internet a few examples and in accordance with them I made something like this:
import serial
ser = serial.Serial(
port='\\.\COM1',
baudrate=1200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.SEVENBITS,
timeout=5
)
if ser.isOpen():
ser.close()
ser.open()
ser.write('D')
s=ser.read(13)
print s
ser.close
and it doesn't work but it should do. Generally, this device send to your computer 13 bytes contain temperature when you send 'D' char to it. I couldn't fix this although I tried to change everything in code and in system configuration. My lecturer couldn't help me too beacuse he doesn't work in Python. He tried beacuse he show me his old program written in C but he has only exec file without code. Program worked on same computer which I executed my Python scripts, so I was sure that COM port, device and system work correctly.
Today, accidentally I add to code this:
import time
import serial
ser = serial.Serial(
port='\\.\COM1',
baudrate=1200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.SEVENBITS,
timeout=5
)
ser.setRTS(False)
time.sleep(0.5)
ser.setRTS(True)
time.sleep(0.5)
ser.setRTS(False)
if ser.isOpen():
ser.close()
ser.open()
ser.write('D')
s=ser.read(13)
print s
ser.close
and now it works perfectly. Why??!! I must make report from this exercise and describe my code line by line. Please, help me.
Operating system: Windows XP.
serial.Serial("COM1",1200,timeout=5)