免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 980 | 回复: 0
打印 上一主题 下一主题

LTM8000温度模块源码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-22 08:51 |只看该作者 |倒序浏览
  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 &= ~PARODD;
  115.         options.c_iflag |= INPCK; /* Disnable parity checking */
  116.         break;
  117.     case 'S':
  118.     case 's': /*as no parity*/
  119.         options.c_cflag &= ~PARENB;
  120.         options.c_cflag &= ~CSTOPB;break;
  121.     default:
  122.         fprintf(stderr,"Unsupported parity\n");
  123.         return (FALSE);
  124.     }
  125.  
  126.     switch (stopbits)
  127.     {
  128.     case 1:
  129.         options.c_cflag &= ~CSTOPB;
  130.         break;
  131.     case 2:
  132.         options.c_cflag |= CSTOPB;
  133.         break;
  134.     default:
  135.         fprintf(stderr,"Unsupported stop bits\n");
  136.         return (FALSE);
  137.     }
  138.  
  139.     /* Set input parity option */
  140.     if (parity != 'n')
  141.         options.c_iflag |= INPCK;
  142.  
  143.     tcflush(fd,TCIFLUSH);
  144.     options.c_cc[VTIME] = 150; /* setting overtime 15 seconds*/
  145.     options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
  146.  
  147.  
  148.  
  149.     if (tcsetattr(fd,TCSANOW,&options) != 0)
  150.     {
  151.         perror("SetupSerial 3");
  152.         return (FALSE);
  153.     }
  154.     return (TRUE);
  155. }
  156.  
  157.  
  158. int openCom(char *Dev)
  159. {
  160.     int    fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY    
  161.     if (-1 == fd)    
  162.     {             
  163.         perror("Can't Open Serial Port");
  164.         return -1;        
  165.     }    
  166.     else    {
  167.         printf("OpenCom fd=%d\n",fd);
  168.         return fd;
  169.     }
  170. }
  171.  
  172. int write_to_com(unsigned char *buffer,int buffer_length,int *written_length)
  173. {
  174.     int fd=g_com_fd;
  175.  
  176.     if (fd < 0||buffer==NULL||buffer_length<=0)    
  177.     {             
  178.         perror("invalid write parameters.");
  179.         return -1;        
  180.     }
  181.  
  182.     *written_length=write(fd,buffer,buffer_length);
  183.     printf("write_to_com length=%d\n",*written_length);
  184.     return 0;
  185.  
  186. }
  187.  
  188.  
  189. int read_from_com(unsigned char *buffer,int read_length,int *read_len)
  190. {
  191.     int fd=g_com_fd;
  192.     if (fd < 0||buffer==NULL||read_length<=0)    
  193.     {             
  194.         perror("invalid read parameters.");
  195.         return -1;        
  196.     }
  197.      struct timeval tv;
  198.      fd_set rfds;
  199.      FD_ZERO(&rfds);
  200.      FD_SET(fd, &rfds);
  201.     tv.tv_sec=10;
  202.     tv.tv_usec=0;
  203.     int nread=0;
  204.     int needlength=read_length;
  205.     //while(needlength){
  206.        if (select(1+fd, &rfds, NULL, NULL, &tv)>0)
  207.        {
  208.            if (FD_ISSET(fd, &rfds))
  209.            {
  210.               nread=read(fd, buffer+(read_length-needlength), needlength);
  211.              // printf("readlength=%d\n", nread);
  212.              // buff[nread]='\0';
  213.               printf("read_from_com length=%d\n",nread);
  214.              needlength-=nread;
  215.            }
  216.        }
  217.  
  218.     // }
  219.     *read_len=read_length-needlength;
  220.     return 0;
  221.  
  222. }
  223. int closeCom(int fd)
  224. {
  225.     close(fd);
  226. }
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233. /////////////////////////////////8662
  234.  
  235.  
  236. long convert(char h[],int length)
  237. {
  238.     int i; char c; long p=0;
  239.     for(i=0;i<length;i++)
  240.     {
  241.         c=h[i];
  242.         if(c>='0'&&c<='9') p=p*16+c-'0';
  243.         else if(c>='A'&&c<='F') p=p*16+10+(c-'A');
  244.         else if(c>='a'&&c<='f') p=p*16+10+(c-'a');
  245.         else printf("convert error!\n");
  246.     }
  247.  
  248.     return(p);
  249. }
  250. /**
  251.  ret:
  252.  0 读到温度数据
  253.  -1 写或读串口数据失败
  254.  -2 读到数据但是不符合正确数据的格式
  255. */
  256. int detect_sensors_per_channel(LTM8662 *pLTM8662){//确定哪个通道上有传感器 且 有几个$AA6
  257.  
  258.     unsigned char buffer[5]={'$','0','1','6',0x0d};
  259.     unsigned char read_buffer[100];
  260.     memset(read_buffer,0,100);
  261.  
  262.     int written_length=0,read_len=0,i=0;
  263.     int readcounts=2;
  264. do{    
  265.     if(write_to_com(buffer,5,&written_length)!=0){
  266.         perror("fail to write detect str.\n");
  267.         return -1;
  268.  
  269.     }
  270.     //等待时间应该是多少呢?
  271.     sleep(2);
  272.  
  273.     if(read_from_com(read_buffer,22,&read_len)!=0)
  274.         return -1;
  275.     printf("\ndetect read length:%d \n",read_len);    
  276. #if DEBUG_FUNCTION
  277.     printf("\ndetect read length:%d \n",read_len);
  278.     for(i=0;i<22;i++)
  279.     printf("%c",read_buffer[i]);
  280.     printf("\n");
  281.  
  282. #endif
  283.     if(readcounts<0)
  284.         return -1;
  285.     readcounts--;
  286.  
  287. }while(read_len<22);    
  288.  
  289.     if(read_len==22&&read_buffer[0]=='!'&&(read_buffer[3]>0||read_buffer[4]>0)){
  290.  
  291.         for(i=0;i<8;i++){
  292. #if DEBUG_FUNCTION
  293.             printf("channel: %d sensor:%c%c %d\n",i,read_buffer[5+i*2],read_buffer[5+i*2+1],convert(read_buffer+5+i*2,2));
  294. #endif    
  295.             pLTM8662->channel_sensors[i]=convert(read_buffer+5+i*2,2);
  296.         }
  297.         return 0;
  298.     }
  299.  
  300.     return -2;
  301.  
  302.  
  303. }
  304. /**
  305.  ret:
  306.  0 读到温度数据
  307.  -1 写或读串口数据失败
  308.  -2 读到数据但是不符合正确数据的格式
  309. */
  310. int get_xchannel_sensorid(LTM8662 *pLTM8662,char channelid){//获取x通道上各传感器的ID 通过ID来确定采取哪种温度数据解析方式&AAN
  311.     //&AAN
  312.     //>AA (ID数量)(ID)(CR)
  313.     int written_length=0,read_len=0,i=0,j,k;
  314.     unsigned char buffer[6]={38,'0','1','0',0x0d,0};
  315.     unsigned char read_buffer[100];
  316.     memset(read_buffer,0,100);
  317.     i=channelid;
  318.  
  319.     if(pLTM8662->channel_sensors[i]>0){
  320.         buffer[3]=i+48;
  321. #if DEBUG_FUNCTION        
  322.         printf(" %s \n",buffer);
  323. #endif    
  324. int readcounts=2;
  325. do{    
  326.         if(write_to_com(buffer,5,&written_length)!=0)
  327.             return -1;
  328.  
  329.     //等待时间应该是多少呢?
  330.     sleep(2);
  331.  
  332.         if(read_from_com(read_buffer,5,&read_len)!=0)
  333.             return -1;
  334. #if DEBUG_FUNCTION
  335.         printf(" %c%c%c%x%x \n",read_buffer[0],read_buffer[1],read_buffer[2],read_buffer[3],read_buffer[4]);
  336.         printf("id head read_len:%d ids:%x%x \n",read_len,read_buffer[3],read_buffer[4]);
  337. #endif    
  338. if(readcounts<0)
  339.         return -1;
  340.     readcounts--;
  341. }while(read_len<5);
  342.         if(read_len==5&&read_buffer[0]=='>'){
  343.             j=read_buffer[4];
  344.             if(read_from_com(read_buffer,j*8+2,&read_len)!=0)
  345.                 return -1;
  346. #if DEBUG_FUNCTION            
  347.             printf("id read content length:%d ox%x \n",read_len,read_buffer[0]);
  348. #endif
  349.             for(k=0;k<j;k++){
  350.                 pLTM8662->channel_sensorid[i]=read_buffer[k*8];
  351.  
  352.             }
  353.                 return 0;
  354.         }
  355.  
  356.         return -2;
  357.     }
  358.  
  359.     return -1;
  360.  
  361. }
  362.  
  363. int temprature_translate_28(unsigned char *temprature_hex,float *temprature_float){
  364.     unsigned char negative_d = 0xf8;//1111 1000 -d
  365.     unsigned char positive_d = 0;//0000 0000 d
  366.     unsigned char temp;
  367.     float ret=0;
  368.  
  369.     temp=temprature_hex[1]&negative_d;
  370.  
  371.     if(temp==negative_d){
  372.  
  373.         ret=((256-temprature_hex[1])*256-temprature_hex[0])*(-0.0625);
  374.         *temprature_float=ret;
  375.         return 0;
  376.  
  377.     }else if(temp==positive_d){
  378.         ret=((0x07&temprature_hex[1])*256+temprature_hex[0])*0.0625;
  379.         *temprature_float=ret;
  380.         return 0;
  381.     }else{
  382.         return -1;
  383.     }
  384. }
  385.  
  386.  
  387. int get_xchannel_temprature(LTM8662 *pLTM8662,char channelid,float * temprature_float){//获取x通道上的编号为n传感器的温度数据
  388.     int written_length=0,read_len=0,i=0,j,k;
  389.     unsigned char buffer[6]={'#','0','1','0',0x0d,0};
  390.     unsigned char read_buffer[100];
  391.     memset(read_buffer,0,100);
  392.     i=channelid;
  393.     float temp;
  394. int readcounts=2;
  395.     if(pLTM8662->channel_sensors[i]>0){
  396.  
  397.         buffer[3]=i+48;
  398. #if DEBUG_FUNCTION        
  399.         printf(" %s \n",buffer);
  400. #endif    
  401. do{    
  402.         if(write_to_com(buffer,5,&written_length)!=0)
  403.             return -1;
  404.         sleep(2);
  405.  
  406.  
  407.         if(read_from_com(read_buffer,5,&read_len)!=0)
  408.             return -1;
  409. #if DEBUG_FUNCTION
  410.         printf(" %c%c%c%x%x \n",read_buffer[0],read_buffer[1],read_buffer[2],read_buffer[3],read_buffer[4]);
  411.         printf("id head read_len:%d ids:%x%x \n",read_len,read_buffer[3],read_buffer[4]);
  412. #endif
  413. if(readcounts<0)
  414.         return -1;
  415.     readcounts--;
  416. }while(read_len<5);
  417.  
  418.         if(read_len==5&&read_buffer[0]=='>'){
  419.             j=read_buffer[4];
  420.             if(read_from_com(read_buffer,j*4+2,&read_len)!=0)
  421.                 return -1;
  422. #if DEBUG_FUNCTION            
  423.             printf("id read content length:%d ox%x \n",read_len,read_buffer[0]);
  424. #endif
  425.             for(k=0;k<j;k++){
  426.                 if(pLTM8662->channel_sensorid[channelid]==0x28)
  427.                 temprature_translate_28(read_buffer,&temp);
  428.  
  429.                 *temprature_float=temp;
  430. #if DEBUG_FUNCTION
  431.                 printf("%d %f \n",k,temp);
  432. #endif
  433.             }
  434.  
  435.         }
  436.     }
  437.  
  438.     return 0;
  439.  
  440. }
  441. void setTermios(struct termios * pNewtio, int uBaudRate)
  442. {
  443.     bzero(pNewtio, sizeof(struct termios)); /* clear struct for new port settings */
  444.  
  445.     //8N1
  446.     pNewtio->c_cflag = uBaudRate | CS8 | CREAD | CLOCAL;
  447.     pNewtio->c_iflag = IGNPAR;
  448.  
  449.     pNewtio->c_oflag = 0;
  450.     pNewtio->c_lflag = 0; //non ICANON
  451.     /*
  452.      initialize all control characters
  453.      default values can be found in /usr/include/termios.h, and
  454.      are given in the comments, but we don't need them here
  455.      */
  456.     pNewtio->c_cc[VINTR] = 0; /* Ctrl-c */
  457.     pNewtio->c_cc[VQUIT] = 0; /* Ctrl-\ */
  458.     pNewtio->c_cc[VERASE] = 0; /* del */
  459.     pNewtio->c_cc[VKILL] = 0; /* @ */
  460.     pNewtio->c_cc[VEOF] = 4; /* Ctrl-d */
  461.     pNewtio->c_cc[VTIME] = 5; /* inter-character timer, timeout VTIME*0.1 */
  462.     pNewtio->c_cc[VMIN] = 0; /* blocking read until VMIN character arrives */
  463.     pNewtio->c_cc[VSWTC] = 0; /* '\0' */
  464.     pNewtio->c_cc[VSTART] = 0; /* Ctrl-q */
  465.     pNewtio->c_cc[VSTOP] = 0; /* Ctrl-s */
  466.     pNewtio->c_cc[VSUSP] = 0; /* Ctrl-z */
  467.     pNewtio->c_cc[VEOL] = 0; /* '\0' */
  468.     pNewtio->c_cc[VREPRINT] = 0; /* Ctrl-r */
  469.     pNewtio->c_cc[VDISCARD] = 0; /* Ctrl-u */
  470.     pNewtio->c_cc[VWERASE] = 0; /* Ctrl-w */
  471.     pNewtio->c_cc[VLNEXT] = 0; /* Ctrl-v */
  472.     pNewtio->c_cc[VEOL2] = 0; /* '\0' */
  473. }
  474. int ini_com_dev(){
  475.     g_com_fd=openCom(g_dev);
  476.     if(g_com_fd<0)
  477.         return -1;
  478.     printf("com open.\n");
  479.     struct termios oldtio, newtio;
  480.     /*set_speed(g_com_fd,9600);
  481.  
  482.     if (set_Parity(g_com_fd,8,1,'N') == FALSE) {
  483.         printf("Set Parity Error\n");
  484.         return -1;
  485.     }
  486.  
  487.     fcntl(g_com_fd, F_SETFL, FNDELAY);*/
  488.  
  489.     setTermios(&newtio, B9600);
  490.  
  491.     tcflush(g_com_fd, TCIFLUSH);
  492.     tcsetattr(g_com_fd, TCSANOW, &newtio);
  493.  
  494.  
  495.     return 0;
  496. }
  497.  
  498. int ini_8662_dev(){
  499.  
  500.     PLTM8662 pLTM8662=&g_ltm8662;
  501.     memset((char*)pLTM8662,0,sizeof(g_ltm8662));
  502.     char channelid;
  503.  
  504.     detect_sensors_per_channel(pLTM8662);
  505.  
  506.     for(channelid=0;channelid<8;channelid++){
  507.         if(pLTM8662->channel_sensors[channelid]>0){
  508.             get_xchannel_sensorid(pLTM8662,channelid);
  509.         }
  510.     }
  511.  
  512.     for(channelid=0;channelid<8;channelid++){
  513.         printf("channel:%d sensors:%d sensorID:%x \n",channelid,pLTM8662->channel_sensors[channelid],pLTM8662->channel_sensorid[channelid]);
  514.     }
  515. }
  516.  
  517.  
  518. void init_sensor_module(){
  519.     ini_com_dev();
  520.     ini_8662_dev();
  521.  
  522. }
  523.  
  524. void un_init_sensor_module(){
  525.     closeCom(g_com_fd);
  526.     PLTM8662 pLTM8662=&g_ltm8662;
  527.     memset((char*)pLTM8662,0,sizeof(g_ltm8662));
  528. }
  529.  
  530. /*
  531.         如果返回0 正常获取传感器值
  532.         如果返回1 此通道没有接入传感器
  533. */
  534. int get_channel_temprature(char channelid,float *channel_temprature){
  535.  
  536.  
  537.     float temprature=0;    
  538.     PLTM8662 pLTM8662=&g_ltm8662;
  539.  
  540.  
  541.         if(pLTM8662->channel_sensors[channelid]>0){
  542.             get_xchannel_temprature(pLTM8662,channelid,&temprature);
  543.             *channel_temprature=temprature;
  544.             return 0;
  545.         }else
  546.             return 1;
  547.  
  548.  
  549.  
  550. }
  551.  
  552. int main(void)
  553. {
  554.  
  555.     char channelid=0;
  556.     float temprature=0;
  557.  
  558.     //gpio control
  559.  
  560.     int gpio_fd = open("/dev/GPIO-Control",O_RDWR);
  561.  
  562.     if(gpio_fd<0)    
  563.         printf("fail to open /dev/gpios");    
  564.     printf("turn com power on.\n");    
  565.     ioctl(gpio_fd,4,0);
  566.  
  567.     init_sensor_module();
  568.     printf("\ntemprature:\n");
  569.     for(channelid=0;channelid<8;channelid++){
  570.             if(get_channel_temprature(channelid,&temprature)==0)
  571.             printf("channel %d temprature:%f \n",channelid,temprature);
  572.             else
  573.                 printf("channel %d no sensor.\n",channelid);
  574.  
  575.     }
  576.     ioctl(gpio_fd,5,0);
  577.     printf("un_init_sensor_module\n");
  578.     un_init_sensor_module();
  579.  
  580. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP