【问题标题】:Openframeworks, reading serial data from ArduinoOpenframeworks,从 Arduino 读取串行数据
【发布时间】:2015-04-25 05:30:19
【问题描述】:

我正在尝试使用 ofSerialobject 从 Arduino UNO 读取串行数据并将其分配为 int。 我可以读取单个字节,但是,我在 openframeworks 控制台中接收到的值与我在 Arduino 串行监视器中读取的值不同。

我提供了各个控制台的屏幕截图:

我的 Arduino 代码只是 Arduino IDE 提供的基本“AnalogReadSerial”示例。

// the setup routine runs once when you press reset:
void setup() {
    // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
    // read the input on analog pin 0:
    int sensorValue = analogRead(A0);
    // print out the value you read:
    Serial.println(sensorValue);
    delay(1);        // delay in between reads for stability
}

而我的 C++ 代码主要是从 ofSerial readByte 函数的文档中复制而来的。

void serialReader::setup()
{
    serial.listDevices();
    vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();

    serial.setup("COM4", 9600); //open the first device and talk to it at 9600 baud
}


void serialReader::printByteToConsole()
{
    int myByte = 0;
    myByte = serial.readByte();
    if ( myByte == OF_SERIAL_NO_DATA )
        printf("\nno data was read");
    else if ( myByte == OF_SERIAL_ERROR )
        printf("\nan error occurred");
    else
        printf("\nmyByte is %d ", myByte);
}

如果您能深入了解可能导致读数之间存在这种差异的原因,我们将不胜感激。谢谢。

【问题讨论】:

    标签: c++ serialization serial-port arduino openframeworks


    【解决方案1】:

    Arduino 的Serial.println 将原始字节转换为它们的 ASCII 等价物,然后发送这些字节,然后是换行符 (10) 和回车符 (13) 字节。因此,原始字节 12 以 4 个总字节的形式发送 - 两个字节代表 ASCII 1 (49)、2 (50),然后是 (10) 和 (13) 用于换行符。因此,由于 openFrameworks 不会自动将 ASCII 值转换回原始字节,因此您看到的是 ASCII 版本。 Arduino 控制台将 ASCII 版本显示为可读文本。

    您可以在此处查看 ASCII 和原始字节(十进制/又名 DEC)之间的转换:

    http://www.asciitable.com/

    如果您希望这两个数字两边都匹配,请考虑在 Arduino 中使用Serial.write 来写入不带 ASCII 转换和换行符的原始字节。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多