-1

I am trying to implement a system using Raspberry-pi and SIM-800 modules. Now I can see incoming calls from my python script, but it can not parse data.

import serial

serialport = serial.Serial("/dev/ttyS0", 115200, timeout=1)

while True:
    command = serialport.read(10)
    print command[0:6]
    if str(command[0:6]) == "RING":
        print "Ringing"

In console, it prints RING when an incoming call, but it doesnt go inside if loop.

Is it because of some extra characters added to this RING value?

Thanks in advance.

  • 2
    Isn't [0:6] six characters? RING is four characters. Nothing to do with the Pi. – joan Sep 05 '19 at 09:07
  • @joan if I use [0:4] then only RI printed on the screen. – Sachith Muhandiram Sep 05 '19 at 09:10
  • 2
    Then there are non-printable characters in the string. Still not a Pi problem. – joan Sep 05 '19 at 09:11
  • @joan so is there a way to solve the problem? – Sachith Muhandiram Sep 05 '19 at 09:14
  • Of course, perhaps have a look at a Python tutorial on strings. Find out the value of each character which will then help you understand the format of the data being received. – joan Sep 05 '19 at 09:17
  • @Sachith, SIM800 talks python byteArrays to Rpi, not strings. I once used Arduino C++ style string and made a big mess. Python3 is bytArray data type friendly. Let me first show you the string way, then byteArray way, then a plug and play demo program doing loopback 'AT' to 'AT. If you find the demo program OK. then remove the loopback wiring connection and connect it to the real thing, SIM800. You should hopefully send 'AT' and get back 'OK'. Good luck and Cheers. :) – tlfong01 Sep 05 '19 at 09:23
  • If [0:4] prints RI, then you could try using [3:6] for the comparison instead. That should give you theRING without the hidden characters. Alternatively, apply the string.strip() function via command = command.strip() before the if statement. Also @joan is correct, this doesn't look like a Pi specific problem. – Fred Sep 20 '19 at 22:49

1 Answers1

2

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 
tlfong01
  • 4,665
  • 3
  • 10
  • 24
  • 2
    I forgot to mention (1) timing - you might need to wait for characters to slowly coming up, (2) flushing buffer - you need to empty buffer, otherwise rubbish would mix with the good data, (3) Version 06 is a stable version - you can search this forum for older versions. (4) I forgot if SIM800 also talk SPI - I do have similar SPI loopback programs verified by other newbies. Again search this forum, (5) My demo program is tested with Rpi3B+ IDLE Python 3.5.3. (I need to test again using Rpi4B buster 2019jul10 Thonny. Reminder: Need sudo if run in terminal mode, Thonny is auto sudo mode. – tlfong01 Sep 05 '19 at 10:04
  • Thanks for the detailed answer, but its not solving my problem, I tried command.decode(utf-8), but its not working. – Sachith Muhandiram Sep 05 '19 at 10:06
  • 2
    Really? I forgot to mention that I verified the string/bytearry part using Python 3.7.3 Thonny. It is supper time. So let me try again late evening or tomorrow. – tlfong01 Sep 05 '19 at 10:09
  • Hm, I have tried to make it work on python3, but no luck. My ring command looks like this b'\r\nRING\r\n' – Sachith Muhandiram Sep 05 '19 at 10:54
  • 2
    Did sim800 gave you five? – tlfong01 Sep 05 '19 at 13:05
  • sorry, I didnt get you. – Sachith Muhandiram Sep 05 '19 at 13:07
  • I mean have you tested " serial loopback" - send 'ABC' and receive 'ABC'? Then you can test SIM800 send 'AT' and get back 'OK', or 'RDY'. – tlfong01 Sep 05 '19 at 14:08
  • I have added SIM800 AT Command Manual in the references section. Please let me know which command on which page you are testing. – tlfong01 Sep 05 '19 at 14:10