Read from serial port

M

mmrasheed

Hi,
I am newbie in python. I am working on Telit GM862 GPS/GPRS module
which has python interpreter built in. But it seems this problem is
pretty much related to general python structure.

I need a promt/terminal when the device is connected to PC. If user
enters a command by serial port and press "Enter" then the data is
read by the device and work on the command. This is similar to
readline() function. Unfortunately there is no readline() function for
GM862 device. The following functions are available for serial port
data receive-

SER.read() - reads whole string at a time from buffer
SER.receive(timeout) - reads if there is any input in the buffer
andwait for the input upto timeout
SER.receivebyte(timeout) - reads if there is any byte sent in the
buffer and wait for the input upto timeout

Now, is this possible to build a readline() using the functions above?
Can I redirect the terminal or serial port data to receive it as a
whole string when entered "\n"?

Please advise me how can I solve this problem.

Thanks a lot.
 
B

binaryjesus

Hi,
I am newbie in python. I am working on Telit GM862 GPS/GPRS module
which has python interpreter built in. But it seems this problem is
pretty much related to general python structure.

I need a promt/terminal when the device is connected to PC. If user
enters a command by serial port and press "Enter" then the data is
read by the device and work on the command. This is similar to
readline() function. Unfortunately there is no readline() function for
GM862 device. The following functions are available for serial port
data receive-

SER.read() - reads whole string at a time from buffer
SER.receive(timeout) - reads if there is any input in the buffer
andwait for the input upto timeout
SER.receivebyte(timeout) - reads if there is any byte sent in the
buffer and wait for the input upto timeout

Now, is this possible to build a readline() using the functions above?
Can I redirect the terminal or serial port data to receive it as a
whole string when entered "\n"?

Please advise me how can I solve this problem.

Thanks a lot.

Well i dont think its much of a problem.
What i think you should be doing is to read the data into a secondary
buffer. it could be a simple string or a specialized buffer class. You
would need a method to update() this secondary buffer. ie read from
port and append the read data to our buffer.

That done you can easily make a readline function. lol i think even a
15yr old can make a readline() from here.

btw does ur gm862 support multiple thread? if yes u can put the
update() function code and also a method launcher when ever a
particular command is sent to the device using a serial connection!

hope it helps !
BJ
 
F

Fredrik Lundh

I need a promt/terminal when the device is connected to PC. If user
enters a command by serial port and press "Enter" then the data is
read by the device and work on the command. This is similar to
readline() function. Unfortunately there is no readline() function for
GM862 device. The following functions are available for serial port
data receive-

SER.read() - reads whole string at a time from buffer
SER.receive(timeout) - reads if there is any input in the buffer
andwait for the input upto timeout
SER.receivebyte(timeout) - reads if there is any byte sent in the
buffer and wait for the input upto timeout

Unless I'm missing something, a simple loop should be all you need:

import SER

def readline(timeout=36000):
line = ""
while 1:
ch = SER.receivebyte(timeout)
if ch < 0:
raise IOError("timeout")
if ch == 10:
break # stop collecting when we get LF
if ch == 13:
continue # ignore CR, in case we get CR+LF
line = line + chr(ch)
return line

Tweak as necessary.

(and before anyone pops up with a "better" way to do that, keep in mind
that the GM862 device uses a custom version of Python 1.5.2.)

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,037
Messages
2,570,371
Members
47,015
Latest member
vorix28193

Latest Threads

Top