C++ でシリアルを動作させるのは大変なようですが、それに加えて、Beaglebone Black でそれを行うのは難しいので、専門知識のある人が必要です!
次のコマンドで /dev/ttyO4 を作成しました。
echo BB-UART4 > /sys/devices/bone_capemgr.9/slots
これにより、/dev/ttyO4 が得られます。次に、ここにリンクされているライブラリを使用して 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 でデータを受信しません。UART4 の RX 端子を TX 端子に接続しました (P9.11 および P9.13)。また、UART5 の RX および TX ピンも接続しました (P8.37 および P8.38)。ttyO4 は、使用している UART について少し混乱させました。
シリアルポートを機能させるために欠けているものはありますか? または、ステップバイステップガイドのように、beaglebone black での c++ とのシリアル通信の実際の例を教えてもらえますか?
よろしく、 コーネル
編集:
Boost シリアル ライブラリは、serialib よりも確実に動作します。ここで見つけてください。