改行で終了する ASCII 文字列をシリアル デバイスとの間で送受信する非常に小さな C プログラムがあります。で、USB アダプターを使用してコンピューターに接続されています/dev/ttyUSB0。
ほとんどの場合、検索するだけのコマンドを送信しますが、場合によってはすべての小文字を大文字に変換します。すべての特殊文字をそのままにします。
送信する文字列は/home\n. プログラムを実行する (再コンパイルせずに実行するだけで) 約 5 回に 1 回./a.out、デバイスが理解する送信メッセージは/HOME\n.
ここに私のソースコードがあります:
#include <stdio.h>
#include <stdlib.h>
#include "zserial.h"
int main() {
char buf[256];
int fd = connect("/dev/ttyUSB0");
char *cmd = "/home\n";
send(fd, cmd);
receive(fd, buf, 256);
puts(buf);
exit(0);
}
そして zserial.c:
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zserial.h"
int send(int fd, char *buf) {
int len = strlen(buf);
int nb = write(fd, buf, len);
if (len != nb || nb < 1)
perror("Error: wrote no bytes!");
tcdrain(fd);
return nb;
}
int receive(int fd, char *dst, int nbytes) {
int i;
char c;
for(i = 0; i < nbytes;) {
int r = read(fd, &c, 1);
/* printf("Read %d bytes\n", r); */
if (r > 0) {
dst[i++] = c;
if (c == '\n') break;
}
}
dst[i] = 0; /* null-terminate the string */
return i;
}
int connect(char *portname) {
int fd;
struct termios tio;
fd = open(portname, O_RDWR | O_NOCTTY | O_NONBLOCK);
tio.c_cflag = CS8|CREAD|CLOCAL;
if ((cfsetospeed(&tio, B115200) & cfsetispeed(&tio, B115200)) < 0) {
perror("invalid baud rate");
exit(-1);
}
tcsetattr(fd, TCSANOW, &tio);
return fd;
}
私は何を間違っていますか?シリアルポートの出力を変更する termios フラグはありますか?