0

私はFTDIチップ(VNC2評価ボード)に取り組んでいます。プログラムを介してチップからシリアルデータを読み取りたい。私が今持っているシリアルデータを読み取るコードは

#include <stdio.h>   
#include <string.h>  
#include <unistd.h>  
#include <fcntl.h>   
#include <errno.h>   
#include <termios.h> 
#include <signal.h>
#include <sys/time.h>
#include <signal.h>

void main()
{
    int portfd,rd;
    int n,f,len=0,t;
    char buf,*s;
    struct termios oldtio,newtio;
    portfd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
    if (portfd<0) printf("Error opening port\n");
    else if(portfd>0)
    {
        printf("Serial Port open . portfd=%d\n",portfd);
        tcgetattr(portfd,&oldtio);
        bzero(&newtio, sizeof(newtio));
        cfsetispeed(&newtio, B115200);
                cfsetospeed(&newtio, B115200);
                newtio.c_cflag |= (CLOCAL | CREAD);
                newtio.c_cflag &= ~PARENB; // set parity to no 
                newtio.c_cflag &= ~CSTOPB;  /*set one stop bit */
                newtio.c_cflag &= ~CSIZE;   /* Mask the character size bits */
                newtio.c_cflag |= CS8;      /* 8 bit data */
                newtio.c_cflag &= ~(ICANON | ECHO | ECHOE |ISIG);
                newtio.c_iflag &= ~(IXON | IXOFF | IXANY);
                newtio.c_oflag &= ~OPOST;
        newtio.c_cflag     &=  ~CRTSCTS;       // no flow control
            newtio.c_lflag     =   0;          // no signaling chars, no echo, no canonical processing
            newtio.c_oflag     =   0;                  // no remapping, no delays
            newtio.c_cc[VMIN]      =   0;                  // read doesn't block
            newtio.c_cc[VTIME]     =   5;
        tcflush(portfd, TCIFLUSH);

            if(tcsetattr (portfd, TCSANOW, &newtio) != 0)
            {
                    printf("Error from tcsetattr\n");
            }
        n = fcntl(portfd, F_GETFL, 0);
        (void)fcntl(portfd, F_SETFL, n & ~O_NDELAY);
        while(1){

                    rd= read(portfd,&buf,sizeof(char));
                //sleep(3);
                    if(rd==-1) printf("Reading error rd:%d\n",rd);
            else if(!rd) printf("No Data to read rd:%d\n",rd);
            else 
            printf(" data read  %c %d\n",buf,(int)buf);
        }

            tcsetattr(portfd, TCSANOW, &oldtio);
    }
    close(portfd);
}

しかし、まだデータを読み取ることができません。続行するのを手伝ってください。

ありがとうございました。

4

0 に答える 0