By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,115 Members | 919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,115 IT Pros & Developers. It's quick & easy.

Read from serial port

P: n/a
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.
Aug 14 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Aug 14, 6:11 am, mmrash...@gmail.com wrote:
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
Aug 14 '08 #2

P: n/a
mm*******@gmail.com wrote:
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>

Aug 14 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.