无法从Arduino接收大量的串行数据

时间:2018-03-14 21:36:07

标签: arduino serial-port processing

我正在尝试从Arduino接收传感器数据,并使用Processing IDE(使用串行通信/ USB)将读数写入文件。

在进行大量测试后,我很确定处理方无法处理数据。 只有前几个(< 100)“样本”成功写入,之后Serial.available()始终返回false。

我从Arduino发送两个字节的块,57600波特,默认设置(8位,无奇偶校验,1个停止位)。

Arduino代码:

unsigned int data = 0;
unsigned char buf[2];

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

void loop() {

  data = analogRead(A0);

  buf[0] = data & 0xFF; // low byte
  buf[1] = data >> 8;   // high byte

  Serial.write(buf, 2);
  Serial.flush();

  delayMicroseconds(300);
}

处理代码:

import processing.serial.*;

Serial serialPort;
String serialData;
PrintWriter output;

int recordingTime = 1000;          // how many miliseconds of data stream to record
byte[] dataBuffer = new byte[2];   // reserve memory for 2 bytes and initialize to 0 (java stuff)
int receivedBytes = 0;

void setup()
{
  serialPort = new Serial(this, "/dev/ttyUSB0", 57600);
  output = createWriter("serialData.txt");
}

void draw()
{

  while(millis() < recordingTime) {

    if (serialPort.available() > 0) {
      receivedBytes = serialPort.readBytes(dataBuffer);

      output.print("\n");
      output.print("Received bytes: ");
      output.print(receivedBytes);
      output.print("\n");

      output.println(binary(dataBuffer[0]));  // low byte
      output.println(binary(dataBuffer[1]));  // high byte
      output.println("");
    }    
    else {
      output.print("\n");
      output.println("No data available");
    }

  }

  output.flush();
  output.close();
  exit();  
}

输出:

Received bytes: 2
11101001
00000011


Received bytes: 2
11101001
00000011


Received bytes: 2
11101001
00000011

...after some lines...

No data available

No data available

No data available

No data available

No data available

No data available

为什么会发生这种情况?为什么几个样本后“没有数据可用”?如果我在Arduino IDE中观看串行监视器输出,它可以正常工作。

3 个答案:

答案 0 :(得分:1)

我可以使用screen和使用Python从Arduino读取串行数据。仍然无法让它在Processing中工作 - 只接收少量样本(精确到17个)。

屏幕命令:$ screen <port name> <baud rate>(按 ctrl + a 然后 shift + k 到停止程序;添加-L标志以记录到文件)。按预期工作。

我设法在Python中使用Pyserial library获得相同的结果:

#
#   log_serial.py
#   Writes incoming serial data to file.

import time
import serial

# Edit this parameters =========================================================

serialPort = "/dev/ttyUSB0"
baudrate = 57600
recordTime = 1000   # milliseconds

# ==============================================================================


def millis():
    """
    Returns current (wall-)time in milliseconds
    """
    return int(round(time.time() * 1000))


ser = serial.Serial(serialPort, baudrate)

with open("output.txt", "w") as f:

    startTime = millis()

    f.write("Recording started at: ")
    f.write(str(startTime))
    f.write("\n")

    while (millis() - startTime) <= recordTime:
        inData = ser.read(2)                                        # reads two bytes
        inInt = int.from_bytes(inData, byteorder='little')          # merges them into an integer

        f.write(str(inInt))
        f.write("\n")

    f.write("Recording finished at: ")
    f.write(str(millis()))
    f.write("\n")

仍然不知道为什么处理版本无法处理它...
我的第一个Processing代码中有一个错误(循环条件错误)。这是更新版本,使用Serial.read()而不是Serial.readBytes(buffer)。仍然没有解决我的问题,只有17个样本:

import processing.serial.*;

Serial serialPort;
String serialData;
PrintWriter output;

int recordingTime = 5000;          // how many miliseconds of data stream to record

void setup()
{
  serialPort = new Serial(this, "/dev/ttyUSB0", 57600);
  output = createWriter("serialData.txt");
}

void draw()
{

  int startTime = millis();
  while((millis() - startTime) <= recordingTime) {

    if (serialPort.available() > 0) {

      int b1 = serialPort.read();
      int b2 = serialPort.read();

      int value = (b2 << 8) + (b1 & 0xFF);
      output.println(value);
    }

  }

  output.flush();
  output.close();
  exit();  
}

答案 1 :(得分:1)

请尝试运行此代码,然后发布书面文件的内容:

import processing.serial.*;

Serial serialPort;
String serialData;
PrintWriter output;

int recordingTime = 5000;          // how many miliseconds of data stream to record

void setup()
{
  serialPort = new Serial(this, "/dev/ttyUSB0", 57600);
  output = createWriter("serialData.txt");
}

void draw()
{

  int startTime = millis();
  while((millis() - startTime) <= recordingTime) {

    if (serialPort.available() > 0) {

      int b1 = serialPort.read();
      int b2 = serialPort.read();

      int value = (b2 << 8) + (b1 & 0xFF);
      output.print(millis());    //to each received value there is written the actual millis()-value
      output.print(",");
      output.println(value);
    }

  }

  output.flush();
  output.close();
  exit();  
}

答案 2 :(得分:0)

使用serialPort.read()代替serialPort.readBytes(dataBuffer),处理和我的Arduino Uno / Mega之间的串行通信没有问题。也许这会有所不同......

您是否已尝试增加变量recordingTime