【发布时间】: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