导航

    全志在线开发者论坛

    • 注册
    • 登录
    • 搜索
    • 版块
    • 话题
    • 在线文档
    • 社区主页

    V3s串口连接传感器,读取传感器数据时候出错

    V Series
    4
    4
    1655
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • newcastle
      newcastle LV 6 最后由 xiaowenge 编辑

      大佬们,我用v3s串口连接传感器,读取传感器数据时候出错,read(): Resource temporarily unavailable,网上说加select,我加了没用,怎么回事啊

      #include <stdio.h>
      #include <sys/types.h>
      #include <sys/stat.h>
      #include <fcntl.h>
      #include <string.h>
      #include <sys/select.h>
      #include <termios.h>
      #include <stdlib.h>
      #include <unistd.h>
      #include <stdbool.h>
      #include <errno.h>
      
      #define SENSOR "/dev/ttyS2"
      
      int main()
      {
      	int fd,ret;
      	struct	termios option;
      	char	cRest[]={0xFE, 0x01, 0x0d, 0x8A, 0x80, 0x0d};
      	char	cPower_On[]={0xFE, 0x01, 0x0d, 0x0A, 0x00, 0x0d};
      	char	cTem[10];
      	fd_set rset;
      	struct timeval timeout;
      	int rv;
      	
      	fd = open(SENSOR,O_RDWR | O_NONBLOCK);
      	if(fd < 0)
      	{
      		perror("open()");
      		return -1;
      	}
      	if(fd > 0)
      	{
      		printf("uart2 open successfully!\n");
      	}
      	
      	/*配置串口*/
      	memset(&option, 0, sizeof option);
          option.c_iflag = IGNPAR;
      	option.c_cflag = B460800 | HUPCL | CS8 | CREAD | CLOCAL;
      	option.c_cc[VTIME] = 0;
      	option.c_cc[VMIN] = 10;
      	
      	ret = tcsetattr(fd, TCSANOW, &option);
      	if (ret < 0) 
      	{
      		printf("set USART2-352 failed\n");
      		return -1;
      	}
      	printf("USART2-352 is opened successfully! \n");
      
      	/*配置传感器*/
      	write(fd,cRest,6);
      	sleep(1);
      	write(fd,cPower_On,6);
          while(1)
          {
      //		FD_ZERO(&rset);
      //		FD_SET(fd,&rset);
      //
      //		timeout.tv_sec = 5;
      //      timeout.tv_usec = 0;
      		memset(cTem, 0, 10);
      		
      //		rv = select(fd+1,&rset,NULL,NULL,&timeout);
      //		if(rv < 0){
      //			perror("select error");
      //			continue;
      //			}
      //		else if(rv == 0){
      //			perror("select timeout");
      //			continue;
      //			}
      //		else
      //		{
      			ret=read(fd,cTem,10);
      			if(ret < 0)
      			{
      				perror("read()");
      				close(fd);
      				break;
      			}
      			if(ret > 0)
      			{
      				printf("read data : %s\n",cTem);
      			}
      		//}
          }
      	
      	close(fd);
      	return 0;
      }
      
      

      这是程序,编译没问题,运行成这样

      QQ图片20231024155932.png

      A 1 条回复 最后回复 回复 引用 分享 0
      • livpo
        livpo LV 6 最后由 编辑

        先把你的程序在x86上编译测试能不能打开并通讯,如果可以,说明v3s这个串口驱动有问题

        1 条回复 最后回复 回复 引用 分享 0
        • A
          Alb702 LV 5 @newcastle 最后由 编辑

          @livpo

          你好。

          在控制台中运行命令:
          echo 12345 >/dev/ttyS2

          如果答案是这样的:
          write error: Input/output error

          这意味着问题不在于您的程序,而在于驱动程序。

          需要在控制台查看 dmesg 命令的输出

          1 条回复 最后回复 回复 引用 分享 0
          • H
            honghu886 LV 4 最后由 编辑

            @livpo 在 v3s串口连接传感器,读取传感器数据时候出错 中说:

            能不能打开并通讯,如果可以,说明v3s这个串口驱动有问题

            串口直接这样读取,不会存在读取数据不完整,应该有一个中断接收完成的函数把

            1 条回复 最后回复 回复 引用 分享 0
            • Moved from 其它全志芯片讨论区 by  xiaowenge xiaowenge 
            • 1 / 1
            • First post
              Last post

            Copyright © 2024 深圳全志在线有限公司 粤ICP备2021084185号 粤公网安备44030502007680号

            行为准则 | 用户协议 | 隐私权政策