Mailing List Archive

Serial programming
I am looking for some example in python about send and receive data from
serial port. Like send "ATZ" and receive "OK". Any help?? Thanks a lot!!


Doraemon
Serial programming [ In reply to ]
Assuming your in Windows.
http://www.python.org/windows/
Although I expect there is a better way using win32 stuff.
http://msdn.microsoft.com/isapi/msdnlib.idc?theURL=/library/sdkdoc/portals/m
ainport.htm

--
--Darrell
DORAEMON.HK <abc@def.ghi.jkl> wrote in message
news:7jp1jt$8hg$1@imsp009a.netvigator.com...
> I am looking for some example in python about send and receive data from
> serial port. Like send "ATZ" and receive "OK". Any help?? Thanks a lot!!
>
>
> Doraemon
>
>
>
>
>
>
Serial programming [ In reply to ]
Oh! Sorry! I am using Linux

Darrell <news@dorb.com> wrote in message
news:cuV73.1455$h14.6642@newsr2.twcny.rr.com...
> Assuming your in Windows.
> http://www.python.org/windows/
> Although I expect there is a better way using win32 stuff.
>
http://msdn.microsoft.com/isapi/msdnlib.idc?theURL=/library/sdkdoc/portals/m
> ainport.htm
>
> --
> --Darrell
> DORAEMON.HK <abc@def.ghi.jkl> wrote in message
> news:7jp1jt$8hg$1@imsp009a.netvigator.com...
> > I am looking for some example in python about send and receive data from
> > serial port. Like send "ATZ" and receive "OK". Any help?? Thanks a
lot!!
> >
> >
> > Doraemon
> >
>
Serial programming [ In reply to ]
DORAEMON.HK <abc@def.ghi.jkl> wrote:
: I am looking for some example in python about send and receive data from
: serial port. Like send "ATZ" and receive "OK". Any help?? Thanks a lot!!

You can open the device file directly and handle the information
yourself. UNIX devices are handle just like files. You can change
device attributes (speed, parity, etc.) with the termios module or
fcntl.ioctl function.

modem = open('/dev/cua0', 'r+')
modem.write('AT\r')
f = modem.read()
if f[:2] != 'OK':
raise ValueError, "modem not okay"
...

There are some Expect modules/extensions around that could help you as
well; my ExpectPy extention is at
http://starship.python.net/~arcege/ExpectPy/. You can find others
through the Python website.

import ExpectPy
modem = ExpectPy.spawnfile(open('/dev/cua0', 'r+'))
modem.send("AT\r")
try:
f = modem.expect(
(ExpectPy.EXACT, "OK\r\n", "OK"),
(ExpectPy.GLOB, "*\r\n", "Not OK")
)
if f == "Not OK":
raise ValueError, "modem not okay"
except (ValueError, ExpectPy.TimeoutError), value:
raise ValueError, value

I hope this helps.

-Arcege
Serial programming [ In reply to ]
I try but not success. Modem receive my command (I try to dail out)
but I have not get any respone. Anything I missed??

My program:
#!/usr/bin/python

modem = open('/dev/ttyS0', 'w+')
modem.write('ATZ\r')
print modem.read() # no respone here
modem.close()

DORAEMON.HK

In article <i5a83.2876$nn.847925@news.shore.net>,
"Michael P. Reilly" <arcege@shore.net> wrote:
> DORAEMON.HK <abc@def.ghi.jkl> wrote:
> : I am looking for some example in python about send and receive data
from
> : serial port. Like send "ATZ" and receive "OK". Any help?? Thanks
a lot!!
>
> You can open the device file directly and handle the information
> yourself. UNIX devices are handle just like files. You can change
> device attributes (speed, parity, etc.) with the termios module or
> fcntl.ioctl function.
>
> modem = open('/dev/cua0', 'r+')
> modem.write('AT\r')
> f = modem.read()
> if f[:2] != 'OK':
> raise ValueError, "modem not okay"
> ...
>
> There are some Expect modules/extensions around that could help you as
> well; my ExpectPy extention is at
> http://starship.python.net/~arcege/ExpectPy/. You can find others
> through the Python website.
>
> import ExpectPy
> modem = ExpectPy.spawnfile(open('/dev/cua0', 'r+'))
> modem.send("AT\r")
> try:
> f = modem.expect(
> (ExpectPy.EXACT, "OK\r\n", "OK"),
> (ExpectPy.GLOB, "*\r\n", "Not OK")
> )
> if f == "Not OK":
> raise ValueError, "modem not okay"
> except (ValueError, ExpectPy.TimeoutError), value:
> raise ValueError, value
>
> I hope this helps.
>
> -Arcege
>
>


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.