473,756 Members | 7,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read from Serial Port

I'd like to read ASCII data from a serial port, but (once again) I'm having
trouble getting started. (Can't seem to find the basic level of docs to
get going <sigh>)

I'd like to use only standard "built-in" modules if possible.

Could somebody offer a simple code-snippet to get me started reading from a
serial port?

Thanks!
--
Casey Bralla
Chief Nerd in Residence
The NerdWorld Organisation
http://www.NerdWorld.org
Jan 21 '06 #1
6 3589
Casey Bralla wrote:
I'd like to read ASCII data from a serial port, but (once again) I'm having
trouble getting started. (Can't seem to find the basic level of docs to
get going <sigh>)

I'd like to use only standard "built-in" modules if possible.

Could somebody offer a simple code-snippet to get me started reading from a
serial port?

While it isn't built in, the standard solution to these problems is the
excellent "pyserial" module.

Google is your friend, and since pyserial is the first hit on a search
for "python serial port" I would recommend you remember that in future
.... that's why there's no URL in this message : Googling yourself will
help the lesson sink in.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Jan 21 '06 #2
On 2006-01-21, Casey Bralla <No****@Nowhere .com> wrote:
I'd like to read ASCII data from a serial port, but (once
again) I'm having trouble getting started. (Can't seem to
find the basic level of docs to get going <sigh>)

I'd like to use only standard "built-in" modules if possible.
Then there aren't going to be any "basic level docs" because
it's just not an easy thing to do. You pretty much have to do
exactly what you would do in C. So, for Linux/Unix, study up on
http://www.easysw.com/~mike/serial/serial.html. Then do the
exact same thing in python using the "os" module stuff that
deals with file descriptors and termios. And you'll probably to
use select to impliment a timeout when there's no data:

http://www.python.org/doc/2.4.2/lib/os-fd-ops.html
http://www.python.org/doc/2.4.2/lib/module-termios.html
http://www.python.org/doc/2.4.2/lib/module-select.html

For Win32, you'll have to do all of the nasty Win32
CreateFile() calls and such-like. IMO, It's even uglier than
the Posix termios stuff (though not by much). For example,
CreateFile() takes seven parameters, and none of them default
to useful values (it's places like that where Win32's VMS
heritage comes shining through in a manner that instead of
illumnating anything temporarily blinds you and causes you to
run into a tree).
Could somebody offer a simple code-snippet to get me started
reading from a serial port?


Not using just the built-in modules. There is no such simple
code-snippet. If you want simple use pyserial which handles
all of the tricky bits for you:

http://pyserial.sourceforge.net/

It's not a "built-in" module, but it's what you want to use
unless you already know all the contortions require to do
serial port stuff using Posix or Win32 system calls.

Really.

If you truly, madly, deeply want to use just built-in modules,
then your best bet is to download pyserial and copy the
appropriate platform-specific code. I'll be happy to answer
questions about the Posix parts of it, but I had the portion of
my brain containing Win32 knowledge cauterized.

--
Grant Edwards grante Yow! I'm losing my
at hair...did it go to
visi.com ATLANTIC CITY??
Jan 21 '06 #3
> appropriate platform-specific code. I'll be happy to answer
questions about the Posix parts of it, but I had the portion of
my brain containing Win32 knowledge cauterized.


I'm working on that. Last time I checked there was even some AMIGA
pre-AGA-stuff in there. Which I actually think is cute, but could be
filled with other more useful stuff. Any suggestions for a good
cauterization service?

SCNR,

Diez
Jan 21 '06 #4
Grant Edwards wrote:
On 2006-01-21, Casey Bralla <No****@Nowhere .com> wrote:
I'd like to use only standard "built-in" modules if possible.
Then there aren't going to be any "basic level docs" because
it's just not an easy thing to do. You pretty much have to do
exactly what you would do in C.

For Win32, you'll have to do all of the nasty Win32
CreateFile() calls and such-like. IMO,


And given that win32all is *not* standard and built-in, at least with
the standard distribution, you can't do Win32 at all if you insist on
builtin modules.

But doing so would be unreasonable, and fortunately the OP indicates
that that's just his desire, not a firm requirement, so pyserial it is! :-)

-Peter

Jan 21 '06 #5
On 2006-01-21, Diez B. Roggisch <de***@nospam.w eb.de> wrote:
appropriate platform-specific code. I'll be happy to answer
questions about the Posix parts of it, but I had the portion of
my brain containing Win32 knowledge cauterized.


I'm working on that. Last time I checked there was even some AMIGA
pre-AGA-stuff in there. Which I actually think is cute, but could be
filled with other more useful stuff. Any suggestions for a good
cauterization service?


Yea, um, hmm. Don't remember that either. Sloppy work then, eh?
Just as well.

--
Grant Edwards grante Yow! ... A housewife
at is wearing a polypyrene
visi.com jumpsuit!!
Jan 21 '06 #6
Casey Bralla wrote:
I'd like to read ASCII data from a serial port, but (once again) I'm having
trouble getting started. (Can't seem to find the basic level of docs to
get going <sigh>)

I'd like to use only standard "built-in" modules if possible.

Could somebody offer a simple code-snippet to get me started reading from a
serial port?

Thanks!

