简体   繁体   中英

Beaglebone Black Serial c++

It seems to be a pain to get serial working in c++, adding to that, to do it on the Beaglebone Black is hard, so I need someone with some expertise!

I created /dev/ttyO4 with the following command:

echo BB-UART4 > /sys/devices/bone_capemgr.9/slots

This gives me /dev/ttyO4. I then write a small program in cpp using a library linked here . My code is found below:

#include <stdio.h>
#include "serialib.h"


#if defined (_WIN32) || defined( _WIN64)
#define         DEVICE_PORT             "COM1"                               // COM1 for windows
#endif

#ifdef __linux__
#define         DEVICE_PORT             "/dev/ttyO4"                         // ttyS0 for linux
#endif


int main()
{
    serialib LS;                                                            // Object of the serialib class
    int Ret;                                                                // Used for return values
    char Buffer[128];

    // Open serial port

    Ret=LS.Open(DEVICE_PORT,115200);                                        // Open serial link at 115200 bauds
    if (Ret!=1) {                                                           // If an error occured...
        printf ("Error while opening port. Permission problem ?\n");        // ... display a message ...
        return Ret;                                                         // ... quit the application
    }
    printf ("Serial port opened successfully !\n");

    // Write the AT command on the serial port

    Ret=LS.WriteString("AT\n");                                             // Send the command on the serial port
    if (Ret!=1) {                                                           // If the writting operation failed ...
        printf ("Error while writing data\n");                              // ... display a message ...
        return Ret;                                                         // ... quit the application.
    }
    printf ("Write operation is successful \n");

    // Read a string from the serial device
    Ret=LS.ReadString(Buffer,'\n',128,5000);                                // Read a maximum of 128 characters with a timeout of 5 seconds
                                                                        // The final character of the string must be a line feed ('\n')
    if (Ret>0)                                                              // If a string has been read from, print the string
        printf ("String read from serial port : %s",Buffer);
    else
        printf ("TimeOut reached. No data received !\n");                   // If not, print a message.

    // Close the connection with the device

    LS.Close();

    return 0;
}

When I run the code, it says it opened the port successfully and successfully wrote serially, but I receive no data on the RX. I have connected the RX pin to the TX pin for UART4 (P9.11 and P9.13). I also connected the RX and TX pins for UART5 just incase (P8.37 and P8.38), as the ttyO4 confused me a bit as to which UART I'm using.

Is there something I'm missing to get the serial port working? Or can someone refer me to a working example for serial communication with c++ on the beaglebone black, perhaps like a step-by-step guide?

Regards, Cornel

EDIT:

The Boost serial libraries work more reliably than serialib, find them here .

So I tried the same steps as you did. I downloaded and compiled the code from http://serialib.free.fr/html/classserialib.html and compiled with a warning about

serialib.cpp: 337:40: warning: converting to non-pointer type 'unsigned int' from NULL [-Wconversion-null]

(If you know how to fix this please let me know). But I ran the program with the Tx/Rx pins hooked up to an Arduino Mega Rx1/Tx1 pins. The program writes fine and I can see the Arduino read in the bytes. When I write from the Arduino to the Rx of the Beaglebone Black it times out and says no data was received.

As far as I can tell, this is a problem with how the Rx pin is set.

EDIT: So I just printed the Buffer that receives the data written to the port and it receives the data just fine. So the problem is with the value of Ret . For now you can use the buffer as the received data. I'll try and figure out why the return value from ReadString() isn't working.

to correct the [-Wconversion-null] error: Open 'serialib.cpp' file and go to the 'ReadStringNoTimeOut' function.

Change

ret=ReadChar(&String[NbBytes]);

to

ret=ReadChar(&String[NbBytes],0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM