C
C
chronius2019-08-04 22:08:53
Python
chronius, 2019-08-04 22:08:53

How to receive a string by serial and send it to the chat?

How to accept a string from the serial port ending with /n/r and send it to telegram by chat_id? That is, to make such a kind of gateway between serail port and telegram.
With sending from the chatbot to serial, everything is simple: ser.write(message.text.encode() + b'\n\r')
But how do you wait for a string all the time and then send it to the chat?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pfg21, 2019-08-04
@pfg21

same as standard. you read characters from the input stream into the cache and, when /r/n arrives, you flush the cache as a message.

S
Sergey c0re, 2019-08-04
@erge

Yandex: python +"serial port" listen
Google: python +"serial port" listen
Setting up Python to work with the serial port
https://pastebin.com/CswaUL9R
Python serial.Serial() Examples
read data from the port in a loop, as it was thought, send for processing.
and so on.

import serial
ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
while True:
    reading = ser.readline()
    print(reading)

while True:
                data = ser.read() #чтение данных с последовательного порта
                if data != '':
                    raw += data #формирование сообщения
                else:
                    if raw != '':
                        doprocessing(raw) # передаем сообщение на обработку
                        raw = ''

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question