Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #96981 > unrolled thread
| Started by | Timon Rhynix <timgeek951@gmail.com> |
|---|---|
| First post | 2015-09-22 04:19 -0700 |
| Last post | 2015-09-23 11:30 +0200 |
| Articles | 8 — 6 participants |
Back to article view | Back to comp.lang.python
Successfully send sms with python Timon Rhynix <timgeek951@gmail.com> - 2015-09-22 04:19 -0700
Re: Successfully send sms with python Pavel S <pavel@schon.cz> - 2015-09-22 05:16 -0700
Re: Successfully send sms with python mm0fmf <none@mailinator.com> - 2015-09-22 17:27 +0100
Re: Successfully send sms with python Cameron Simpson <cs@zip.com.au> - 2015-09-23 09:20 +1000
Re: Successfully send sms with python Michael Torrie <torriem@gmail.com> - 2015-09-22 22:25 -0600
Re: Successfully send sms with python Pavel S <pavel@schon.cz> - 2015-09-23 01:46 -0700
Re: Successfully send sms with python Cameron Simpson <cs@zip.com.au> - 2015-09-23 19:30 +1000
Re: Successfully send sms with python Laura Creighton <lac@openend.se> - 2015-09-23 11:30 +0200
| From | Timon Rhynix <timgeek951@gmail.com> |
|---|---|
| Date | 2015-09-22 04:19 -0700 |
| Subject | Successfully send sms with python |
| Message-ID | <05ab0edd-7285-4aca-b603-76e94dd8f0aa@googlegroups.com> |
Hello, I have used pyserial, sms0.4 and other libraries to send sms via huawei E1750 modem.
The code runs well and no error is thrown but the text message is not sent/delivered to the number.
One of my code is as follows:
import serial
import time
class TextMessage:
def __init__(self, recipient="0123456789", message="TextMessage.content not set."):
self.recipient = recipient
self.content = message
def setRecipient(self, number):
self.recipient = number
def setContent(self, message):
self.content = message
def connectPhone(self):
conn = 'COM13'
self.ser = serial.Serial(conn, 460800, timeout=5)
time.sleep(1)
def sendMessage(self):
self.ser.write('ATZ\r')
time.sleep(1)
self.ser.write('AT+CMGF=1\r')
time.sleep(1)
self.ser.write('''AT+CMGS="''' + self.recipient + '''"\r''')
time.sleep(1)
self.ser.write(self.content + "\r")
time.sleep(1)
self.ser.write(chr(26))
time.sleep(1)
print "message sent!"
def disconnectPhone(self):
self.ser.close()
When run it, the "message sent!" is printed but no message is sent/delivered.
Please assist on what I am missing. Thank you
[toc] | [next] | [standalone]
| From | Pavel S <pavel@schon.cz> |
|---|---|
| Date | 2015-09-22 05:16 -0700 |
| Message-ID | <17ead219-f3cc-45be-be56-74d5ac274731@googlegroups.com> |
| In reply to | #96981 |
On Tuesday, September 22, 2015 at 1:20:07 PM UTC+2, Timon Rhynix wrote:
> Hello, I have used pyserial, sms0.4 and other libraries to send sms via huawei E1750 modem.
> The code runs well and no error is thrown but the text message is not sent/delivered to the number.
> One of my code is as follows:
>
> import serial
> import time
>
> class TextMessage:
> def __init__(self, recipient="0123456789", message="TextMessage.content not set."):
> self.recipient = recipient
> self.content = message
>
> def setRecipient(self, number):
> self.recipient = number
>
> def setContent(self, message):
> self.content = message
>
> def connectPhone(self):
> conn = 'COM13'
> self.ser = serial.Serial(conn, 460800, timeout=5)
> time.sleep(1)
>
> def sendMessage(self):
> self.ser.write('ATZ\r')
> time.sleep(1)
> self.ser.write('AT+CMGF=1\r')
> time.sleep(1)
> self.ser.write('''AT+CMGS="''' + self.recipient + '''"\r''')
> time.sleep(1)
> self.ser.write(self.content + "\r")
> time.sleep(1)
> self.ser.write(chr(26))
> time.sleep(1)
> print "message sent!"
>
> def disconnectPhone(self):
> self.ser.close()
>
> When run it, the "message sent!" is printed but no message is sent/delivered.
> Please assist on what I am missing. Thank you
Hi,
why don't you use http://wammu.eu/python-gammu/
?
[toc] | [prev] | [next] | [standalone]
| From | mm0fmf <none@mailinator.com> |
|---|---|
| Date | 2015-09-22 17:27 +0100 |
| Message-ID | <YjfMx.19631$v03.18527@fx01.am4> |
| In reply to | #96981 |
On 22/09/2015 12:19, Timon Rhynix wrote:
> Hello, I have used pyserial, sms0.4 and other libraries to send sms via huawei E1750 modem.
> The code runs well and no error is thrown but the text message is not sent/delivered to the number.
> One of my code is as follows:
>
> import serial
> import time
>
> class TextMessage:
> def __init__(self, recipient="0123456789", message="TextMessage.content not set."):
> self.recipient = recipient
> self.content = message
>
> def setRecipient(self, number):
> self.recipient = number
>
> def setContent(self, message):
> self.content = message
>
> def connectPhone(self):
> conn = 'COM13'
> self.ser = serial.Serial(conn, 460800, timeout=5)
> time.sleep(1)
>
> def sendMessage(self):
> self.ser.write('ATZ\r')
> time.sleep(1)
> self.ser.write('AT+CMGF=1\r')
> time.sleep(1)
> self.ser.write('''AT+CMGS="''' + self.recipient + '''"\r''')
> time.sleep(1)
> self.ser.write(self.content + "\r")
> time.sleep(1)
> self.ser.write(chr(26))
> time.sleep(1)
> print "message sent!"
>
> def disconnectPhone(self):
> self.ser.close()
>
> When run it, the "message sent!" is printed but no message is sent/delivered.
> Please assist on what I am missing. Thank you
>
If it's like the GSM modem I used then you should replace those "\r"
strings with "\r\n".
[toc] | [prev] | [next] | [standalone]
| From | Cameron Simpson <cs@zip.com.au> |
|---|---|
| Date | 2015-09-23 09:20 +1000 |
| Message-ID | <mailman.83.1442965855.28679.python-list@python.org> |
| In reply to | #96981 |
On 22Sep2015 04:19, Timon Rhynix <timgeek951@gmail.com> wrote:
>Hello, I have used pyserial, sms0.4 and other libraries to send sms via huawei E1750 modem.
>The code runs well and no error is thrown but the text message is not sent/delivered to the number.
>One of my code is as follows:
>
>import serial
>import time
>
>class TextMessage:
> def __init__(self, recipient="0123456789", message="TextMessage.content not set."):
> self.recipient = recipient
> self.content = message
>
> def setRecipient(self, number):
> self.recipient = number
>
> def setContent(self, message):
> self.content = message
>
> def connectPhone(self):
> conn = 'COM13'
> self.ser = serial.Serial(conn, 460800, timeout=5)
> time.sleep(1)
>
> def sendMessage(self):
> self.ser.write('ATZ\r')
> time.sleep(1)
> self.ser.write('AT+CMGF=1\r')
> time.sleep(1)
> self.ser.write('''AT+CMGS="''' + self.recipient + '''"\r''')
> time.sleep(1)
> self.ser.write(self.content + "\r")
> time.sleep(1)
> self.ser.write(chr(26))
> time.sleep(1)
> print "message sent!"
>
> def disconnectPhone(self):
> self.ser.close()
>
>When run it, the "message sent!" is printed but no message is sent/delivered.
>Please assist on what I am missing. Thank you
Two suggestions:
as already suggested, you may need to send \r\n instead of just \r
do you need to follow every .write() with a .flush()? if the Serial object is
a buffered stream that will be necessary
Cheers,
Cameron Simpson <cs@zip.com.au>
Motorcycling is indeed a delightful pastime. - Honda Rider Training Film
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2015-09-22 22:25 -0600 |
| Message-ID | <mailman.88.1442982343.28679.python-list@python.org> |
| In reply to | #96981 |
On 09/22/2015 05:19 AM, Timon Rhynix wrote:
> When run it, the "message sent!" is printed but no message is sent/delivered.
> Please assist on what I am missing. Thank you
Is this "message sent!" from your code or is it a message you get back
on the serial port from the gsm modem? Oh nevermind I see that it comes
from your own code.
Just an observation, but your code looks quite Java-like. Seems to me
that wrapping this up in a class is not necessary, since the goal is to
provide an interface to send a text message, I'd just use a plain
function, stored in its own module so it can also store things like the
name of the serial port, timeout values, etc. There's no need to make
the caller save any state.
Consider something like this with no error checking when using the
serial port, no context managers for the serial device:
file sms.py:
----------------------
import serial
import time
serial_port = 'COM13'
timeout = 5
baud = 460800
def send_message(recipient, message):
ser = serial.Serial(serial_port, baud, timeout=timeout)
time.sleep(1)
self.ser.write('ATZ\r')
time.sleep(1)
self.ser.write('AT+CMGF=1\r')
time.sleep(1)
self.ser.write('''AT+CMGS="''' + self.recipient + '''"\r\n''')
time.sleep(1)
self.ser.write(self.content + "\r\n")
time.sleep(1)
self.ser.write(chr(26))
time.sleep(1)
print "message sent!"
ser.close()
------------------------
Someone can just do:
import sms
sms.serial_port = "/dev/ttyUSB2"
sms.send_message("5555555","Hi there")
Call it good. Unlike Java, Python does not require everything to be
wrapped up in a class. Particularly if what you really want is a
singleton. In many respects you can treat a python module (a python
file) as a singleton. You can use its namespace to store some state or
basic config information, and define the functions you want others to
use. Private helper functions can start with an underscore, telling
others not to use them.
[toc] | [prev] | [next] | [standalone]
| From | Pavel S <pavel@schon.cz> |
|---|---|
| Date | 2015-09-23 01:46 -0700 |
| Message-ID | <60b3f59b-bf9e-4e77-ae78-67c3c54962d4@googlegroups.com> |
| In reply to | #96981 |
I don't understand why all of you are telling him about '\r\n\, write(),..' instead of recommending to use take library which already has all problems resolved (python-gammu / wammu). When one will write custom templating stuff, you would also recommend him to take jinja.
[toc] | [prev] | [next] | [standalone]
| From | Cameron Simpson <cs@zip.com.au> |
|---|---|
| Date | 2015-09-23 19:30 +1000 |
| Message-ID | <mailman.96.1443001899.28679.python-list@python.org> |
| In reply to | #97024 |
On 23Sep2015 01:46, Pavel S <pavel@schon.cz> wrote: >I don't understand why all of you are telling him about '\r\n\, write(),..' instead of recommending to use take library which already has all problems resolved (python-gammu / wammu). Sometimes it is useful to know how things are done instead of which big dumb button to push. Besides, he came here saying: I'm doing this, what am I doing wrong? We're trying to help him with this. It is not always about the final destination you know. Cheers, Cameron Simpson <cs@zip.com.au>
[toc] | [prev] | [next] | [standalone]
| From | Laura Creighton <lac@openend.se> |
|---|---|
| Date | 2015-09-23 11:30 +0200 |
| Message-ID | <mailman.95.1443000641.28679.python-list@python.org> |
| In reply to | #96981 |
In a message of Tue, 22 Sep 2015 22:25:31 -0600, Michael Torrie writes:
<snip>
>Consider something like this with no error checking when using the
>serial port, no context managers for the serial device:
>file sms.py:
>----------------------
>import serial
>import time
>
>serial_port = 'COM13'
>timeout = 5
>baud = 460800
>
>def send_message(recipient, message):
> ser = serial.Serial(serial_port, baud, timeout=timeout)
> time.sleep(1)
> self.ser.write('ATZ\r')
> time.sleep(1)
> self.ser.write('AT+CMGF=1\r')
> time.sleep(1)
> self.ser.write('''AT+CMGS="''' + self.recipient + '''"\r\n''')
> time.sleep(1)
> self.ser.write(self.content + "\r\n")
> time.sleep(1)
> self.ser.write(chr(26))
> time.sleep(1)
> print "message sent!"
>
> ser.close()
2 questions.
Why all the sleep(1)s?
and don't you need to flush the thing after each write?
Laura
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web