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: -
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!"
-
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:
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
.. 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
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: - se = serial.Serial(port=0,
-
baudrate=38400,
-
bytesize=serial.EIGHTBITS,
-
parity=serial.PARITY_NONE,
-
stopbits=serial.STOPBITS_ONE,
-
timeout=4,
-
xonxoff=False,
-
rtscts=False,
-
writeTimeout=None,
-
dsrdtr=False,
-
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.
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.
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.
>>> -
import serial
-
se = serial.Serial(port=0,
-
baudrate=38400,
-
bytesize=serial.EIGHTBITS,
-
parity=serial.PARITY_NONE,
-
stopbits=serial.STOPBITS_ONE,
-
timeout=4,
-
xonxoff=False,
-
rtscts=False,
-
writeTimeout=1,
-
dsrdtr=False,
-
interCharTimeout=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("DCFTP0001\r") # send command string to meter requesting 1 sample (0001) for Flow(F), Temp(T) and Pressure (P)
-
-
data = se.readline() # read data from meter
-
#data = data.split()
-
print data
-
-
se.close() # close port
-
print "done!"
-
bvdet 2,851
Recognized Expert Moderator Specialist
Try closing the port before opening it.
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
bvdet 2,851
Recognized Expert Moderator Specialist
Thanks for the feedback Dave. Nice thread!
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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>
|
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);
|
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:
|
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.
| |
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...
|
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...
|
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.
|
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?
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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();...
|
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...
|
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
| |
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...
| | |