1

私はLinuxが初めてです。シリアル通信で問題に直面しています。私は 2 台のコンピューターを持っています。1 台はハイパー ターミナルを実行しており、もう 1 台はハイパー ターミナルと通信するための C プログラムを作成しています。両方の PC は RS232 経由で接続されています。いくつかのコードを書き、ハイパー ターミナルにデータを送信することはできますが、Linux PC で C で記述されたプログラムを使用して、ハイパー ターミナルからデータを読み取る方法がわかりません。

助言がありますか?

前もって感謝します。

以下は私のコードです:

int main(int argc, char **argv)
{
    struct termios options;
    int fd;

    fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
    if(fd == -1)
    {
        printf("Could not open port /dev/ttyS0\n");
        return 1;
    }

    tcgetattr(fd, &options); //get port options

    cfsetispeed(&options, B9600); //set input baud rate
    cfsetospeed(&options, B9600); //set output baud rate

    options.c_cflag |= (CLOCAL | CREAD); //enable receiver and set local mode

    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    options.c_cflag &= ~CRTSCTS; //disable hardware flow control
    options.c_cflag &= ~(ICANON | ECHO | ISIG); //set raw input

    options.c_cflag |= IXOFF; //disable software flow control

    tcsetattr(fd, TCSANOW, &options); //set new port options

    sleep(1);
    int rc,count;
    int size = 8;
    unsigned char buf[10];
    FILE *fp = NULL;
    char ch;
    int i=0;
    printf("enter the data you want to send");

    while((ch=getchar())!='\n')
    {
        write(fd,&ch,1);
    }
    close(fd);

    printf("Finished.\n");

    return 0;
}
4

0 に答える 0