473,378 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 18501
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

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

Similar topics

7
by: Chris | last post by:
Here's the situation: I work at a scientific institution and I have a portable electronic device which is used to take measurements. The device produces a very small amount of numerical data,...
2
by: A. Wiebenga | last post by:
Hi all! I am currently involved in a project in which I am supposed to write a XSLT-transformation sheet for some XML data. I will outline the situation first: I've got one large XML file...
5
by: Matt | last post by:
I wrote the tcp socket client-server program that the server will echo the message received from the client. In client program: char sendBuf; while(1) { cout << "Enter message:";...
2
by: edworboys | last post by:
I have designed a data entry form with a number of fields and a sub form. The first field (Country) is a combo box and the user selects a country. This, in turn reduces the number of options in the...
1
by: Sean McKaharay | last post by:
Hey All, I am trying to write an app that will transfer data from one SQL Server to another. Here is my situation: I have 2 computers neither one is connected. I have the same database on both...
2
by: =?Utf-8?B?SkRhdmlkZQ==?= | last post by:
Hello again! :( I'm trying to implement asynchronous DnD (and Copy/Paste) in a custom NSE: despite the lack of documentation, I found that i need my DataObject implement the optional interface...
3
by: Brian Walsh | last post by:
Hi to all that might be able to help me. I have created a pretty extensive database that has switchboards and all all of the like. My delima is that I need to be able to trasfer single recods from...
7
by: Lou | last post by:
I have a class that uses the serial port class Private SerialPort as New SerialPort When I receive the asyncronous serial port response it appears that data is on a different thread than my...
1
by: brooksjd | last post by:
Is there a way to automate a process that takes data from a folder and runs a form (that is already working manually) that will upload data to an online server?
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.