そのため、プロジェクトで 2 つの Xbee ZB (S2B) を使用してデータを相互に送信しています。8 データ ビット、パリティなし、1 ストップ ビット システム (8N1) です。
2 つの質問があります。
1. ラップトップの USB アダプタへの RS232 (DB9 コネクタ) インターフェイスと接続しているので、B230400 のボー レートでは、書き込み/読み取り/オープン/クローズよりもfwrite/fread/fopen/fcloseへのシステム コールを使用する方が適しています。? (私が知る限り、fread() にはボーレートの設定がないため、正しく動作しないと思います。)
2. 1 台のコンピューターで 1 つのプログラム (送信プログラム) と別のプログラム (受信プログラム) を実行しています。送信側と受信側のさまざまな情報セットを知りたいので、両方のプログラムが読み書きできる必要があります。
受信側(文字Qが表示されたら、ポートを停止して閉じる)または送信側(読み取り側が文字Gを送信して書き込みを開始し、書き込みが終了したら閉じる)のように。
( http://tldp.org/HOWTO/Serial-Programming-HOWTO/x115.html )のコードの一部を非標準入力処理で使用して、USB ポートからの書き込みと読み取りのための基本的なテンプレートをセットアップしようとしました。いずれかの側。私の質問は:
*送信プログラムと受信プログラムの termios 構造体 (newtio) に異なる構成設定 (c_cflag、c_iflag、c_oflag、c_lflag) が必要な場合は? *
私のコード:
#include <stdio.h>
#include <strings.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <termios.h>
int open_port(char *path, int modes)
{
int fd; // File descriptor for port
fd = open(path, modes); // Open the path
if(fd == -1) return -1; // Could not be opened
printf("Port %s opened successfully.\n", path);
return fd; // Return fd
}
int setBaud(struct termios *termios_p, int baud)
{
int ires = 0; int ores = 0;
switch(baud)
{
0:
ires = cfsetispeed(&termios_p, B0);
ores = cfsetispeed(&termios_p, B0);
break;
50:
ires = cfsetispeed(&termios_p, B50);
ores = cfsetispeed(&termios_p, B50);
break;
75:
ires = cfsetispeed(&termios_p, B75);
ores = cfsetispeed(&termios_p, B75);
break;
110:
ires = cfsetispeed(&termios_p, B110);
ores = cfsetispeed(&termios_p, B110);
break;
134:
ires = cfsetispeed(&termios_p, B134);
ores = cfsetispeed(&termios_p, B134);
break;
150:
ires = cfsetispeed(&termios_p, B150);
ores = cfsetispeed(&termios_p, B150);
break;
200:
ires = cfsetispeed(&termios_p, B200);
ores = cfsetispeed(&termios_p, B200);
break;
300:
ires = cfsetispeed(&termios_p, B300);
ores = cfsetispeed(&termios_p, B300);
break;
600:
ires = cfsetispeed(&termios_p, B600);
ores = cfsetispeed(&termios_p, B600);
break;
1200:
ires = cfsetispeed(&termios_p, B1200);
ores = cfsetispeed(&termios_p, B1200);
break;
1800:
ires = cfsetispeed(&termios_p, B1800);
ores = cfsetispeed(&termios_p, B1800);
break;
2400:
ires = cfsetispeed(&termios_p, B2400);
ores = cfsetispeed(&termios_p, B2400);
break;
4800:
ires = cfsetispeed(&termios_p, B4800);
ores = cfsetispeed(&termios_p, B4800);
break;
9600:
ires = cfsetispeed(&termios_p, B9600);
ores = cfsetispeed(&termios_p, B9600);
break;
19200:
ires = cfsetispeed(&termios_p, B19200);
ores = cfsetispeed(&termios_p, B19200);
break;
38400:
ires = cfsetispeed(&termios_p, B38400);
ores = cfsetispeed(&termios_p, B38400);
break;
57600:
ires = cfsetispeed(&termios_p, B57600);
ores = cfsetispeed(&termios_p, B57600);
break;
115200:
ires = cfsetispeed(&termios_p, B115200);
ores = cfsetispeed(&termios_p, B115200);
break;
230400:
ires = cfsetispeed(&termios_p, B230400);
ores = cfsetispeed(&termios_p, B230400);
break;
default:
ires = cfsetispeed(&termios_p, B9600);
ores = cfsetispeed(&termios_p, B9600);
break;
}
if(ires == -1 | ores == -1) return -1; // ISpeed or OSpeed could not be set
else { printf("Baud set successfully to %d\n", baud); return 0; }
}
char* readIt(int fd)
{
char buf;
char *tmp;
tmp = calloc(2, sizeof(char);
if( read(fd, buf, 1) == 1 ) { tmp[0] = buf; tmp[1] = 0; } // Return char in tmp[0]
else { tmp[0] = 0x45; tmp[1] = 0x52; } // Return ER in char meaning error
return tmp;
}
int writeIt(int fd, char *str, int bytes)
{
if( write(fd, str, bytes) == bytes ) return 0; // Write success
else return -1; // Failed write
}
int main (int argc, char **argv)
{
int usb;
volatile int STOP = 0;
if(argc == 1) { printf("Must enter usb port path.\n"); exit(EXIT_FAILURE); } // No second argument
else if (argc > 1) { usb = open_port(argv[1], O_RDWR | O_NOCTTY ); }
struct termios oldterm, newterm;
tcgetattr(fd, &oldtio); // Save old terminal settings
bzero(&newtio, sizeof(newtio)); // Clear out struct
if( setBaud(&newtio, 230400) != 0 ) { printf("Baud could not be set\n"); exit (EXIT_FAILURE); } // Set up baud rate
newtio.c_cflag = CS8 | CREAD | CLOCAL; // 8 data bits, enable receiver, local line
newtio.c_cflag &= ~PARENB | ~CSTOPB; // No parity, one stop bit
newtio.c_iflag &= ~(IXON | IXOFF | IXANY); // Disable software flow control
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0; // No timeout clock for read
newtio.c_cc[VMIN] = 1; // Wait for one character before reading another
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newtio); // Set fd with new settings immediately
/* Do writing or reading from port in here - readIt or writeIt*/
tcsetattr(fd, TCSANOW, &oldtio); // Set fd with old settings immediately
close(fd); // Close fd
return 0;
}
可能であれば、この時点でデータ破損を処理せずに最適な書き込み速度と読み取り速度を探しています。最適とは、termios 構造体の構成フラグの最適な設定のことです。
ご協力ありがとうございました。