Reading from a serial port after writing on it

前端 未结 5 919
执念已碎
执念已碎 2020-12-06 23:11

I am working on a project that has my computer communicating with an arduino board that reads the sensor output and put it on the serial port only if a \"t\" was received.th

5条回答
  •  天涯浪人
    2020-12-06 23:47

    Thank you for your answers. This is my final code:

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    
    int main() {    
    
        int STATE_OK=0;
        int STATE_WARNING=1;
        int STATE_CRITICAL=2; 
        char tempbuf[10];
        struct termios tty;
    
        int fd=open("/dev/ttyACM1",O_RDWR | O_NOCTTY);
        if(fd == -1){
                printf("Unable to open /dev/ttyACM1\n");
                return STATE_WARNING;
        }else {
            if(tcgetattr(fd, &tty)!=0){
                perror("tcgetatt() error");
            }else{
                    cfsetospeed(&tty, B9600);
                    cfsetispeed(&tty, B9600);
    
                    tty.c_cflag &= ~PARENB;
                    tty.c_cflag &= ~CSTOPB;
                    tty.c_cflag &= ~CSIZE;
                    tty.c_cflag |= CS8;
                    tty.c_cflag &= ~CRTSCTS; 
                    tty.c_cflag |= CLOCAL | CREAD;
    
                    tty.c_iflag |= IGNPAR | IGNCR;
                    tty.c_iflag &= ~(IXON | IXOFF | IXANY);
                    tty.c_lflag |= ICANON;
                    tty.c_oflag &= ~OPOST;
                    tcsetattr(fd, TCSANOW, &tty);
    
                    int w=write(fd, "t", 1);/*printf("%d\n",w);
                    fprintf(stderr, "fd = %d.\n", fd);*/
                    usleep(1000);
                    int n=read(fd,tempbuf,8);/*printf("%d \n",n);*/
                    tempbuf[9]=0;
                    float temp=atof(tempbuf);
    
                    if (temp>27){
                        printf("CRITICAL: %f celsius\n",temp);
                        return STATE_CRITICAL;
                    }else{
                        printf("Everything is OK and the temperature is %f Celsius\n",temp);
                        return STATE_OK;
                    }
            }
        }
        close(fd);
        return 0;
    }
    

提交回复
热议问题