Here's some code which should help to get you started, I thought rather
than leaving you in the hands of Google (which is not your friend, no
more than MaccyDs is, as Flavour Flav said - 'Don;t believe the hype'),
this may be helpful :

Here's a bit cut out from my own code using pySerial - some of the
variables are not detailed but you can get the idea :

self.__objSeria lPort = serial.Serial(s elf._dctConnect ionParams
['ConnectionID'],
self._dctConnec tionParams
['BaudRate'],
self._dctConnec tionParams
['DataBits'],
self._dctConnec tionParams
['Parity'],
self._dctConnec tionParams
['StopBits'],
10000, #TimeOut

int(self._dctCo nnectionParams
['XONXOFF']),

int(self._dctCo nnectionParams
['RTSCTS']))

To send data with pyserial, you do this :

self.__objLock. acquire()
try:
try:
self.__objSeria lPort.write(pSt rMessage)
except StandardError:
self._objCurren tState = self._STATUS_CO NSTS.ERROR
raise
else:
self._objCurren tState = self._STATUS_CO NSTS.CONNECTED
finally:
self.__objLock. release()

The lock on this isn't needed for single threaded code but I would
recommend making a class which deals with the communication in a
thread-safe manner (the connection and disconnection as well - the above
bit should be in a lock as well).

PySerial doesn't have the concept of Observer patterns to get
message coming back in so you'll have to make a polling thread to
observe the serial port when you want to read data, here's the checking
bit - I'll leave the threading and observers up to you:

def __checkSerial(s elf):
"""
Checks serial port to see if anything is available to read
"""
self.__objLock. acquire()
try:
try:
intNoChars = self.__objSeria lPort.inWaiting ()
if intNoChars > 0:
strReceivedStri ng =
self.__objSeria lPort.read(intN oChars)
self.fireNewMes sage(strReceive dString)
except:
raise
finally:
self.__objLock. release()

PySerial wraps all the platform specific stuff you, so you should
really use that, it behaves fairly well - the only real problem is a
lack of an observer interface but you can solve that as detailed above.
One final thing, don;t forget that RS232 isn't really a standard - it's
more like a rumour :-).

Cheers,

Neil

--

Neil Benn
Senior Automation Engineer
Cenix BioScience
BioInnovations Zentrum
Tatzberg 47
D-01307
Dresden
Germany

Tel : +49 (0)351 4173 154
e-mail : be**@cenix-bioscience.com
Cenix Website : http://www.cenix-bioscience.com

Jan 23 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13193
by: willie | last post by:
Hi, I'm writing a program which requires the use of three serial ports and one parallel port. My application has a scanning devices on each port, which I can access fine with pyserial. However, I'm unsure of how exactly I should be designing the program, I thought I could use threading to start class: class scanner(Thread): def __init__(self,port):
3
5055
by: collinm | last post by:
hi i send a command to a led display, the led display is suppose to return me some character i write a string on a serial port void ledDisplayExist() { char msg={'\0', '\0', '\0', '\0', '\0', '\1', 'Z', '0', '0',
0
2138
by: asam.reddy | last post by:
Hi, I am trying to read serial port using the MSComm control in VS.NET2003(referred from the VS6.0). I have the code already in VB6 which I am coding to C#.NET. After setting all the required settings of the COM1 Port Dim buffer As Variant Dim arr() As Byte
4
11206
by: joe bloggs | last post by:
I am writing a mobile application to interface with a legacy system and I am planning to use web services to communicate with this system. The legacy system receives data through a serial port. What I would like to do is make the serial port accessible via a web service. The web service and the legacy application would be running on the same machine. The mobile application would access the web service via a network connection. It...
2
5255
by: hani | last post by:
hi, i'm try to do a program that can read serial port in web page. can anyone give me any opinion to do this? i've download the javacomm and try the examples. i'ts working. but it is in java client. someone advise me to use javabean. buat i don't quite understand how. thank you.
2
2353
by: yo2osv | last post by:
I want read serial port and show on the screen using C programing
0
1474
by: boss1 | last post by:
i have a problem with php coding.that is i dont know how to read rs 232 serial port by using php ? my operating system is: windows-2000 professional. can u kindly help me ? i'll be very much greatful to u. Thanks.
4
4816
by: max_mont | last post by:
Hi all, I'm a newbie in .NET technology. I've already developed Serial communication applications in C++ (WIN32). And I wanted to migrate to .NET technology. There is a serial component in framework to read and write on serial port. I would like to make asynchronous reception. I saw that we can pass a delegate to the serial class which is call when some data is readen on the port.
0
4566
by: ghjk | last post by:
I want to read sms from GSM modem using C# in serial communication. I wrote the code. But i want to do it automatically. I put my code here and please tell me how can i do it automatically. public partial class SMS : Form { //create an Serial Port object SerialPort sp = new SerialPort(); public SMS() {
6
6669
by: terry | last post by:
Hi, I am trying to send a character to '/dev/ttyS0' and expect the same character and upon receipt I want to send another character. I tired with Pyserial but in vain. Test Set up: 1. Send '%' to serial port and make sure it reached the serial port. 2. Once confirmed, send another character.
0
9384
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9212
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
9973
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
9645
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...
1
7186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6473
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
5069
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...
0
5247
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.