Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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二进制串行读取问题_C_Linux_Binary_Serial Port - Fatal编程技术网

C Linux二进制串行读取问题

C Linux二进制串行读取问题,c,linux,binary,serial-port,C,Linux,Binary,Serial Port,我编写了一些代码,试图在Linux(ubuntu)中通过串行端口读取二进制流。它的行为异常,丢失了部分数据: 代码: int-need; PBYTE pChar=(PBYTE)malloc(1024*4096*sizeof(字节)); int nRead=0; int SRL=open(“/dev/ttyS0”,O|RDWR | O|NOCTTY | O|NDELAY | O|NONBLOCK); 结构termios tty; 结构termios tty_old; memset(&tty,0,t

我编写了一些代码,试图在Linux(ubuntu)中通过串行端口读取二进制流。它的行为异常,丢失了部分数据:

代码:

int-need;
PBYTE pChar=(PBYTE)malloc(1024*4096*sizeof(字节));
int nRead=0;
int SRL=open(“/dev/ttyS0”,O|RDWR | O|NOCTTY | O|NDELAY | O|NONBLOCK);
结构termios tty;
结构termios tty_old;
memset(&tty,0,tty的大小);
//错误处理
如果(tcgetattr(SRL和tty)!=0)
{
printf(“\n错误:%d,%s\n”,errno,strerror(errno));
}
//保存旧的tty参数
tty_old=tty;
//设置波特率
cfsetospeed(&tty,(速度)B57600);
cfsetispeed&tty(速度)B57600;
//设置其他端口,设置为8n1
tty.c_cflag&=~PARENB;
tty.c_cflag&=~CSTOPB;
tty.c_cflag&=~CSIZE;
tty.c|u cflag |=CS8;
tty.c_cflag&=~CRTSCTS;//无流量控制
tty.c_cc[VMIN]=1;
tty.c_cc[VTIME]=1;
tty.c|u cflag |=CREAD | CLOCAL;//启用“读取”并忽略ctrl行
//生
//cfmakeraw&tty;
tty.c|lflag&=~(ICANON | ECHO | ECHO | ISIG);
tty.c|u iflag&=~(IXON | IXOFF | IXANY);
//刷新端口,然后应用属性
tcflush(SRL、TCIFLUSH);
如果(tcsetattr(SRL、TCSANOW和tty)!=0)
{
printf(“\n错误:%d,%s\n”,errno,strerror(errno));
}
整数计数=0;
int noread_cnt=0;
nNeed=1024;
而(1)
{
计数++;
//pthread_mutex_lock(&mutex_comm);
nRead=读取(SRL、pChar、nNeed);
//pthread_mutex_unlock(&mutex_comm);
如果(nRead>0)
{
printf(“\n读取:%d”,nRead);
对于(int i=0;i
正在发送斜坡:00 11 22 3 44 55 66 77 88 99 aa bb cc dd ee ff

(通过同事的测试应用程序发送,该应用程序已知有效)

我的代码输出如下:

改为:3 11 22 33

改为:3 55 66 77

阅读:399AA bb

阅读:3 dd ee ff

我的代码似乎删除了四个字节行的第一个字节,即00、44、88和cc字节


我是linux的新手,所以我想我可能是把端口设置错了。然而,我似乎找不到问题所在。对此,如有任何指导,将不胜感激

我发现了问题所在。我在串行端口上运行了一个getty。我转到/etc/init/并找到了ttyS0.conf文件。在它里面,它说它正在运行getty,所以我注释掉了所有的行。当我重新启动时,我的代码运行良好。但是,我确实意识到,现在我没有默认的串行控制台终端,除非我取消对getty指令的注释


Sawdust和Jim,谢谢您的评论。

您可能应该使用C标记来获得帮助。除了发布生成错误的不完整代码(
“for”循环初始声明仅在C99模式下允许)和各种警告之外,您的代码似乎可以正常工作。您应该删除C99要求,然后重新评估“已知工作的同事的测试应用程序”。例如,将其替换为终端仿真程序并键入字符或发送短文件。
int nNeed;
PBYTE pChar = (PBYTE) malloc(1024*4096*sizeof(BYTE));
int nRead = 0;


int SRL = open("/dev/ttyS0", O_RDWR| O_NOCTTY | O_NDELAY | O_NONBLOCK );

struct termios tty;
struct termios tty_old;
memset (&tty, 0, sizeof tty);

// Error Handling 
if ( tcgetattr ( SRL, &tty ) != 0 ) 
{
   printf("\n ERROR: %d, %s \n", errno, strerror(errno));
}

// Save old tty parameters 
tty_old = tty;

// Set Baud Rate
cfsetospeed (&tty, (speed_t)B57600);
cfsetispeed (&tty, (speed_t)B57600);

// Setting other Port Stuff, MAKE 8n1
tty.c_cflag     &=  ~PARENB;            
tty.c_cflag     &=  ~CSTOPB;
tty.c_cflag     &=  ~CSIZE;
tty.c_cflag     |=  CS8;

tty.c_cflag     &=  ~CRTSCTS;           // no flow control
tty.c_cc[VMIN]   =  1;                 
tty.c_cc[VTIME]  =  1;                  
tty.c_cflag     |=  CREAD | CLOCAL;     // turn on READ & ignore ctrl lines

// Make raw 
//cfmakeraw(&tty);

tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
tty.c_iflag &= ~(IXON | IXOFF |IXANY);

// Flush Port, then applies attributes 
tcflush( SRL, TCIFLUSH );
if ( tcsetattr ( SRL, TCSANOW, &tty ) != 0) 
{
     printf("\n ERROR: %d, %s \n", errno, strerror(errno));
}

int count = 0;
int noread_cnt = 0;
nNeed = 1024;

while(1) 
{
    count++;

    //pthread_mutex_lock(&mutex_comm);
    nRead = read(SRL, pChar, nNeed);
    //pthread_mutex_unlock(&mutex_comm);

    if(nRead > 0) 
    {
        printf("\n Read: %d    ", nRead);
        for (int i = 0; i < nRead; i++)
        {
            //printf("\n IN: (nRead) ");
            //printf("%02x  ",*pChar++);
            printf("%02x  ",pChar[i]);
        }
        printf("\n");
    }
    else if (nRead < 0)
    {
        if (errno == EAGAIN)
        {
            // Not real error, read again
            noread_cnt++;

            if (noread_cnt == 19200*100)
            {
                printf("EAGAIN\n");
                noread_cnt = 0;
            }
        }
        else
        {
            printf("\n nREad: %d, ERROR: %d, %s \n", nRead, errno, strerror(errno) );
        }
    }

    sleep(0);
}

return NULL;