Question
How to talk to SIM800 module using python serial byteArray data type?
Answer
(1) Try my first demo program to clarify between the two python data types:
(a) string,
(b) byteArray,
(2) Try my fully debugged, plug and play, newbie friendly, sample code attached python (StackExchange recommended MCVE - Minimal, Complete, Verifiable, Example) serial loopback program, to make sure your serial hardware and software are working.
(3) Now test SIM800.
References
(1) Obtaining GPS & Internet together with Waveshare GSM/GPRS/GNSS HAT module
Obtaining GPS & Internet together with Waveshare GSM/GPRS/GNSS HAT module
(2) How to connect SIM800 GSM ADD-ON to RaspberryPi 3
How to connect SIM800 GSM ADD-ON to RaspberryPi 3
(3) Raspberry PI Sim800 GSM Add-on V2.0 - $35 2019
https://www.itead.cc/raspberry-pi-sim800-gsm-gprs-add-on-v2-0.html
(4) SIM800 AT Command Manual V1.05 - 2014
https://www.itead.cc/wiki/images/6/6f/SIM800_Series_AT_Command_Manual_V1.05.pdf
(5) Rpi Talking Serial To Arduino
Serial to Arduino totally non-responsive
(6) Rpi talking Serial to PH meter
Calibrate PH-4502C pH meter
My demo program can be used to talk serial to Arduino, Win10 (Using serial terminal emulator RealTerm, set at 9,600, 8N1), BlueTooth serial module, phMeter, actually ALL guys using the "AT", "OK" handshaking/protocol. Once you find the other side gives you five, then you check out the device's AT command set to talk more advanced stuff.
One thing to pay attention is the use of newline/return character/byte '\r\n' at the end of the bytearray, for example the 'AT' byteArray command
b'AT\r\n'
Once you have struggled jumping over this mental block, then the rest is a piece of cake.
Appendices
Appendix A The demo program v0.6
# bytearray_test_06_2019sep0501.py stringtest06 Rpi4B buster 2019jul10 python 3.7.3 tlfong01 2019sep05hkt1607
# *** Testing string ***
testString1 = 'abc'
print('testString1 =', testString1)
testCharArray2 = ['x', 'y', 'z']
print('testStringArray2 =', testCharArray2)
testString3 = testCharArray2[0] + testCharArray2[1] + testCharArray2[2]
print('testString3 =', testString3)
# *** Testing bytearray ***
testByteArray4 = b'AT\r\n'
print('testByteArray4 =', testByteArray4)
testString5 = testByteArray4.decode('utf-8')
print('testString5 =', testString5)
''' Sample Output 2019sep05kt1652
>>> %Run python_bytearray_06_2019sep0501.py
testString1 = abc
testStringArray2 = ['x', 'y', 'z']
testString3 = xyz
testByteArray4 = b'AT\r\n'
testString5 = AT
>>>
'''
################################################################
################################################################
################################################################
'''
# *** Testing Serial Send/Receive Loopback/Echo ***
https://raspberrypi.stackexchange.com/questions/96653/calibrate-ph-4502c-ph-meter/96654#96654
# uart_test06 tlfong01 2019apr08hkt1603 ***
# Computer = Rpi3B+
# Linux = $ hostnamectl = raspberrypi Raspbian GNU/Linux 9 (stretch) Linux 4.14.34-v7+ arm
# Python = >>> sys.version = 3.5.3 Jan 19 2017
# Test 1 - repeatWriteBytes() - UART port repeatedly send out bytes.
# Function - Repeat many times sending bytes, pause after each bytes.
# Test 2 - loopBackTest() - UART port send and receive bytes.
# Function - Send one bytes to TX, wait some time (Note 1), then read bytes back from RX.
# Setup - Connet Tx pin to Rx pin to form a loop.
# Note 1
# Bolutek BlueTooth BC04 needs at least 10mS to respond
from time import sleep
import serial
serialPort0 = serial.Serial(port = '/dev/serial0',
baudrate = 9600,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout= 1)
def setSerialPortBaudRate(serialPort, baudrate):
serialPort.baudrate = baudrate
return
def serialPortWriteBytes(serialPort, writeBytes):
serialPort.write(writeBytes)
return
def serialPortReadBytes(serialPort, maxBytesLength):
readBytes = serialPort.read(maxBytesLength)
return readBytes
def serialPortWriteWaitReadBytes(serialPort, writeBytes, maxBytesLength, waitTime):
serialPort.flushInput()
serialPort.flushOutput()
serialPort.write(writeBytes)
sleep(waitTime)
readBytes = serialPortReadBytes(serialPort, maxBytesLength)
print(' bytes written = ', writeBytes)
print(' bytes read = ', readBytes)
return readBytes
def repeatWriteBytes(serialPort, writeBytes, pauseTimeBetweenBytes, repeatCount):
print(' Begin repeatWriteOneByte(), ...')
for i in range(repeatCount):
serialPortWriteBytes(serialPort, writeBytes)
sleep(pauseTimeBetweenBytes)
print(' End repeatWriteOneByte().')
return
def serialPortLoopBack(serialPort, writeBytes, maxBytesLength, waitTime):
print(' Begin serialPortLoopBack() [Remember to connect Tx to Rx!] , ...')
serialPortWriteWaitReadBytes(serialPort, writeBytes, maxBytesLength, waitTime)
print(' End serialPortLoopBack(), ...')
return
setSerialPortBaudRate(serialPort0, 9600)
#repeatWriteBytes(serialPort0, b'AT\r\n', 0.01, 200000000)
serialPortLoopBack(serialPort0, b'AT\r\n', 32, 0.030)
'''
''' Sample output tlfong01 2019apr0801
>>>
=== RESTART: /home/pi/Python_Programs/test1193/uart_test02_2019apr0801.py ===
Begin serialPortLoopBack() [Remember to connect Tx to Rx!] , ...
bytes written = b'AT\r\n'
bytes read = b'AT\r\n'
End serialPortLoopBack(), ...
>>>
'''
# End
[0:4]
then onlyRI
printed on the screen.