Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.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环境下,从485端口读取旧数据_C_Linux_Rs485 - Fatal编程技术网

C 在Linux环境下,从485端口读取旧数据

C 在Linux环境下,从485端口读取旧数据,c,linux,rs485,C,Linux,Rs485,我通过以下代码,通过C程序从设备上连续读取RS 485端口的数据。由于某些原因,获取的数据不是设备中的最新数据。当我读取数据时,数据的变化没有反映出来,它给了我旧的值。几分钟后,我得到了更改后的值(因为我一直在阅读)。如果我通过Pytty读取相同的设备,我会立即得到更新的值。因此,我的代码中肯定有一些问题,尽管我无法解决。任何帮助都会很好 static int load_serial_port(char *port) //port is 485, port="/dev/ttyS2"/ { int

我通过以下代码,通过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 int load_serial_port(字符*端口)//端口为485,端口=“/dev/ttyS2”/
{
int-fd=0;
fd=打开(端口,O_RDWR);
如果(fd<0){
日志错误(“串行端口打开失败”);
返回-1;
}
结构串行接口rs485 rs485conf;
/*启用RS485模式:*/
rs485conf.flags |=SER_RS485_已启用;
/*发送时,将RTS引脚的逻辑级别设置为1:*/

rs485conf.flags |=发送时的SER_RS485_RTS_; /*或者,发送时将RTS引脚的逻辑级别设置为0:*/ rs485conf.flags&=~(SER_RS485_RTS_ON_SEND); /*发送后,将RTS引脚的逻辑级别设置为1:*/
rs485conf.flags |=发送后的SER_RS485_RTS_; /*或者,发送后将RTS引脚的逻辑级别设置为0:*/ rs485conf.flags&=~(发送后的序列号RS485); /*如果需要,在发送前设置rts延迟:*/ rs485conf.delay\u rts\u发送之前=0; /*如果需要,设置发送后的rts延迟:*/ rs485conf.delay\u rts\u发送后=0; /*如果要在发送数据的同时接收数据,请设置此标志*/
//rs485conf.flags |=发送期间的SER_RS485_RX_; 如果(ioctl(fd、TIOCSRS485和rs485conf)<0){ 日志_错误(“SerialPort配置失败”); 返回-1; } 结构termios选项; tcgetattr(fd和选项); cfsetospeed(&option,B9600);/*波特率*/ option.c_cflag&=~PARENB;/*无奇偶校验*/ option.c_cflag&=~CSTOPB;/*1停止位*/ option.c_cflag&=~CSIZE; option.c|u cflag |=CS8 | CLOCAL;/*8位*/ option.c_lflag=ICANON;/*规范模式*/ option.c_of lag&=~OPOST;/*原始输出*/ tcsetattr(fd、TCSANOW和option);/*应用设置*/ tcflush(fd,tcflush); 日志调试(“串行端口加载的fd%d”,fd); 返回fd; }
下面是read函数

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);
}
静态无效端口读取(端口*s)
{
uint8_t rxBuffer[20];
char portString[20]=“”;
双值=0.0;
如果(s->serialPortFd>0){
int amount=读取(s->serialPortFd,rxBuffer,100);
int i=0;
int charindex=0;
如果(金额>1){
对于(i=0;i
有时我觉得专家们在这个论坛上发帖、评论,而没有仔细阅读问题。在阅读和做了更多的实验之后,我找到了正确的解决方案,我希望能节省时间。 我的阅读与代码中的以下更改一致:

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

我没有刷新文件。

Dunno mate,由于某些原因,您的端口对象没有更新。检查它是否具有“刷新”或“读取”功能。也许在端口读取函数中调用em是个好主意。rs485conf.flags |=SER_RS485_RTS_ON_SEND;rs485conf.flags&=~(SER_RS485_RTS_ON_SEND);你想在这里干什么?你先把钻头打开,然后把它关掉。与发送后的SER_RS485_RTS_相同,您的
读取(s->serialPortFd,rxBuffer,100)
是错误的:您在一个只有20个字节的缓冲区中读取多达100个字节,因此有缓冲区溢出的风险。考虑编写<代码>阅读(S- > SerialPofd,RxPuffic,siZeof(RxPuffic))< /C> >。@ SV SV,其中读取函数,但我没有找到刷新功能。Putty的工作情况如何?在putty中,我有完全相同的设置。您的代码不完整;特别是,它似乎缺少一个
main()
函数和至少一个
#include
。请输入您的代码,使其成为您问题的一部分,然后我们可以尝试复制并解决它。你也应该阅读。