0

我觉得这可能是 python 方面的问题,因为 arduino m 代码在我使用监视器时有效。

无论如何,我基本上是使用 python 2.7 btw 将来自 csv 文件的字符串数组发送到 arduino。

我的问题是 arduino 停止接收字符串(大约 12 个字符串)。

如果有人能看到任何东西,那可能是导致问题的原因,我将不胜感激。我已经尝试在代码周围使用各种 time.sleep(s),因为我已经阅读了很多东西——在 serial.serial() 之后初始化端口需要一段时间。我什至尝试在发送完所有数据后等待 - 在 python 代码必须读取comport之前(这是我检查的主要方法)。我也一直在使用软件串行 rx rx 引脚连接到单独的 USB 到串行设备(我不依赖它的输出,因为它很便宜)。我还尝试了每个可用的波特率并且没有骰子。

这是python代码:`

import serial
import time
ser = serial.Serial('COM3', 9600, timeout=0)
file = open('C:\\samples.csv')
time.sleep(2)
while 1:
        line = file.readline()
        print line
        if not line:
                break
        ser.write(line)
        #time.sleep(4)
time.sleep(20)        
while 1:
        try:
                print ser.readline()
                time.sleep(1)
        except ser.SerialTimeoutException:
                print('Data could not be read')
                time.sleep(1)`

这是arduino代码-我测试过的链表库,它可以工作:

#include <analogShield.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(18, 19); // RX, TX
#include <LinkedList.h>

unsigned int full = 65536;
unsigned int zero = 32767;

//SoftwareSerial mySerial(18, 19); // RX, TX
LinkedList<String> myLinkedList = LinkedList<String>();
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  mySerial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. 
  }


  //Serial.println("TESTING...");

  //
  delay(20000);
}

void loop() { // run over and over

  if (Serial.available()) {
    String s = Serial.readString();
    myLinkedList.add(s);
    mySerial.println(s);
    //delay(1);

  }
  else {
    Serial.println("THIS IS LIST "  );
    Serial.println(myLinkedList.size());
    for (int i = 0; i<myLinkedList.size();i++) {
      //unsigned int volt = myLinkedList.get(i);
      Serial.println(myLinkedList.get(i));
      //analog.write(0,volt);
      //delay(1);
      //delayMicroseconds(8);

      }

    while (true) {
      //analog.write(0,zero);  
    }
    }
}

`

4

1 回答 1

0

因此,在第 18 个字符串处,arduino 代码停止,因为它进入了 while(true) 循环。也许传输中有一个小刹车,您的代码进入执行终止的 else 部分。我建议在收到一些特殊(字符串)命令后进入程序的第二部分。

总是相同的字符串,到 18560 的一半。

18560个字符串?这不适合巨型 2560 RAM。

于 2017-01-21T21:15:58.273 回答