0

PC (Ubuntu 14.04) から Arduino Uno にシリアル接続 (USB) 経由でデータを送信しようとしています。Arduino は、テスト目的で受信したデータを表示する必要があります。(何か貰えたら嬉しいな…)

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;
}

これがうまくいかない理由はありますか?

ありがとう!

4

1 に答える 1