Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Pyserial - send and receive characters through linux serial port

Reply
Thread Tools

Pyserial - send and receive characters through linux serial port

 
 
terry
Guest
Posts: n/a
 
      04-25-2008
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.

I tried with write and read methods in Pyserial but no luck.

Can you help?

Thanking you all.
T
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      04-26-2008
En Fri, 25 Apr 2008 20:22:37 -0300, terry <(E-Mail Removed)> escribiˇ:

> 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.


I assume you have a device attached to /dev/ttyS0 that echoes back the
received characters?

> Test Set up:
>
> 1. Send '%' to serial port and make sure it reached the serial port.
> 2. Once confirmed, send another character.
>
> I tried with write and read methods in Pyserial but no luck.


Check the communication parameters (baud rate, parity, etc.), cable,
connectors, your device... AFAIK a lot of people is using pyserial so I'd
look the problem elsewhere. Try posting a short code example showing your
problem.

--
Gabriel Genellina

 
Reply With Quote
 
 
 
 
Bjoern Schliessmann
Guest
Posts: n/a
 
      04-26-2008
terry wrote:

> 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.


Pyserial works very well for me (despite the device I connect to has
quite a screwed protocol and implementation).

> Test Set up:
>
> 1. Send '%' to serial port and make sure it reached the serial
> port. 2. Once confirmed, send another character.
>
> I tried with write and read methods in Pyserial but no luck.


Are you actaully trying to get lucky by doing this, or are you
observing a specific problem, e.g. nothing is received or wrong
characters are received? Also, by what are they received?

Regards,


Bj÷rn

--
BOFH excuse #139:

UBNC (user brain not connected)

 
Reply With Quote
 
terry
Guest
Posts: n/a
 
      05-02-2008
On Apr 26, 8:21*am, Grant Edwards <(E-Mail Removed)> wrote:
> On 2008-04-25, terry <(E-Mail Removed)> wrote:
>
> > 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 withPyserialbut in vain.

>
> Pyserialworks. *I've been using it almost daily for many
> years. *Either your program is broken, your serial port is
> broken, or the device connected to the serial port is broken.
>
> > Test Set up:

>
> > 1. Send '%' to serial port and make sure it reached the serial port.
> > 2. Once confirmed, send another character.

>
> > I tried with write and read methods inPyserialbut no luck.

>
> > Can you help?

>
> Ah yes, the problem is in line 89 of your program.
>
> We've no way to help if you don't provide details. If you
> really want help, write as small a program as possible that
> exhibits the problem. *I'd like to emphasize _small_. The
> larger the program the less likely people are to look at it,
> and the less likely they are to find the problem if they do
> look at it.
>
> Much of the time the exercise of writing a small demo program
> will lead you to the answer. *If not, then post it, along with
> the output from the program that shows the problem.
>
> Then we can tell you what you did wrong.
>
> --
> Grant Edwards * * * * * * * * * grante * * * * * * Yow! I'm also against
> * * * * * * * * * * * * * * * * * at * * * * * * * BODY-SURFING!!
> * * * * * * * * * * * * * * * *visi.com * * * * * *


Here is the code.

"""Open serial connection"""
def openSerialConnection(self,serpt):
try:
s1 = serial.Serial(serpt,timeout=10)

except:
self.print_u("Failed to open serial port %s. " %serpt)

def enterThroughSerialPort(self,serpt):
s1 = serial.Serial(serpt,timeout=10)
self.print_u('Sending !!!!..')
while True:
s1.write('*')
c = s1.read(1)
if c:
self.print_u('Found "*" ')
break
print c
s1.write('enter\r')
s1.read('login')

if __name__ == '__main__':
serpt = '/dev/ttyS0'
x.openSerialConnection(serpt)
# funtion to reboot the device goes here ---#
x.enterThroughSerialPort(serpt)

After opening the serial connection, the device is rebooted followed
by sending '*' to serial port and reading back the same. I seem to
have problem while trying to read '*' back from serial port. First of
all I am not sure if serial port received the '*'.

Thanks!
 
Reply With Quote
 
terry
Guest
Posts: n/a
 
      05-02-2008
On May 2, 10:26*am, terry <(E-Mail Removed)> wrote:
> On Apr 26, 8:21*am, Grant Edwards <(E-Mail Removed)> wrote:
>
>
>
>
>
> > On 2008-04-25, terry <(E-Mail Removed)> wrote:

>
> > > 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 withPyserialbut in vain.

>
> > Pyserialworks. *I've been using it almost daily for many
> > years. *Either your program is broken, your serial port is
> > broken, or the device connected to the serial port is broken.

>
> > > Test Set up:

>
> > > 1. Send '%' to serial port and make sure it reached the serial port.
> > > 2. Once confirmed, send another character.

>
> > > I tried with write and read methods inPyserialbut no luck.

>
> > > Can you help?

>
> > Ah yes, the problem is in line 89 of your program.

>
> > We've no way to help if you don't provide details. If you
> > really want help, write as small a program as possible that
> > exhibits the problem. *I'd like to emphasize _small_. The
> > larger the program the less likely people are to look at it,
> > and the less likely they are to find the problem if they do
> > look at it.

>
> > Much of the time the exercise of writing a small demo program
> > will lead you to the answer. *If not, then post it, along with
> > the output from the program that shows the problem.

>
> > Then we can tell you what you did wrong.

>
> > --
> > Grant Edwards * * * * * * * * * grante * * * * * * Yow! I'm also against
> > * * * * * * * * * * * * * * * * * at * * * * * * * BODY-SURFING!!
> > * * * * * * * * * * * * * * * *visi.com * * * * * *

