在Linux环境中从485端口读取旧数据

时间:2017-11-17 16:18:29

标签: c linux rs485

我正在通过C程序从RS 485端口通过以下代码从设备连续读取数据。由于某种原因,获得的数据不是设备中的最新数据。我读数据时没有反映数据的变化,它给了我旧的价值。几分钟后,我得到了改变的价值(因为我不断阅读)。如果我通过Pytty读取相同的设备,我会立即获得更新的值。所以我的代码中肯定存在一些问题,尽管我无法弄明白。任何帮助都会很棒!

static int load_serial_port(char *port) //port is 485, port="/dev/ttyS2"/
{
int fd = 0;
    fd = open (port, O_RDWR);
        if (fd < 0) {
            log_error("SerialPort opening failed.");
            return -1;
        }
        struct serial_rs485 rs485conf;
        /* Enable RS485 mode: */
        rs485conf.flags |= SER_RS485_ENABLED;

        /* Set logical level for RTS pin equal to 1 when sending: */
        rs485conf.flags |= SER_RS485_RTS_ON_SEND;
        /* or, set logical level for RTS pin equal to 0 when sending: */
        rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND);

        /* Set logical level for RTS pin equal to 1 after sending: */
        rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
        /* or, set logical level for RTS pin equal to 0 after sending: */
        rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND);

        /* Set rts delay before send, if needed: */
        rs485conf.delay_rts_before_send = 0;

        /* Set rts delay after send, if needed: */
        rs485conf.delay_rts_after_send = 0;

        /* Set this flag if you want to receive data even whilst sending data */
        //rs485conf.flags |= SER_RS485_RX_DURING_TX;

        if (ioctl (fd, TIOCSRS485, &rs485conf) < 0) {
            log_error("SerialPort config failed.");
            return -1;
        }
        struct termios option;
        tcgetattr(fd, &option);

        cfsetospeed(&option, B9600); /* baud rate */
        option.c_cflag &= ~PARENB; /* no parity */
        option.c_cflag &= ~CSTOPB; /* 1 stop bit */
        option.c_cflag &= ~CSIZE;
        option.c_cflag |= CS8 | CLOCAL; /* 8 bits */
        option.c_lflag = ICANON; /* canonical mode */
        option.c_oflag &= ~OPOST; /* raw output */

        tcsetattr(fd, TCSANOW, &option); /* apply the settings */
        tcflush(fd, TCOFLUSH);
        log_debug("SerialPort loaded fd %d", fd);
return fd;
}

以下是读取功能

static void port_read(port_t *s)
{

uint8_t rxBuffer[20];
char portString[20] = "";
double value = 0.0;
if(s->serialPortFd > 0) {
int amount = read(s->serialPortFd, rxBuffer, 100);
    int i = 0;
    int charindex = 0;
    if(amount > 1 ) {
        for (i = 0; i< amount; i++) {
            if ( isdigit(  rxBuffer[i]) || (   (char)rxBuffer[i] == '-' || (char)rxBuffer[i] == '.') ) {
                portString[charindex] = (char)rxBuffer[i];
                charindex++;
            }
        }
        portString[charindex] = '\0';
        sscanf(portString, "%lf", &value); //value is same ???
    }
}
memset(rxBuffer, 0, sizeof rxBuffer);
}

1 个答案:

答案 0 :(得分:0)

有时我觉得专家发帖,在这个论坛上发表评论而不仔细阅读问题。我在阅读并做了更多实验后找到了正确的解决方案,并期望节省时间。 我的阅读在我的代码中进行了以下更改:

            int amount = read(s->serialPortFd, rxBuffer, 20);
            tcflush(s->serialPortFd, TCIOFLUSH);

我错过了闪烁文件。