Addressing the modem via pyserial

D

Doru-Catalin Togea

Hi!

I asked previously how to address the COM port from Python and I was
directed to pyserial. It is very elegant and I guess it works very well.

However, it seems that what I try to do is more complicated than I first
thought. I want to make my modem place a call using the number '1234'. The
modem is connected to COM3 on my computer. So I tried the following:

-------------------------------
import serial

ser = serial.Serial(2) # COM3
ser.timeout = 2 # otherwise the read(2) which follows blocks for ever
ser.write("atdt1234")
reply = ser.read(2)
print reply
ser.close()
print "ok"
-------------------------------

In the HyperTerminal which comes with Win XP Pro, I can set up a
connection to COM3 and there type the 'atdt1234' command. It works (I get
a call since I have the modem connected to a device with a phone on it).

My question is actually: will an at-command like 'atdt1234' translate to
ser.write('atdt1234')?

Secondly, a modem question: I have found a lot of web pages about
at-commands listing the hayes command set. Still many of them do not seem
to work on two different modems I have tried. Like '&$' should display an
overview of the commands supported by the modem. Ii tried this in the
HyperTerminal where I can place the above mentioned call, and I get
'ERROR'. I also get 'ERROR' for most other commands I try.

Any ideas?

Thanks,
Catalin

--

<<<< ================================== >>>>
<< We are what we repeatedly do. >>
<< Excellence, therefore, is not an act >>
<< but a habit. >>
<<<< ================================== >>>>
 
F

Fredrik Lundh

Doru-Catalin Togea said:
In the HyperTerminal which comes with Win XP Pro, I can set up a
connection to COM3 and there type the 'atdt1234' command. It works (I get
a call since I have the modem connected to a device with a phone on it).

even without pressing enter?
My question is actually: will an at-command like 'atdt1234' translate to
ser.write('atdt1234')?

try

ser.write('atdt1234\r\n')

or just

ser.write('atdt1234\n')

</F>
 
R

Roel Schroeven

Doru-Catalin Togea schreef:
My question is actually: will an at-command like 'atdt1234' translate to
ser.write('atdt1234')?

You'll have to include the end-of-line; I guess 'atdt1234\n' should
work, otherwise you can try 'atdt1234\r\n' or 'atdt1234\r'.

Also, don't forget that you have to set the modem into command mode if
it was not already (using '+++' and a pause before and/or after that
IIRC), but I guess that's not the problem in this case.
Secondly, a modem question: I have found a lot of web pages about
at-commands listing the hayes command set. Still many of them do not
seem to work on two different modems I have tried. Like '&$' should
display an overview of the commands supported by the modem. Ii tried
this in the HyperTerminal where I can place the above mentioned call,
and I get 'ERROR'. I also get 'ERROR' for most other commands I try.

Unfortunately the hayes command set is not a well-defined standard
supported by all modems. There are almost as many variations as there
are modems. In the good old days the user manual that came with the
modem had an overview of the available commands for that modem.
 
Joined
Aug 31, 2009
Messages
1
Reaction score
0
Accessing the modem via pySerial

Hello all

This is my first post on this forum, so greetings to everyone here :)

I have a similar problem to that posted by Doru-Catalin Togea. I am also trying to communicate with my internal modem using AT commands via pySerial in Windows.

To test this I started with this bit of code....

import serial
myport = serial.Serial(2, timeout=2) # COM 3 where modem is, with \n timeout to avoid locking up
myport.write('at&v')
reply = myport.readline()
print reply
myport.close()

This was just to see if my modem would reply to a simple status request (at&v).
However, when I read and print that reply all I get is an echo of whatever I wrote in the myport.write statement. In the above example therefore, the print reply statement comes back with 'at&v', just as entered in the write statement. I tried this with +++ before it, and \r, \n & even \r\n. Nothing made any difference.

It seems as if the myport.write function is not actually getting to the modem at all. Even if the statement was problematic for the modem in some way, I would have expected an error or message of some kind from the modem itself, not just an echo of whatever I write to the port.

Just to make the point I adjusted the above code as follows....

import serial
myport = serial.Serial(2, timeout=2) # COM 3 where modem is, with \n timeout to avoid locking up
myport.write('happy christmas')
reply = myport.readline()
print reply
myport.close()


The reply was happy christmas. Surely if the myport.write function was reaching the modem this would have caused some kind of error message.

I'd appreciate any advice anyone can offer as to what I might be mising here. I have looked through the docs for the python serial module but can't find anything helpful there.

Doru-Catalin Togea, did you manage to get your code working properly in the end ??

Thanks in advance if anyone can help me.
 
Joined
Dec 30, 2009
Messages
5
Reaction score
0
AT commands with pyserial

Hi everybody!

I have the same problem list above. I can´t connect a bluetooth device with serial module from Python, my script is very simple and it works directly on hyperterminal by windows. I posted my code below if someone has any hit I would appreciate. Thanks in advance.


import serial
ser = serial.Serial(2) # open first serial port
print ser
print ser.portstr # check which port was really used
ser.write("atd000195079CEA\n\r") # write a string

ser.write("123test")
 
Joined
Dec 30, 2009
Messages
5
Reaction score
0
It works well with
ser.write("atd000195079CEA\r") # write a string
reply = ser.readline()
print reply

Regards. Schawm RS
 
Joined
Nov 9, 2010
Messages
1
Reaction score
0
sendBreak

Hi,

I had a similar problem (couldn't read response from modem)

The solution that worked for me was to add a sendBreak() after the write command.

Code:
import serial

modem = serial.Serial(0)

modem.write('AT\r\n')
modem.sendBreak()

reply = modem.read(modem.inWaiting())
print reply

modem.close()
 

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
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top