获取串行通讯数据错误 [英] Getting wrong data in serial communication

查看:166
本文介绍了获取串行通讯数据错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个简单的C程序,从串行设备获取数据串行。这些数据是十六进制格式。写code之前,我检查了它在cutecom,我正在接受
  25 54 00 1E 这是正确和精确的数值。但是,当我写的code,那么我收到此 BFE50A14 这是错误的数据。我不知道在哪里,我犯了一个错误,请大家帮忙。谢谢!

I am making a simple C program to get data serially from a serial device. The data is in hex format. Before writing the code I checked it in cutecom and I was receiving 25 54 00 1e which is the correct and exact value. But when I write the code then I receive this BFE50A14 which is wrong data. I dont know where I am making a mistake, please help. Thanks.!

code:

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

#define portname "/dev/ttyUSB0"

int set_interface_attribs (int fd, int speed, int parity)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
                // error_message ("error %d from tcgetattr", errno);
                printf("error opening the device");
                return -1;
        }

        cfsetospeed (&tty, speed);
        cfsetispeed (&tty, speed);

        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
        // disable IGNBRK for mismatched speed tests; otherwise receive break
        // as \000 chars
        tty.c_iflag &= ~IGNBRK;         // disable break processing
        tty.c_lflag = 0;                // no signaling chars, no echo,
                                    // no canonical processing
        tty.c_oflag = 0;                // no remapping, no delays
        tty.c_cc[VMIN]  = 0;            // read doesn't block
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

        tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                    // enable reading
        tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
        tty.c_cflag |= parity;
        tty.c_cflag &= ~CSTOPB;
        tty.c_cflag &= ~CRTSCTS;

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
        {
            // error_message ("error %d from tcsetattr", errno);
            printf("error opening the device");
            return -1;
        }
        return 0;
}

int set_blocking (int fd, int should_block)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
            //error_message ("error %d from tggetattr", errno);
            printf("error opening the device");
            return;
        }

        tty.c_cc[VMIN]  = should_block ? 1 : 0;
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
          //  error_message ("error %d setting term attributes", errno);
            printf("error opening the device");
}


int main()
{


    int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
    if (fd < 0)
    {

            printf("error opening the device");

    }
/*CHANGES*/
if(!set_interface_attribs(fd, B9600, 0))
        {
            printf("error set interface");
        }
        else
           printf("correct");
        if(!set_blocking(fd, 0))
        {
            printf("error set blocking");
        }
        else
            printf("done");
*/
    set_interface_attribs (fd, B9600, 0);


    set_blocking (fd, 0);                // set no blocking

    usleep ((7 + 25) * 100);             

    int receivebuffer [10];

    read (fd, receivebuffer, sizeof receivebuffer);
/***CHANGES***//
    printf("value of buffer is %2X %2X %2X %2X \n\n", receivebuffer[0],receivebuffer[1],receivebuffer[2],receivebuffer[3]);



return 0;
}

我收到的receivebuffer数据,我用printf打印出来,并使用%X 来打印十六进制格式。我得到的输出是 BFE50A14 但正确的输出 25 54 00 1E 。请帮忙,谢谢!

I am receiving the data in receivebuffer and I am printing it using printf and using %X to print it in hex format. The output I am getting is BFE50A14 but the correct output is 25 54 00 1e . Please help, Thanks.!

推荐答案

下面是我们在评论到目前为止包括:

here is what we have covered so far in comments:

Note: this only vaguely resembles the code most recently posted

Note: do not use tabs in your code. because
      different editors have the tab stops and/or tab width
      set differently

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

#define portname "/dev/ttyUSB0"

static struct termios oldtty;

int set_interface_attribs (int fd, int speed, int parity)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &oldtty) != 0)
        {
                // error_message ("error %d from tcgetattr", errno);
                printf("error opening the device");
                return -1;
        }

        memcpy( tty, oldtty, sizeof struct termion );

        cfsetospeed (&tty, speed);
        cfsetispeed (&tty, speed);

        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
        // disable IGNBRK for mismatched speed tests; otherwise receive break
        // as \000 chars
        tty.c_iflag &= ~IGNBRK;         // disable break processing
        tty.c_lflag = 0;                // no signaling chars, no echo,
                                    // no canonical processing
        tty.c_oflag = 0;                // no remapping, no delays
        tty.c_cc[VMIN]  = 0;            // read doesn't block
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

        tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                    // enable reading
        tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
        tty.c_cflag |= parity;
        tty.c_cflag &= ~CSTOPB;
        tty.c_cflag &= ~CRTSCTS;

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
        {
            // error_message ("error %d from tcsetattr", errno);
            printf("error opening the device");
            return -1;
        }
        return 0;
} // end function: set_interface_attribs


int set_blocking (int fd, int should_block)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
            //error_message ("error %d from tggetattr", errno);
            printf("error opening the device");
            return -1;
        }

        tty.c_cc[VMIN]  = should_block ? 1 : 0;
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
          //  error_message ("error %d setting term attributes", errno);
            printf("error opening the device");
    return 0;
} // end function: set_blocking


int main()
{
    int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
    if (fd < 0)
    {
        printf("error opening the device");
    }


    /*CHANGES*/
    if(!set_interface_attribs(fd, B9600, 0))
    {
        printf("error set interface");
    }

    else
       printf("correct");

    if(!set_blocking(fd, 0))
    {
        printf("error set blocking");
    }

    else
        printf("done");

    // what is this stray end of comment?  
    // suggest enabling all the compiler warings
    // and fixing the warnings 
    // */
    if( set_interface_attribs (fd, B9600, 0) )
    { // then set_interface_attribs failed
        return -1;
    }

    // implied else set_interface_attribs successful

    if( set_blocking (fd, 0) )                // set no blocking
    { // then set_blocking failed
        return -1;   // might need to also restore oldtty attributes
    }

    // implied else, set_blocking successful

    usleep ((7 + 25) * 100);             

    char receivebuffer [20];

    if( 4 > read (fd, receivebuffer, sizeof receivebuffer) )
    { // then read failed
        return -1;
    }

    // implied else, read successful

    printf("value of buffer is %2X %2X %2X %2X \n\n", 
        receivebuffer[0],
        receivebuffer[1],
        receivebuffer[2],
        receivebuffer[3]);

    // cleanup
    tcsetattr (fd, TCSANOW, &oldtty);
    return 0;
} // end function: main

这篇关于获取串行通讯数据错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