472,118 Members | 1,184 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,118 software developers and data experts.

RS232 data transfers OK with Hyperterminal, but not with PySerial

17
Hi

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
  1. import serial
  2. se = serial.Serial()
  3. se.baudrate = 38400
  4. se.bytesize = 8
  5. se.stopbits = 1
  6. se.xonxoff = 0
  7. se.rtscts = 0
  8. se.timeout = 0
  9. se.port = 0     # = COM1
  10. parity=serial.PARITY_NONE
  11.  
  12. x = raw_input("Enter name: ")
  13. print "Hi " + x +", opening port"
  14. se.open()               #open port
  15.  
  16. print se.portstr       # confirm which port was really used
  17. se.write = "?\r"    # send command string to meter 
  18.  
  19. data = se.readline()    # read data from meter 
  20. data = data.split()
  21. print data
  22.  
  23. se.close()             # close port
  24. print "done!"
  25.  
Sep 15 '10 #1
10 18153
bvdet
2,851 Expert Mod 2GB
You never sent the command. The write method call requires parentheses and an argument. Try this:
Expand|Select|Wrap|Line Numbers
  1. se.write("?\r")
Sep 15 '10 #2
Dave067
17
Many thanks!!
Now the parentheses are in place, the data is sent to the monitor fine...
In order to receive the reply from the monitor, I had to increase the se.timeout parameter to 4 to allow time for the monitor to respond and send a return signal. Not sure whether this is the proper way to do things, but it seem to work.

Is there a neater way of writing all of the port initiallisation parameters, so they're all in a set of parentheses; or is it OK to write them in a long list like I've done?

Thanks again

Kind regards

Dave
Sep 15 '10 #3
Dave067
17
.. also, when I was accessing the meter via Hyperterminal, the ASCII command string "DCFTP0001"<CR> would instruct the meter to send back an 18 byte string of characters corresponding to the flow rates that I am trying to log. This worked fine.

However...
When I replace the ASCII sequence to check communication in my Pythonscript:
se.write("?\r")
...with the command to request data:
se.write("DCFTP0001\r")

..the sequence is sent in the Serial Port Monitor, but the data indicator light on the meter doesn't flash, no data is returned, and bizarrely, the output from Python is the same as for the test sequence:
"OK"

Is there some sort of buffer that I have to clear with a Python command between successive "read" and "write" commands to the port?

Thanks again

Dave
Sep 15 '10 #4
bvdet
2,851 Expert Mod 2GB
If your code works for you, then you have well written code. :)
Seriously, the way you set the parameters is fine (except for the parity assignment), but you could have set the parameters when you instantiated serial.Serial(), which is what I prefer to do. Example:
Expand|Select|Wrap|Line Numbers
  1. se = serial.Serial(port=0,
  2.                    baudrate=38400,
  3.                    bytesize=serial.EIGHTBITS,
  4.                    parity=serial.PARITY_NONE,
  5.                    stopbits=serial.STOPBITS_ONE,
  6.                    timeout=4,
  7.                    xonxoff=False,
  8.                    rtscts=False,
  9.                    writeTimeout=None,
  10.                    dsrdtr=False,
  11.                    interCharTimeout=None)
When using keywords as shown above, it makes the code quite readable.

Your next step may be to create a GUI interface. Tkinter, wxPython, and several other GUI toolkits are well suited to applications such as yours.

The pySerial API documentation can be found here.
Sep 15 '10 #5
bvdet
2,851 Expert Mod 2GB
Honestly, I've never used pySerial. Would you post the code you used to send the command? You could try setting a write timeout.
Sep 15 '10 #6
Dave067
17
Hi
Thanks for the advice & links to the documentation.
Ports open and close the in serial monitor, but no data transferred in either direction, and PySerial seems to be unable to access COM1 any more - not sure why.

Have tried rebooting the laptop, disconnecting/re-inserting USB dongle & flow meter. No joy :-(
Any suggestions welcome!
Thanks
Dave

PySerial output:
>>>
Enter name: dave
Hi dave, opening port

Traceback (most recent call last):
File "C:\Python25\serial_hello world.py", line 18, in <module>
se.open() #open port
File "C:\Python25\Lib\site-packages\serial\serialwin32.py", line 56, in open
raise SerialException("could not open port %s: %s" % (self.portstr, ctypes.WinError()))
SerialException: could not open port COM1: [Error 13] Access is denied.
>>>

Expand|Select|Wrap|Line Numbers
  1. import serial
  2. se = serial.Serial(port=0,
  3.                     baudrate=38400,
  4.                     bytesize=serial.EIGHTBITS,
  5.                     parity=serial.PARITY_NONE,
  6.                     stopbits=serial.STOPBITS_ONE,
  7.                     timeout=4,
  8.                     xonxoff=False,
  9.                     rtscts=False,
  10.                     writeTimeout=1,
  11.                     dsrdtr=False,
  12.                     interCharTimeout=None)
  13.  
  14. x = raw_input("Enter name: ")
  15. print "Hi " + x +", opening port"
  16.  
  17. se.open()               #open port
  18. print se.portstr       # confirm which port was really used
  19. se.write("DCFTP0001\r")     # send command string to meter requesting 1 sample (0001) for Flow(F), Temp(T) and Pressure (P)
  20.  
  21. data = se.readline()    # read data from meter 
  22. #data = data.split()
  23. print data
  24.  
  25. se.close()             # close port
  26. print "done!"
  27.  
Sep 15 '10 #7
bvdet
2,851 Expert Mod 2GB
Try closing the port before opening it.
Sep 15 '10 #8
Dave067
17
Hi again
I think the problem was a conflict on accessing COM1 due to a combination of both Hyperterminal & Windows Automatic Updates running in the background, and also the Serial Port Monitor being opened in the wrong sequence, before the USB dongle was connected. D'Oh!
Am now up and running again :-)
If I get the rest of the code written, I'll post it to the group in case it is of help to anyone working on a similar project.
Thanks again for your help
Dave
Sep 15 '10 #9
bvdet
2,851 Expert Mod 2GB
Thanks for the feedback Dave. Nice thread!
Sep 15 '10 #10
Nawar0066
1 Bit
Hallo Dave,

could you help me please if you got the code because i have the same problem with same device
Jul 8 '21 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by A. Wiebenga | last post: by
1 post views Thread by Sean McKaharay | last post: by
2 posts views Thread by =?Utf-8?B?SkRhdmlkZQ==?= | last post: by
7 posts views Thread by Lou | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.