2016-07-22 20 views
0

Ich versuche, Daten über die serielle Verbindung (USB) von meinem PC (Ubuntu 14.04) zu einem Arduino Uno zu senden. Der Arduino sollte die empfangenen Daten zu Testzwecken anzeigen. (Ich bin glücklich, wenn ich etwas bekomme ...)C++ libserial serielle Verbindung zu Arduino

Ich benutze libserial, um die Daten zu senden, aber das Arduino erhält nichts. Mit Hilfe der Arduino IDE konnte ich die Daten erfolgreich an den Arduino senden. Mit normalen Konsolenbefehlen ist es auch möglich, die Daten zu senden.

Hier ist mein Arduino Code:

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

Und dies der C++ Code (auf der PC-Seite):

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

Haben Sie irgendwelche Ideen, warum dies nicht funktioniert?

Danke!

+0

Hat der PC die Daten senden? Schließen Sie es an einen anderen PC oder einen anderen seriellen Anschluss am selben PC an und verwenden Sie minicom o.ä., um zu sehen, ob der PC-Code tatsächlich Daten sendet. Dann interpretieren Sie die Daten, um sicherzustellen, dass Sie gesendet haben, was Sie senden wollten. Es ist auch oft hilfreich, eine Portierungsschicht zu schreiben, die die Arduino- und PC-Linux-Inhalte abstrahiert, so dass Sie Ihre Anwendungslogik auf einem PC oder etwas einfacher zu Debuggen als ein Arduino testen können. Wenn Sie wissen, dass die Logik funktioniert, starten Sie auf dem Arduino. – user4581301

+0

Danke! Mit Hilfe von Minicom habe ich das Problem gefunden .... Ich werde die "Lösung" posten. –

Antwort

0

Ich fand eine Lösung, wie mit dem Arduino über die serielle Schnittstelle (USB) zu kommunizieren. Ich benutze libserial nicht. (Nur Anzeige, wenn es eine neue Linie ist)

ich verbessern den Arduino-Code:

// include the library code: 
#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); 

    if (stringComplete) { 
    // print the string when a newline arrives: 

    lcd.setCursor(0, 1); 
    lcd.print("    "); 

    lcd.setCursor(0, 1); 
    lcd.print(inputString); 

    inputString = ""; 
    stringComplete = false; 

    } 
} 

void serialEvent() { 

    if (Serial.available()) { 
    while (Serial.available()) { 
    // get the new byte: 
    char inChar = (char)Serial.read(); 
    // add it to the inputString: 
    inputString += inChar; 
    // if the incoming character is a newline, set a flag 
    // so the main loop can do something about it: 
    if (inChar == '\n') { 
     stringComplete = true; 
    } 
    } 
    } 
} 

C++ Code:

//to compile use: g++ serial_success.cpp -o serial -std=c++11 

//you might not need every inclusion 
#include <iostream> 
#include <unistd.h> 
#include <string> 

#include <stdio.h> // standard input/output functions 
#include <string.h> // string function definitions 
#include <unistd.h> // UNIX standard function definitions 
#include <fcntl.h> // File control definitions 
#include <errno.h> // Error number definitions 
#include <termios.h> // POSIX terminal control definitionss 
#include <time.h> // time calls 

using namespace std; 

#define BAUDRATE B9600 


int main(int argc, char** argv) 
{ 

    int fileDescriptor = open("/dev/ttyACM0", O_RDWR | O_NOCTTY); 

    struct termios newtio; 
    bzero(&newtio, sizeof(newtio)); 
    newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD; 

    // set to 8N1 
    newtio.c_cflag &= ~PARENB; 
    newtio.c_cflag &= ~CSTOPB; 
    newtio.c_cflag &= ~CSIZE; 
    newtio.c_cflag |= CS8; 

    newtio.c_iflag = IGNPAR; 

    // output mode to 
    //newtio.c_oflag = 0; 
    newtio.c_oflag |= OPOST; 

    /* set input mode (non-canonical, no echo,...) */ 
    newtio.c_lflag = 0; 

    newtio.c_cc[VTIME] = 10; /* inter-character timer 1 sec */ 
    newtio.c_cc[VMIN] = 0; /* blocking read disabled */ 

    tcflush(fileDescriptor, TCIFLUSH); 
    if (tcsetattr(fileDescriptor, TCSANOW, &newtio)) { 
     perror("could not set the serial settings!"); 
     return -99; 
    } 

    int i = 0; 
    string test = ">123,456,7890;"; 

    while(i < 10) { 

     usleep(100000); 

     string res = test + std::to_string(i) + "\n"; 
     long wrote = write(fileDescriptor, res.c_str(), sizeof(char)*res.size()); 

     cout << res << endl; 


     i++; 
    } 


    cout << "ready" << endl; 

    return 0; 
}