Beaglebone Black Serial c ++

时间:2013-08-08 14:35:09

标签: c++ serial-port

使用c ++进行连续编程似乎很痛苦,除此之外,在Beaglebone Black上执行它很难,所以我需要有一些专业知识的人!

我使用以下命令创建了/ dev / ttyO4:

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

这给了我/ dev / ttyO4。然后我使用链接here的库在cpp中编写一个小程序。我的代码如下:

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

当我运行代码时,它表示它已成功打开端口并成功串行写入,但我没有收到RX上的数据。我已将RX引脚连接到UART4的TX引脚(P9.11和P9.13)。我还连接了UART5的RX和TX引脚(P8.37和P8.38),因为ttyO4让我对我正在使用的UART感到困惑。

是否有一些我缺少的东西让串口工作?或者有人可以参考我在beaglebone black上与c ++进行串行通信的工作示例,或许像是一步一步的指南?

此致 茱萸

修改

Boost串行库比serialib更可靠,找到它们here

2 个答案:

答案 0 :(得分:2)

所以我尝试了和你一样的步骤。我从http://serialib.free.fr/html/classserialib.html下载并编译了代码,并编译了一下

的警告

serialib.cpp:337:40:警告:从NULL转换为非指针类型'unsigned int'[-Wconversion-null]

(如果你知道如何解决这个问题,请告诉我)。但我运行程序时,Tx / Rx引脚连接到Arduino Mega Rx1 / Tx1引脚。程序写得很好,我可以看到Arduino读取字节。当我从Arduino写到Beaglebone Black的Rx时,它超时并且没有收到数据。

据我所知,这是Rx引脚设置方式的问题。

编辑:所以我只是打印了接收写入端口的数据的缓冲区,它接收数据就好了。所以问题在于 Ret 的价值。现在您可以使用缓冲区作为接收数据。我会试着弄清楚为什么 ReadString()的返回值不起作用。

答案 1 :(得分:2)

更正[-Wconversion-null]错误: 打开&#39; serialib.cpp&#39;文件并转到&#39; ReadStringNoTimeOut&#39;功能。

更改

ret=ReadChar(&String[NbBytes]);

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

相关问题