在 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/