- /*
- 本程序读取ltm8662模块的数据
-
- 因目前系统中只有一个模块 故模块地址默认为 00
-
- 程序只能支持每个通道接一个传感器的情况
-
- 只支持ID为0x28的温度传感器数据格式 如有需要自己添加新的数据格式解析程序
-
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <memory.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <errno.h>
-
- #define FALSE -1
- #define TRUE 1
-
- #define DEBUG_FUNCTION 1
-
- typedef struct ltm8662{
- char channel_sensors[8];//每个通道上传感器的数量 最多64个
- char channel_sensorid[8];//每个通道上传感器的ID 依此来解析数据 实际上应该是8×64 现在每个通道只接一个传感器只设为1
-
- }LTM8662;
-
- typedef LTM8662* PLTM8662;
-
-
-
- /////添加三个静态全局变量
- static char *g_dev = "/dev/tq2440_serial1";
- static int g_com_fd=-1;
- static LTM8662 g_ltm8662;
-
- ///////////////////////////////////////////com
- /**
- *@brief setting serial com speed
- *@param fd int handle of com
- *@param speed int
- *@return void
- */
- int speed_arr[] = {
B38400, B19200, B9600, B4800, B2400, B1200, B300,
- B38400, B19200, B9600, B4800, B2400, B1200, B300, };
- int name_arr[] = {
38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
- 19200, 9600, 4800, 2400, 1200, 300, };
- void set_speed(int fd, int speed)
- {
- int i;
- int status;
- struct termios Opt;
- tcgetattr(fd, &Opt);
- for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {
- if (speed == name_arr[i]) {
- tcflush(fd, TCIOFLUSH);
- cfsetispeed(&Opt, speed_arr[i]);
- cfsetospeed(&Opt, speed_arr[i]);
- status = tcsetattr(fd, TCSANOW, &Opt);
- if (status != 0) {
- perror("tcsetattr fd");
- return;
- }
- tcflush(fd,TCIOFLUSH);
- }
- }
- }
-
- /*
- *@brief setting databits,stopbits,parity
- *@param fd int handle of device
- *@param databits int value is 7 or 8
- *@param stopbits int value is 1 or 2
- *@param parity int value is N,E,O,,S
- */
- int set_Parity(int fd,int databits,int stopbits,int parity)
- {
- struct termios options;
- if ( tcgetattr( fd,&options) != 0) {
- perror("SetupSerial 1");
- return(FALSE);
- }
- options.c_cflag &= ~CSIZE;
- switch (databits)
- {
- case 7:
- options.c_cflag |= CS7;
- break;
- case 8:
- options.c_cflag |= CS8;
- break;
- default:
- fprintf(stderr,"Unsupported data size\n"); return (FALSE);
- }
- switch (parity)
- {
- case 'n':
- case 'N':
- options.c_cflag &= ~PARENB; /* Clear parity enable */
- options.c_iflag &= ~INPCK; /* Enable parity checking */
- break;
- case 'o':
- case 'O':
- options.c_cflag |= (PARODD | PARENB);
- options.c_iflag |= INPCK; /* Disnable parity checking */
- break;
- case 'e':
- case 'E':
- options.c_cflag |= PARENB; /* Enable parity */
- options.c_cflag &= ~PARODD;
- options.c_iflag |= INPCK; /*
LTM8000温度模块源码
最新推荐文章于 2025-07-18 14:46:05 发布