>
> Here is the code.
>
> """Open serial connection"""
> * * * * def openSerialConnection(self,serpt):
> * * * * * * try:
> * * * * * * * * s1 = serial.Serial(serpt,timeout=10)
>
> * * * * * * except:
> * * * * * * * * self.print_u("Failed to open serial port %s. " %serpt)
>
> * * * * def enterThroughSerialPort(self,serpt):
> * * * * * * s1 = serial.Serial(serpt,timeout=10)
> * * * * * * *self.print_u('Sending !!!!..')
> * * * * * * *while True:
> * * * * * * * * s1.write('*')
> * * * * * * * *c = s1.read(1)
> * * * * * * * *if c:
> * * * * * * * * * self.print_u('Found "*" ')
> * * * * * * * * * * break
> * * * * * * print c
> * * * * * * *s1.write('enter\r')
> * * * * * * *s1.read('login')
>
> if __name__ == '__main__':
> * * serpt = '/dev/ttyS0'
> * * x.openSerialConnection(serpt)
> * * # funtion to reboot the device goes here ---#
> * * x.enterThroughSerialPort(serpt)
>
> After opening the serial connection, the device is rebooted followed
> by sending '*' to serial port and reading back the same. I seem to
> have problem while trying to read '*' back from serial port. First of
> all I am not sure if serial port received the '*'.
>
> Thanks!- Hide quoted text -
>
> - Show quoted text -


This is the err message I received:

c = s1.read(1)
File "/usr/local/lib/python2.5/site-packages/serial/serialposix.py",
line 275, in read
ready,_,_ = select.select([self.fd],[],[], self._timeout)
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      05-03-2008
En Fri, 02 May 2008 15:44:09 -0300, terry <(E-Mail Removed)> escribiˇ:
> On May 2, 10:26*am, terry <(E-Mail Removed)> wrote:
>> On Apr 26, 8:21*am, Grant Edwards <(E-Mail Removed)> wrote:
>>
>> > Much of the time the exercise of writing a small demo program
>> > will lead you to the answer. *If not, then post it, along with
>> > the output from the program that shows the problem.

>>
>> Here is the code.
>>
>> """Open serial connection"""
>> * * * * def openSerialConnection(self,serpt):
>> * * * * * * try:
>> * * * * * * * * s1 = serial.Serial(serpt,timeout=10)
>>
>> * * * * * * except:
>> * * * * * * * * self.print_u("Failed to open serial port %s. " %serpt)
>>
>> * * * * def enterThroughSerialPort(self,serpt):
>> * * * * * * s1 = serial.Serial(serpt,timeout=10)
>> * * * * * * *self.print_u('Sending !!!!..')
>> * * * * * * *while True:
>> * * * * * * * * s1.write('*')
>> * * * * * * * *c = s1.read(1)
>> * * * * * * * *if c:
>> * * * * * * * * * self.print_u('Found "*" ')
>> * * * * * * * * * * break
>> * * * * * * print c
>> * * * * * * *s1.write('enter\r')
>> * * * * * * *s1.read('login')
>>
>> if __name__ == '__main__':
>> * * serpt = '/dev/ttyS0'
>> * * x.openSerialConnection(serpt)
>> * * # funtion to reboot the device goes here ---#
>> * * x.enterThroughSerialPort(serpt)
>>
>> After opening the serial connection, the device is rebooted followed
>> by sending '*' to serial port and reading back the same. I seem to
>> have problem while trying to read '*' back from serial port. First of
>> all I am not sure if serial port received the '*'.

>
> This is the err message I received:
>
> c = s1.read(1)
> File "/usr/local/lib/python2.5/site-packages/serial/serialposix.py",
> line 275, in read
> ready,_,_ = select.select([self.fd],[],[], self._timeout)


If you want some help, you have to help us to diagnose the problem.
The above lines are just a small excerpt of the complete error report that
surely you got; usually it includes the exception *type*, the exception
*value*, and the whole *stack trace*. Now we have to *guess* what happened
in that select call.
Also, when posting code, try to post the *actual* code you run - copy and
paste it. Do not retype it again. The code you posted has indentation
errors, uninitialized variables...
Try to make a *small*, runnable example that reproduces the problem;
remove anything that isn't esential to the issue being discussed. Write
down what is your expected behaviour, and what actually happens. If the
program halts due to an error, copy the *whole* report including the
exception type, message, and the complete traceback.

In this particular case, due to the serial device, it's improbable that
someone will be able to execute your program, but anyway try to write the
example code. Sometimes you may find the problem yourself just by trying
to write the example.

A few notes on the code:

- don't create many Serial objects; create only a single instance and use
it everywhere it is needed. (That is, instead of using '/dev/ttyS0' as an
argument, pass the Serial object already created, or save it as an
instance attribute: self.serial = Serial(...) )

- ensure you're using the right protocol parameters (baud rate, bits,
parity...) for the device. That's why it helps to create the Serial object
in a single place.

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Get USB ID of a serial port through pyserial? Python 0 03-31-2011 05:59 PM
Get USB ID of a serial port through pyserial? John Nagle Python 1 03-31-2011 02:54 PM
[pyserial - winXP] Serial port stop receiving data after a few hours, raise no error pauland80@gmail.com Python 0 08-07-2007 09:06 PM
[pyserial - winXP] Serial port stop receiving data after a few hours, raise no error pauland80@gmail.com Python 2 06-07-2007 05:56 AM
Socket Send and Receive, Same local port, Different Remote Port jeffburgoon@hotmail.com Perl Misc 1 03-02-2005 08:54 AM



Advertisments