Writing string in python serial port on raspberrypi

I am implementing a SIM900 interface in RPi using python language. However, there is a problem with sending a string in the port.write () function.

Here is my code:

# This line has to write on serial port port.write(b'AT+SAPBR=3,1,"APN","TATA.DOCOMO.INTERNET"\r\n') 

The above line works well ...

Now my requirement is the TATA.DOCOMO.INTERNET line, which should be read from the command line, and I need to add this line using b'AT+SAPBR=3,1,"APN","xxxx"\r\n' .

But when I try to read the APN name in a variable and after adding it does not work, the lines of code

 # AP ="TATA.DOCOMO.INTERNET"-string read from serial input AP = gsm.read() t1 = "AT+SAPBR=3,1," t2 = '"APN",' t3 = AP t4 = '\r\n' t = t1 + t2 + t3 + t4 

Its output in python is as follows:

 AT+SAPBR=3,1,"APN","TATA.DOCOMO.INTERNET"\r\n' 

It looks like the line I need, but when I write in gsm.write (t) it gives an error, I even tried using

 gsm.write(byte(t)) gsm.write(t.encode()) 

But that still doesn't work.

+5
source share
1 answer

You can try using this code with the os module, which is the standard in Python:

 import os os.system(b'echo -e "AT+SAPBR=3,1,\"APN\",\"TATA.DOCOMO.INTERNET\"\r\n" >> ttyAMA0') 

Best wishes

0
source

Source: https://habr.com/ru/post/1238225/


All Articles