safe and fast method for communicating a mix list of integers and booleans between pyserial and Arduino

北慕城南 提交于 2020-01-16 09:05:14

问题


I want to send and receive a mix list of integers and booleans between pyserial and an Arduino. I have figured the Arduino to pyserial out, well partly:

Arduino code:

...
bool var1 = ...;
int var2 = ...;
...


void setup() {
   ...
   Serial.begin(9600);
   ...
}

void loop() {
    ...
    // here I send the data in CSV format
    Serial.print(var1);
    Serial.print(", ");
    Serial.print(var2);
    Serial.print(", ");
    ...
    Serial.println(var_n);
    ...
}

and the pyserial side:

import serial

serPort = serial.Serial("COM7")

dataRaw = serPort.readline().strip()
dataList = dataRaw.decode('ascii').split(', ')
data = [int(d) for d in dataList]

serPort.close()

however, the first issue is that python stops at readline. What I want to have is

  1. to be sure serial buffer on both sides does not overflow. basically, if the receiver buffer is full the sender keeps the data in its send buffer till receiver buffer has some vacancy.
  2. I want the readline to be run only if the receive buffer has received a new line. Basically some form of interrupt function, which is triggered when a new line is put in the receive buffer.
  3. I want to read lines complete. so the text between to /n characters (carriage return?) must be read.

Although I'm kinda cheating here and get the boolean information as an integer in python, it does the job for me. However, I have no idea how should I send a similar mixed list from pyserial to Arduino. Consider the python list:

data = [12345, True, 67890, False, ...]

How can I send the list above to the Arduino also in a safe and fast way? I don't think that Arduino has regex or can afford complicated split, strip, ... string operations. What is the best way to send a list as above and then receive it on the Arduino side?

P.S. I did not ask this question on arduino.stackexchange because it is more general in terms of including serial communication and using python.

来源:https://stackoverflow.com/questions/57937668/safe-and-fast-method-for-communicating-a-mix-list-of-integers-and-booleans-betwe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!