Python serial communication

前端 未结 2 1170
走了就别回头了
走了就别回头了 2021-02-15 02:13

I\'m working on an Arduino project, and I am interfacing it with a Python script due to memory limitations. On the Python side I have a 2 dimensional matrix containing respectiv

2条回答
  •  我在风中等你
    2021-02-15 03:11

    I would change the communication so python sends newlines between numbers, so you're not as dependent on the timing:

    s.write(str(25)+'\n')
    

    and then on the receiving side:

    void loop(){
        while (Serial.available() > 0) {
            char d = Serial.read();
            if (d == '\n') {
                char t[str.length()+1];
                str.toCharArray(t, (sizeof(t)));
                int intdata = atoi(t);
                Serial.print(intdata);
                str = String();
            }
            else {
                str.concat(d);
            }
        }
    }
    

提交回复
热议问题