代码产生错误的结果

时间:2017-02-19 05:34:46

标签: c serial-port

以下代码在(Geany)和Runs中编译。 --->

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

int set_interface_attribs(int fd, int speed)
{
    struct termios tty;

    if (tcgetattr(fd, &tty) < 0) {
        printf("Error from tcgetattr: %s\n", strerror(errno));
        return -1;
    }

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

    tty.c_cflag |= (CLOCAL | CREAD);    /* ignore modem controls */
    tty.c_cflag &= ~CSIZE;
    tty.c_cflag |= CS8;         /* 8-bit characters */
    tty.c_cflag &= ~PARENB;     /* no parity bit */
    tty.c_cflag &= ~CSTOPB;     /* only need 1 stop bit */
    tty.c_cflag &= ~CRTSCTS;    /* no hardware flowcontrol */

    /* setup for non-canonical mode */
//    tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
//   tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
    tty.c_oflag &= ~OPOST;

    /* fetch bytes as they become available */
    tty.c_cc[VMIN] = 1;
    tty.c_cc[VTIME] = 1;
    tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);    /* sets Raw mode */

    if (tcsetattr(fd, TCSANOW, &tty) != 0) {
        printf("Error from tcsetattr: %s\n", strerror(errno));
        return -1;
    }
    return 0;
}

void set_mincount(int fd, int mcount)
{
    struct termios tty;

    if (tcgetattr(fd, &tty) < 0) {
        printf("Error tcgetattr: %s\n", strerror(errno));
        return;
    }

    tty.c_cc[VMIN] = mcount ? 1 : 0;
    tty.c_cc[VTIME] = 5;        /* half second timer */

    if (tcsetattr(fd, TCSANOW, &tty) < 0)
        printf("Error tcsetattr: %s\n", strerror(errno));
}


int main()
{
    char *portname = "/dev/ttyS0";
    int fd;
    int i;
    int wlen;

    fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
    if (fd < 0) {
        printf("Error opening %s: %s\n", portname, strerror(errno));
        return -1;
    }
    /*baudrate 2400, 8 bits, no parity, 1 stop bit */
    set_interface_attribs(fd, B2400);
    //set_mincount(fd, 0);                /* set to pure timed read */

    unsigned char str[] = {0x56, 0x45, 0x52, 0x0D};
    write(fd,str,strlen(str));


    /* simple output */

/*
    wlen = write(fd, "*\n", 2);
    if (wlen != 2) {
        printf("Error from write: %d, %d\n", wlen, errno);
    }
    wlen = write(fd, "1\n", 2);
    if (wlen != 2) {
        printf("Error from write: %d, %d\n", wlen, errno);
    }
    wlen = write(fd, "8\n", 2);
    if (wlen != 2) {
        printf("Error from write: %d, %d\n", wlen, errno);
    }
*/
    tcdrain(fd);    /* delay for output */

    /* simple noncanonical input */

    do {
        unsigned char buf[80];
        int rdlen;

        rdlen = read(fd, buf, sizeof(buf) - 1);
        if (rdlen > 0) {
#ifdef DISPLAY_STRING
            buf[rdlen] = 0;
            printf("Read %d: \"%s\"\n", rdlen, buf);
#else /* display hex */
            unsigned char   *p;
            printf("Read %d:", rdlen);
            for (p = buf; rdlen-- > 0; p++)
                printf(" 0x%x", *p);
            printf("\n");
#endif
        } else if (rdlen < 0) {
            printf("Error from read: %d: %s\n", rdlen, strerror(errno));
        }
        /* repeat read to get full message */
    } while (i++ < 10);
}

&LT; ---  我需要在&#39; Serial Port&#39;调用某个结果: 从键盘&#34; * 1 8 cr&#34;在Windows(Turbo Basic)下完美运行,但在我的Linux下运行的代码下运行(由GCC编译): - &GT;

For " * 1 8 cr " (star one eight enter(cr)) the four "binary" "chars" required are:
*= binary 00101010
1= binary 00110001
8= binary 00111000
cr= binary 00001101

&LT; -   不幸的是我的代码没有创建正确的输出(串行设置被认为是使用&#39; termios&#39;结构设置为(2400 N,8,1有开始和结束位)。 我已经附加了一个旧的OscilloScope并且有些位被发送但我不知道如何捕获它们来检查它们的正确性。   该守则有什么问题?

0 个答案:

没有答案