簡體   English   中英

Openframeworks,從Arduino讀取串行數據

[英]Openframeworks, reading serial data from Arduino

我正在嘗試使用ofSerial對象從Arduino UNO讀取串行數據並將其指定為int 我能夠讀取單個字節,但是,我在openframeworks控制台中收到的值與我在Arduino串行監視器中讀取的值不同。

我提供了相應控制台的屏幕截圖:

Arduino串行監視器

openframeworks控制台

我的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);
}

任何洞察可能導致讀數之間差異的內容都將非常感激。 謝謝。

Arduino的Serial.println將原始字節轉換為它們的ASCII等價物,然后發送這些字節,然后是換行符(10)和回車符(13)字節。 因此,原始字節12作為4個總字節發送 - 兩個字節表示新的行字符的ASCII 1 (49), 2 (50)和然后(10)和(13)。 因此,由於openFrameworks不會自動將ASCII值轉換回原始字節,因此您將看到ASCII版本。 Arduino控制台將ASCII版本顯示為可讀文本。

你可以在這里看到ASCII和原始字節(Decimal / aka DEC)之間的轉換:

http://www.asciitable.com/

如果您希望兩個數字在兩側都匹配,請考慮在Arduino中使用Serial.write來寫入沒有ASCII轉換和新行字符的原始字節。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM