473,666 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RS232 data transfers OK with Hyperterminal, but not with PySerial

17 New Member
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 18591
bvdet
2,851 Recognized Expert Moderator Specialist
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 New Member
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 New Member
.. 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("DCFTP 0001\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 Recognized Expert Moderator Specialist
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 Recognized Expert Moderator Specialist
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 New Member
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\se rial_hello world.py", line 18, in <module>
se.open() #open port
File "C:\Python25\Li b\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 Recognized Expert Moderator Specialist
Try closing the port before opening it.
Sep 15 '10 #8
Dave067
17 New Member
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 Recognized Expert Moderator Specialist
Thanks for the feedback Dave. Nice thread!
Sep 15 '10 #10

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

Similar topics

7
12749
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, about 10 to 15 numbers per measurement. This operation is performed frequently and I would like to implement an automatic solution that moves this data across a windows network and into the fields of an Access database application. The device...
2
1895
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 which is roughly build up like this: <forms> <subform>
5
404
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:"; cin.getline(sendBuf,100); rVal = send(theSocket, sendBuf, strlen(sendBuf), 0);
2
2035
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 second field (Prospect Name). The user then uses the subform to enter a document location. The problem is this:
1
1578
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 computers. I need to update the data at the end of the night to reflect the changes on the particular database. So the data collected on computer1 needs to get to computer2 and the data on computer2 needs to get to computer1.
2
2694
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 IAsyncOperation. I want to always use asynchronous operations, so I added the following lines to my DataObject class (I use ATL): class ATL_NO_VTABLE PPDataObject : public CComObjectRootEx<CComSingleThreadModel>, public IDataObject, public...
3
1370
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 a form to another table. I would like to do this with a command using an event proceedure. I am developing this for my fellow coworkers to enable them to more effiecienty view thier data and move data around as need arises. Here is what I have in a...
7
4077
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 UI. When I try to raise an event in my class my frmMain doesn't get the callback. i think this is a threading issue. Question is how do I get the serial port response from my vb class to my main form.
1
1292
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
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.