faucamp / python-gsmmodem

Python module to control a GSM modem attached to the system: send/receive SMS messages, handle calls, etc
GNU Lesser General Public License v3.0
385 stars 303 forks source link

TypeError: unicode strings are not supported, please encode to bytes: 'ATZ\r' #108

Closed agn-7 closed 3 years ago

agn-7 commented 3 years ago

A problem in USSD example as follows:

TypeError                                 Traceback (most recent call last)
<ipython-input-1-5359af7ce02c> in <module>
     26 
     27 if __name__ == '__main__':
---> 28     main()
     29 

<ipython-input-1-5359af7ce02c> in main()
     12     #logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
     13     modem = GsmModem(PORT, BAUDRATE)
---> 14     modem.connect(PIN)
     15     modem.waitForNetworkCoverage(10)
     16     print('Sending USSD string: {0}'.format(USSD_STRING))

/usr/local/lib/python3.8/dist-packages/gsmmodem/modem.py in connect(self, pin)
    175         # Send some initialization commands to the modem
    176         try:
--> 177             self.write('ATZ') # reset configuration
    178         except CommandError:
    179             # Some modems require a SIM PIN at this stage already; unlock it now

/usr/local/lib/python3.8/dist-packages/gsmmodem/modem.py in write(self, data, waitForResponse, timeout, parseError, writeTerm, expectedResponseTermSeq)
    396         """
    397         self.log.debug('write: %s', data)
--> 398         responseLines = SerialComms.write(self, data + writeTerm, waitForResponse=waitForResponse, timeout=timeout, expectedResponseTermSeq=expectedResponseTermSeq)
    399         if self._writeWait > 0: # Sleep a bit if required (some older modems suffer under load)
    400             time.sleep(self._writeWait)

/usr/local/lib/python3.8/dist-packages/gsmmodem/serial_comms.py in write(self, data, waitForResponse, timeout, expectedResponseTermSeq)
    125                 self._response = []
    126                 self._responseEvent = threading.Event()
--> 127                 self.serial.write(data)
    128                 if self._responseEvent.wait(timeout):
    129                     self._responseEvent = None

/usr/local/lib/python3.8/dist-packages/serial/serialposix.py in write(self, data)
    530         if not self.is_open:
    531             raise portNotOpenError
--> 532         d = to_bytes(data)
    533         tx_len = length = len(d)
    534         timeout = Timeout(self._write_timeout)

/usr/local/lib/python3.8/dist-packages/serial/serialutil.py in to_bytes(seq)
     61         return seq.tobytes()
     62     elif isinstance(seq, unicode):
---> 63         raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq))
     64     else:
     65         # handle list of integers and bytes (one or more items) for Python 2 and 3

TypeError: unicode strings are not supported, please encode to bytes: 'ATZ\r'
lrlrlrlr commented 3 years ago

Hi I got the same problem. You should switch your python3 to python2.7.