2

I have written Flask web server on my Raspberry PI.

The server receives POST and sends commands to SIM868 module hardware. This is my code I could write.

def send(self):
        if not self.validator():
            data = {'message': 'Validation error'}
            return jsonify(data), 422

        delay = False
        if 'AMA' in self.port:
            delay = True

        gsm = serial.Serial('/dev/tty%s' % self.port, 9600, timeout=1)

        gsm.write('AT+CMGF=1\r')

        gsm.write('AT+CMGS="%s"\r\n' % self.number.encode())

        gsm.write(self.message.encode())
        # gsm.write('\x1a')  # Doesn't help
        # gsm.write(x11x01)  # Doesn't help
        # gsm.write('\r\n')  # Doesn't help
        # gsm.write(ascii.ctrl('z'))  # Doesn't help

        abort_after = 10
        start = time.time()
        output = ""

    while True:

            output = output + gsm.readline()

            print(output)

            if '+CMGS:' in output:
                gsm.close()
                return '{"id":%s,"error":0,"message":"success", "raw":"%s"}' % (id, output)

            if 'ERROR' in output:
                gsm.close()
                return '{"id":%s,"error":0,"message":"fail", "raw":"%s"}' % (id, output)

            delta = time.time() - start
            if delta >= abort_after:
                gsm.close()
                return '{"id":%s,"error":1,"message":"timeout", "raw":"%s"}' % (id, output)

So the problem is: The module can receive the number and the text but cannot send, because didn't receive CTRL+Z command. After that I open the PICOCOM and hit CTRL+Z and right after it my SMS sends.

Everything is fine, but how to really simulate CTRL+Z in Python app working with serial ports?

After the last command sent to the SIM hardware I see OK answer, but should see CMGS+ answer too.

The result I get - timeout (the last elseif case)

Googled multiple times and read multiple documentations in past month :)

1 Answers1

0

You could use the pynput library. Start by installing it with:

pip install pynput

Import it into your project, create a Controller object, and use the .press() and .release() commands to simulate keystrokes. Use Key.cmd as the argument for the control key. Here is a tutorial.

Shono 1
  • 46
  • 6