I'm a Python newbie trying to write a datalogger to acquire data from a laboratory meter using RS232.
Hardware: Samsung N100 laptop. Maplin's USB-Serial converter dongle. TSI 4100 series laboratory flow meter
Software: Windows XP. Python 2.5 & IDLE interface. HDD Free Serial Port Monitor 3.31. Hyperterminal Private Edition v5.0 (Drivers for USB dongle installed, Windows firewall exception made for COM1).
RS232 Port Settings: Baud rate:38400, data bits:8, Parity:None, Stop bits:1, Flow control: None
The meter requires me to send a command code before it returns data to me (both in ASCII format). In its simplest form, sending the test command "?<CR>" (= 0x3F
0x0D) should return the message "OK<CR>" (= 0x4F 0x4B 0x0D).
This works fine via Hyperterminal. The USB adaptor is correctly identified as 'COM1', and I can verify that the correct data is sent or received by:
1) Seeing a "data transfer" indicator flash on the meter
2) Seeing the ASCII & HEX codes for the sent and received data displayed the serial port monitor software
3) Using an oscilloscope on the serial port leads and analysing the pulses.
However when I try to do the same in Python using the following code, the code executes fine and indicates that COM1 has been used, but no actual data transfer
occurs: the monitor's indicator doesn't flash, and although the serial port monitor registers that the connection has been opened and then closed by Python,
no data is sent or received.
I've checked the documentation and examples on the pySerial website and relevant postings on the forum, but I'm stuck. I'm sure I must have made a dumb mistake somewhere in my code, but can't find it, and would be very grateful for your help. Also as a Python newbie, any general hints on how to write my code neater and more compactly would be appreciated.
Many thanks in anticipation
Dave
Python output is:
>>>
Enter name: dave
Hi dave, opening port
COM1
[]
done!
>>>
Code follows:
Expand|Select|Wrap|Line Numbers
- import serial
- se = serial.Serial()
- se.baudrate = 38400
- se.bytesize = 8
- se.stopbits = 1
- se.xonxoff = 0
- se.rtscts = 0
- se.timeout = 0
- se.port = 0 # = COM1
- parity=serial.PARITY_NONE
- x = raw_input("Enter name: ")
- print "Hi " + x +", opening port"
- se.open() #open port
- print se.portstr # confirm which port was really used
- se.write = "?\r" # send command string to meter
- data = se.readline() # read data from meter
- data = data.split()
- print data
- se.close() # close port
- print "done!"