Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C Linux termios修改串口读取()后的第一个字符_C_Linux_Serial Port_Termios - Fatal编程技术网

C Linux termios修改串口读取()后的第一个字符

C Linux termios修改串口读取()后的第一个字符,c,linux,serial-port,termios,C,Linux,Serial Port,Termios,我的termios安装程序正在使用read()修改从串行端口读取的第一个字符。我有一个微控制器和一个linux设备对话。微控制器响应从linux机器发送的命令。设置如下所示: 微控制器(PIC24F)RS485端口RS485到USB转换器Ubuntu PC 当我运行一个终端程序,如Cutecom时,一切都按计划进行。我向PIC发送了一个命令字符,我得到了一个响应,但是当我使用我的命令行程序时,第一个字符正在被修改。这是我的密码: #include <string.h> #incl

我的termios安装程序正在使用read()修改从串行端口读取的第一个字符。我有一个微控制器和一个linux设备对话。微控制器响应从linux机器发送的命令。设置如下所示:

  • 微控制器(PIC24F)RS485端口RS485到USB转换器Ubuntu PC
当我运行一个终端程序,如Cutecom时,一切都按计划进行。我向PIC发送了一个命令字符,我得到了一个响应,但是当我使用我的命令行程序时,第一个字符正在被修改。这是我的密码:

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

#define DEVICE "/dev/ttyUSB0"
#define SPEED B38400 

int main()
{
    struct termios tio; //to hold serial port settings
    struct termios stdio; //so we can accept user input
    struct termios old_stdio; //save the current port settings
    int tty_fd; //file descriptor for serial port
    int res, n, res2, read1, wri;
    char buf[255];
    char buf2[255]; 

    //save the current port settings
    tcgetattr(STDOUT_FILENO,&old_stdio); 

    //setup serial port settings
    bzero(&tio, sizeof(tio));
    tio.c_iflag = 0;
    tio.c_iflag = IGNPAR | IGNBRK | IXOFF;
    tio.c_oflag = 0;
    tio.c_cflag = CS8 | CREAD | CLOCAL; //8n1 see termios.h 
    tio.c_lflag = ICANON;

    //open the serial port
    tty_fd=open(DEVICE, O_RDWR | O_NOCTTY); 

    //set the serial port speed to SPEED
    cfsetospeed(&tio,SPEED); 

    //apply to the serial port the settings made above
    tcsetattr(tty_fd,TCSANOW,&tio); 

    for(n = 5; n > 0; n--)
    {
    printf("Please enter a command: ");
    (void)fgets(buf2, 255, stdin);
    (void)write(tty_fd, buf2, strlen(buf2));                   
    printf("Ok. Waiting for reply.");
    res = read(tty_fd, buf, 255);       
    printf("Read:%d START%d %d %d %d %dFINISH\n",res,buf[0],buf[1],buf[2],buf[3],
    buf[4]);              
    }

    //close the serial port 
    close(tty_fd); 

    //restore the original port settings
    tcsetattr(STDOUT_FILENO,TCSANOW,&old_stdio); 

    return EXIT_SUCCESS; 
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义设备“/dev/ttyUSB0”
#定义速度B38400
int main()
{
struct termios tio;//用于保存串行端口设置
struct termios stdio;//因此我们可以接受用户输入
struct termios old_stdio;//保存当前端口设置
int tty_fd;//串行端口的文件描述符
int-res,n,res2,read1,wri;
char-buf[255];
char-buf2[255];
//保存当前端口设置
tcgetattr(标准文件号和旧标准文件号);
//设置串行端口设置
bzero(&tio,sizeof(tio));
tio.c_iflag=0;
tio.c|u iflag=IGNPAR | IGNBRK | IXOFF;
tio.c_of Lag=0;
tio.c_cflag=CS8 | CREAD | CLOCAL;//8n1见termios.h
tio.c_lflag=ICANON;
//打开串行端口
tty_fd=打开(设备,O_RDWR | O_NOCTTY);
//将串行端口速度设置为speed
cfsetospeed(&tio,速度);
//将上述设置应用于串行端口
tcsetattr(tty_fd、TCSANOW和tio);
对于(n=5;n>0;n--)
{
printf(“请输入命令:”);
(无效)fgets(buf2,255,标准DIN);
(无效)写入(tty_fd,buf2,strlen(buf2));
printf(“好的,等待答复”);
res=读取(tty_fd,buf,255);
printf(“读取:%d开始%d%d%d%d%d完成\n”),res,buf[0],buf[1],buf[2],buf[3],
buf[4]);
}
//关闭串行端口
关闭(tty_fd);
//恢复原始端口设置
TceTattr(标准文件号、标准文件号和旧标准文件号);
返回退出成功;
}
这是我得到的结果的一个例子

  • 当PIC发送“00000\n”时,输出为:读取:6开始-16 48结束
  • 当PIC发送“23456\n”时,输出为:读取:6开始-14 51 52 53完成
  • 当PIC发送“34567\n”时,输出为:读取:6开始-14 52 53 54结束
  • 当PIC发送“45678\n”时,输出为:读取:6开始-12 53 54 55完成
  • 当PIC发送“56789\n”时,输出为:读取:6开始-12 54 55 56完成
