1

改行で終了する 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 フラグはありますか?

4

1 に答える 1

3

c_oflag & OLCUC出力で小文字から大文字へのマッピングをオンにします。を初期化tioしたことがないので、ランダムなフラグが設定されていても驚くことではありません。

次の 2 つの選択肢があります。

  1. tcgetattr現在の設定をtermios構造体に初期化してから、興味のあるものを変更してから、それらを書き戻しますtcsetattr

  2. 速度フィールドだけでなく、すべての termios フィールドを既知の値に初期化します。c_cflag

于 2014-10-01T03:16:39.327 に答える