Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. from twisted.application import internet, service
  2. from twisted.internet import defer, protocol, reactor, serialport, task, threads
  3. from twisted.protocols import basic
  4. from twisted.python import log
  5.  
  6. from collections import deque
  7.  
  8. ## SerialModemProtocol ##
  9.  
  10. class SerialModemProtocol(basic.LineOnlyReceiver):
  11.  
  12.     initSequence = [
  13.         'ATE0', # command echo disabled
  14.         'AT',
  15.         'AT#BND=0', # 900/1800
  16.         'AT#AUTOBND=2', # quad-band
  17.         'AT+CMGF=1', # text mode
  18.         'AT+CMEE=2', # verbose debug
  19.     ]
  20.  
  21.     def __init__(self):
  22.         self.outgoingQueue = deque(self.initSequence)
  23.  
  24.     def connectionMade(self):
  25.         print 'conn made'
  26.         self.init_modem()
  27.  
  28.     def connectionLost(self, reason):
  29.         print 'conn lost: %s' % (reason,)
  30.  
  31.     def lineReceived(self, line):
  32.         if 'OK' in line:
  33.             self._sendNext()
  34.  
  35.     def send_command(self, cmd):
  36.         pass
  37.         # self.transport.write or self.sendLine, I think
  38.  
  39.     def _sendNext(self):
  40.         if self.outgoingQueue:
  41.             self.send_command(self.outgoingQueue.popleft())
  42.  
  43.     def init_modem(self):
  44.         self._sendNext()
  45.  
  46.  
  47. ## SerialModemProtocol ##
  48.  
  49. serialport.SerialPort(SerialModemProtocol(), '/dev/ttyUSB1', reactor,
  50.     baudrate=115200, timeout=2)
  51.  
  52. reactor.callLater(10, reactor.stop)
  53. reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement