LTM8000温度模块源码

  1. /*
  2. 本程序读取ltm8662模块的数据
  3.  
  4. 因目前系统中只有一个模块 故模块地址默认为 00
  5.  
  6. 程序只能支持每个通道接一个传感器的情况
  7.  
  8. 只支持ID为0x28的温度传感器数据格式 如有需要自己添加新的数据格式解析程序
  9.  
  10. */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <memory.h>
  14. #include <unistd.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <fcntl.h>
  18. #include <termios.h>
  19. #include <errno.h>
  20.  
  21. #define FALSE -1
  22. #define TRUE 1
  23.  
  24. #define DEBUG_FUNCTION 1
  25.  
  26. typedef struct ltm8662{
  27.     char channel_sensors[8];//每个通道上传感器的数量 最多64个
  28.     char channel_sensorid[8];//每个通道上传感器的ID 依此来解析数据 实际上应该是8×64 现在每个通道只接一个传感器只设为1
  29.  
  30. }LTM8662;
  31.  
  32. typedef LTM8662* PLTM8662;
  33.  
  34.  
  35.  
  36. /////添加三个静态全局变量
  37. static char *g_dev = "/dev/tq2440_serial1";
  38. static int g_com_fd=-1;
  39. static LTM8662 g_ltm8662;
  40.  
  41. ///////////////////////////////////////////com
  42. /**
  43. *@brief setting serial com speed
  44. *@param fd int handle of com
  45. *@param speed int
  46. *@return void
  47. */
  48. int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
  49. B38400, B19200, B9600, B4800, B2400, B1200, B300, };
  50. int name_arr[] = { 38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
  51. 19200, 9600, 4800, 2400, 1200, 300, };
  52. void set_speed(int fd, int speed)
  53. {
  54.     int i;
  55.     int status;
  56.     struct termios Opt;
  57.     tcgetattr(fd, &Opt);
  58.     for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {
  59.         if (speed == name_arr[i]) {
  60.             tcflush(fd, TCIOFLUSH);
  61.             cfsetispeed(&Opt, speed_arr[i]);
  62.             cfsetospeed(&Opt, speed_arr[i]);
  63.             status = tcsetattr(fd, TCSANOW, &Opt);
  64.             if (status != 0) {
  65.                 perror("tcsetattr fd");
  66.                 return;
  67.             }
  68.             tcflush(fd,TCIOFLUSH);
  69.         }
  70.     }
  71. }
  72.  
  73. /*
  74. *@brief setting databits,stopbits,parity
  75. *@param fd int handle of device
  76. *@param databits int value is 7 or 8
  77. *@param stopbits int value is 1 or 2
  78. *@param parity int value is N,E,O,,S
  79. */
  80. int set_Parity(int fd,int databits,int stopbits,int parity)
  81. {
  82.     struct termios options;
  83.     if ( tcgetattr( fd,&options) != 0) {
  84.         perror("SetupSerial 1");
  85.         return(FALSE);
  86.     }
  87.     options.c_cflag &= ~CSIZE;
  88.     switch (databits)
  89.     {
  90.     case 7:        
  91.         options.c_cflag |= CS7;
  92.         break;
  93.     case 8:
  94.         options.c_cflag |= CS8;
  95.         break;
  96.     default:
  97.         fprintf(stderr,"Unsupported data size\n"); return (FALSE);
  98.     }
  99.     switch (parity)
  100.     {
  101.     case 'n':
  102.     case 'N':
  103.         options.c_cflag &= ~PARENB; /* Clear parity enable */
  104.         options.c_iflag &= ~INPCK; /* Enable parity checking */
  105.         break;
  106.     case 'o':
  107.     case 'O':
  108.         options.c_cflag |= (PARODD | PARENB);
  109.         options.c_iflag |= INPCK; /* Disnable parity checking */
  110.         break;
  111.     case 'e':
  112.     case 'E':
  113.         options.c_cflag |= PARENB; /* Enable parity */
  114.         options.c_cflag
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值