c++ - Openframeworks, reading serial data from Arduino -
i'm trying read serial data arduino uno using ofserial
object , assign int
. able read in individual bytes, however, values i'm receiving in openframeworks console not same values i'm reading in arduino serial monitor.
i have provided screenshots of respective consoles:
my arduino code basic "analogreadserial" example available arduino ide.
// setup routine runs once when press reset: void setup() { // initialize serial communication @ 9600 bits per second: serial.begin(9600); } // loop routine runs on , on again forever: void loop() { // read input on analog pin 0: int sensorvalue = analogread(a0); // print out value read: serial.println(sensorvalue); delay(1); // delay in between reads stability }
whereas c++ code copied documentation ofserial readbyte function.
void serialreader::setup() { serial.listdevices(); vector <ofserialdeviceinfo> devicelist = serial.getdevicelist(); serial.setup("com4", 9600); //open first device , talk @ 9600 baud } void serialreader::printbytetoconsole() { int mybyte = 0; mybyte = serial.readbyte(); if ( mybyte == of_serial_no_data ) printf("\nno data read"); else if ( mybyte == of_serial_error ) printf("\nan error occurred"); else printf("\nmybyte %d ", mybyte); }
any insight may causing disparity between readings appreciated. thank you.
arduino's serial.println
translates raw bytes ascii equivalent , sends bytes followed linefeed (10) , carriage return (13) bytes. so, raw byte 12
sent 4 total bytes -- 2 bytes representing ascii 1
(49), 2
(50) , (10) , (13) new line characters. so, since openframeworks not automatically translate ascii values raw bytes, seeing ascii version. arduino console shows ascii version readable text.
you can see translation between ascii , raw bytes (decimal / aka dec) here:
if want 2 numbers match on both sides, consider using serial.write
in arduino write raw bytes without ascii translation , new line characters.
Comments
Post a Comment