由于某种原因,第一个字符被一些termios设置搞砸了。它必须是termios设置,因为当我运行Cutecom时,会准确返回上述相同的测试输入。我一遍又一遍地阅读手册,尝试输入控件上的所有不同设置,但无论我做什么都无法解决这个问题

为了便于修复,我只需将数据移动1个字符,但希望避免这样做

有没有人遇到过这样的问题,或者知道该怎么办

非常感谢

2013年3月28日 很好的建议,奥斯汀。对于感兴趣的人,这里有两个输出:

  • 首先是我的程序中的termios设置

    速度38400波特;第0行;第0列;直线=0; intr=;退出=;擦除=;杀死=;eof=; eol=;eol2=;swtch=;开始=;停止=; susp=;rprnt=;werase=;lnext=; 同花顺=;最小值=0;时间=0; -parenb-parodd cs8-hupcl-cstopb cread clocal-crtscts ignbrk-brkint ignpar-parmrk-inpck-istrip-inlcr-igncr-icrnl-ixon-ixoff -iuclc-ixany-imaxbel-iutf8 -opost-olcuc-ocrnl-onlcr-onocr-onlret-ofill-ofdel nl0 cr0 tab0 bs0 vt0 ff0 -isig icanon-iexten-echo-echok-echonl-noflsh-xcase-tostop-echoprt -echoctl-echoke

  • 以及cutecom使用的设置

    速度38400波特;第0行;第0列;直线=0; intr=^C;退出=^\;擦除=^?;杀死你;eof=^D;eol=; eol2=;swtch=;开始=^Q;停止=^S;susp=^Z;rprnt=^R; werase=^W;lnext=^V;冲洗=^O;最小值=60;时间=1; -parenb-parodd cs8 hupcl-cstopb cread clocal-crtscts ignbrk-brkint-ignpar-parmrk-inpck-istrip-inlcr-igncr-icrnl-ixon-ixoff -iuclc-ixany-imaxbel-iutf8 -opost-olcuc-ocrnl-onlcr-onocr-onlret-ofill-ofdel nl0 cr0 tab0 bs0 vt0 ff0 -isig-icanon-iexten-echo-echok-echonl-noflsh-xcase-tostop-echoprt -echoctl-echoke

我仍在经历这一切,并将更新后,我取得进展的职位

