c++ - Beaglebone 黑色串行 c++

标签 c++ serial-port

在 C++ 中进行串行操作似乎很痛苦,此外,在 Beaglebone Black 上进行操作也很困难,所以我需要有一些专业知识的人!

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

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

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

#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(P9.11 和 P9.13)的 TX 引脚。我还连接了 UART5 的 RX 和 TX 引脚以防万一(P8.37 和 P8.38),因为 ttyO4 让我有点困惑我正在使用哪个 UART。

为了让串行端口正常工作,我缺少什么吗?或者有人可以向我推荐一个在 beaglebone black 上使用 c++ 进行串行通信的工作示例,也许就像一个循序渐进的指南?

问候, 康乃尔

编辑:

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

最佳答案

所以我尝试了与您相同的步骤。我从 http://serialib.free.fr/html/classserialib.html 下载并编译了代码并编译时带有警告

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

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

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

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

关于c++ - Beaglebone 黑色串行 c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18128814/

相关文章:

c++ - 使用 C++ 和 Linux 收听 "connection less UDP Multicast"的步骤

c++ - QT_NO_DEBUG 是否会导致 NDEBUG 的定义?

java - BGS5T RS232 与 1 线传感器通信

c - 在 Linux 中用 C 语言读取 RS232 串行引脚

python - Linux用户空间串口通信(非阻塞)

c++ - 移除 vector 元素使用 vector<bool> 中的条件

c++ - 使用索引设置 vector 元素时的奇怪行为

c++ - boost::integer_mask gcc 编译器错误

linux - 在 Linux 中与 CashCode 账单接受器通信

c# - 如何在 .NET 中检测通过串行端口或以太网连接的设备状态的变化