0
我試圖通過串行連接(USB)從我的PC(Ubuntu 14.04)發送數據到Arduino Uno。 Arduino應該顯示接收到的數據用於測試目的。 (我很高興,如果我收到什麼......)C++與Arduino的libserial串行連接
我使用libserial發送數據,但Arduino沒有收到任何東西。在Arduino IDE的幫助下,我可以成功地將數據發送給Arduino。使用正常的控制檯命令也可以發送數據。
這裏是我的Arduino代碼:
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("connecting...");
inputString.reserve(200);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
lcd.setCursor(0, 0);
lcd.print("successful connected");
}
void loop() {
lcd.setCursor(0, 1);
// print the string when a newline arrives:
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(inputString);
delay(500);
}
void serialEvent() {
if (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
}
}
而這個C++代碼(PC端):
//Libserial: sudo apt-get install libserial-dev
#include <SerialStream.h>
#include <iostream>
#include <unistd.h>
using namespace LibSerial;
using namespace std;
int main(int argc, char** argv)
{
SerialStream my_serial_stream;
//
// Open the serial port for communication.
//
my_serial_stream.Open("/dev/ttyACM0");
my_serial_stream.SetBaudRate(SerialStreamBuf::BAUD_9600);
//my_serial_stream.SetVTime(1);
//my_serial_stream.SetVMin(0);
my_serial_stream.SetCharSize(SerialStreamBuf::CHAR_SIZE_8);
my_serial_stream.SetParity(SerialStreamBuf::PARITY_NONE);
my_serial_stream.SetFlowControl(SerialStreamBuf::FLOW_CONTROL_NONE);
my_serial_stream.SetNumOfStopBits(1);
int i = 0;
while(i<=5) {
usleep(1500000);
if (!my_serial_stream.good()) {
my_serial_stream << i << "\n" << endl;
cout << i << endl;
}
else {
cout << "serial is not good" << endl;
}
i++;
}
my_serial_stream.Close();
cout << "ready" << endl;
return 0;
}
你有什麼想法,爲什麼這不起作用?
謝謝!
是否PC發送的數據?將其插入另一臺PC或同一臺PC上的其他串行端口,然後使用minicom或類似軟件查看PC代碼是否確實發送數據。然後解釋數據以確保您發送了您想要發送的內容。編寫一個移植Arduino和PC Linux的移植層也是很有幫助的,這樣你就可以在PC上測試你的應用程序邏輯,或者比Arduino更容易調試。當你知道邏輯有效時,然後開始在Arduino上運行。 – user4581301
謝謝!在minicom的幫助下,我發現問題....我將發佈「解決方案」。 –