13年3月29日 还是有同样的问题。我甚至找到了Cutecom的源代码,并遵循了他们使用的termios设置。问题仍然存在。第一个字符已损坏

  • 以下是我的程序中的Termios设置。由于某些原因,无法设置刷新

    速度38400波特;第0行;第0列;直线=0; intr=^?;退出=^\;擦除=^H;杀死你;eof=^D;eol=; eol2=;swtch=;开始=^Q;停止=^S;susp=^Z;rprnt=^R; werase=^W;lnext=^V;同花顺=;最小值=60;时间=1; -parenb-parodd cs8 hupcl-cstopb cread clocal-crtscts ignbrk-brkint-ignpar-parmrk-inpck-istrip-inlcr-igncr-icrnl-ixon-ixoff -iuclc-ixany-imaxbel-iutf8 -opost-olcuc-ocrnl-onlcr-onocr-onlret-ofill-ofdel nl0 cr0 tab0 bs0 vt0 ff0 -isig-icanon-iexten-echo-echok-echonl-noflsh-xcase-tostop-echoprt -echoctl-echoke

  • 我的新代码是:

    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <termios.h>
    #include <sys/ioctl.h>
    
    #define DEVICE "/dev/ttyUSB0"
    #define SPEED B38400 
    
    int main()
    {
    struct termios tio; //to hold serial port settings
    struct termios stdio; //so we can accept user input
        struct termios old_stdio; //save the current port settings
        int tty_fd; //file descriptor for serial port
        int retval, res, n, res2, read1, wri;
        char buf[255];
        char buf2[255]; 
    
    
        tty_fd = open(DEVICE, O_RDWR | O_NDELAY);
        if(tty_fd < 0)
        {
            perror(DEVICE);
            exit(-1);
        }
        printf("Init 1 complete.\n");
    
        tcflush(tty_fd, TCIOFLUSH);
    
        int f = fcntl(tty_fd, F_GETFL, 0);
        fcntl(tty_fd, F_SETFL, f & ~O_NDELAY);
    
        retval = tcgetattr(tty_fd, &old_stdio);
        if(retval != 0)
        {
            perror(DEVICE);
            exit(-1);
        }
        printf("Init 2 complete.\n");
    
        struct termios newtio;
        retval = tcgetattr(tty_fd, &newtio);
        if(retval != 0)
        {
            perror(DEVICE);
            exit(-1);
        }
        printf("Init 3 complete.\n");
    
        cfsetospeed(&newtio, SPEED);
        cfsetispeed(&newtio, SPEED);
    
        newtio.c_cflag = (newtio.c_cflag & ~CSIZE) | CS8;
        newtio.c_cflag |= CLOCAL | CREAD;
        newtio.c_cflag &= ~(PARENB | PARODD);
        newtio.c_cflag &= ~CRTSCTS;
        newtio.c_cflag &= ~CSTOPB;
    
        newtio.c_iflag = IGNBRK;
        newtio.c_iflag &= ~(IXON | IXOFF | IXANY);
    
        newtio.c_lflag = 0;
    
        newtio.c_oflag = 0;
    
        newtio.c_cc[VTIME] = 1;
        newtio.c_cc[VMIN] = 60;
        newtio.c_cc[VINTR] = 127; 
        newtio.c_cc[VQUIT] = 28;
        newtio.c_cc[VERASE] = 8;
        newtio.c_cc[VKILL] =  21;
        newtio.c_cc[VEOF] = 4;
        newtio.c_cc[VSTOP] = 19;
        newtio.c_cc[VSTART] = 17;
        newtio.c_cc[VSUSP] = 26;
        newtio.c_cc[VREPRINT] = 18;
        newtio.c_cc[VFLSH] = 15;
        newtio.c_cc[VWERASE] = 23;
        newtio.c_cc[VLNEXT] = 22;
    
    
        retval = tcsetattr(tty_fd, TCSANOW, &newtio);
        if(retval != 0)
        {
            perror(DEVICE);
            exit(-1);
        }
        printf("Init 4 complete.\n");
    
        int mcs = 0;
        ioctl(tty_fd, TIOCMGET, &mcs);
        mcs |= TIOCM_RTS;
        ioctl(tty_fd, TIOCMSET, &mcs);
    
        retval = tcgetattr(tty_fd, &newtio);
        if(retval != 0)
        {
            perror(DEVICE);
            exit(-1);
        }
        printf("Init 5 complete.\n");
    
        newtio.c_cflag &= ~CRTSCTS;
    
        retval = tcsetattr(tty_fd, TCSANOW, &newtio);
        if(retval != 0)
        {
            perror(DEVICE);
            exit(-1);
        }
        printf("Init 6 complete.\n");
    
    
        for(n = 5; n > 0; n--)
        {
        printf("Please enter a command: ");
        (void)fgets(buf2, 255, stdin);
        (void)write(tty_fd, buf2, strlen(buf2));
        printf("Ok. Waiting for reply\n");
        res = read(tty_fd, buf, 255);       
        printf("Read:%d START%d %d %d %d %dFINISH\n",res,buf[0],buf[1],buf[2], buf[3],
        buf[4]);              
        }
    
        //restore the original port settings
        tcsetattr(tty_fd, TCSANOW, &old_stdio); 
    
        close(tty_fd);
    
        return EXIT_SUCCESS; //return all good
    }
    
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #定义设备“/dev/ttyUSB0”
    #定义速度B38400
    int main()
    {
    struct termios tio;//用于保存串行端口设置
    struct termios stdio;//因此我们可以接受用户输入
    struct termios old_stdio;//保存当前端口设置
    int tty_fd;//串行端口的文件描述符
    int-retval、res、n、res2、read1、wri;
    char-buf[255];
    char-buf2[255];
    tty_fd=
    
    stty -a -F /dev/ttyUSB0
    
    while(1)
    {
        WATCHDOG();
    
        if(flag == 1)
        {
            char *start = str2;
            RS485_TXEN1();
            indicator = UART1_getch(); //get character sent from PC
            switch(indicator)
            {
                case '1' :                       
                        UART1_putstr("00000\n");
                        DAC_Write( DAC_CH_2, 4095);
                        break;
                case '2' :                      
                        UART1_putstr("23456\n");
                        DAC_Write( DAC_CH_2, 0);
                        break;
                case '3' :
                        UART1_putstr("34567\n");
                        break;
                case '4' :                       
                        UART1_putstr("45678\n");
                        break;
                case '\n' :
                        UART1_putch('\n');
                        break;
                default  :         
                        UART1_putstr("56789\n");                  
                        break;
            }
            RS485_RXEN1();
            flag = 0;
        }
    }