Beaglebone Black 串行 C++ [英] Beaglebone Black Serial c++
问题描述
在 C++ 中进行串行工作似乎很痛苦,此外,在 Beaglebone Black 上进行串行工作很困难,所以我需要有一些专业知识的人!
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!
我使用以下命令创建了/dev/ttyO4:
I created /dev/ttyO4 with the following command:
echo BB-UART4 > /sys/devices/bone_capemgr.9/slots
这给了我/dev/ttyO4.然后,我使用在 此处 链接的库在 cpp 中编写了一个小程序.我的代码如下:
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;
}
当我运行代码时,它说它成功打开端口并成功串行写入,但我在RX上没有收到任何数据.我已将 RX 引脚连接到 UART4(P9.11 和 P9.13)的 TX 引脚.我还连接了 UART5 的 RX 和 TX 引脚以防万一(P8.37 和 P8.38),因为 ttyO4 使我对我使用的 UART 有点困惑.
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.
我是否缺少让串行端口正常工作的东西?或者有人可以向我推荐一个在 beaglebone black 上使用 c++ 进行串行通信的工作示例,也许就像一个分步指南?
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?
问候,康奈尔
Boost 串行库比 serialib 更可靠,请在此处找到它们.
The Boost serial libraries work more reliably than serialib, find them here.
推荐答案
更正 [-Wconversion-null] 错误:打开serialib.cpp"文件并转到ReadStringNoTimeOut"函数.
to correct the [-Wconversion-null] error: Open 'serialib.cpp' file and go to the 'ReadStringNoTimeOut' function.
改变
ret=ReadChar(&String[NbBytes]);
到
ret=ReadChar(&String[NbBytes],0);
这篇关于Beaglebone Black 串行 C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!