- 论坛徽章:
- 0
|
- /*
-
本程序读取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; /* Disnable parity checking */
-
break;
-
case 'S':
-
case 's': /*as no parity*/
-
options.c_cflag &= ~PARENB;
-
options.c_cflag &= ~CSTOPB;break;
-
default:
-
fprintf(stderr,"Unsupported parity\n");
-
return (FALSE);
-
}
-
-
switch (stopbits)
-
{
-
case 1:
-
options.c_cflag &= ~CSTOPB;
-
break;
-
case 2:
-
options.c_cflag |= CSTOPB;
-
break;
-
default:
-
fprintf(stderr,"Unsupported stop bits\n");
-
return (FALSE);
-
}
-
-
/* Set input parity option */
-
if (parity != 'n')
-
options.c_iflag |= INPCK;
-
-
tcflush(fd,TCIFLUSH);
-
options.c_cc[VTIME] = 150; /* setting overtime 15 seconds*/
-
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
-
-
-
-
if (tcsetattr(fd,TCSANOW,&options) != 0)
-
{
-
perror("SetupSerial 3");
-
return (FALSE);
-
}
-
return (TRUE);
-
}
-
-
-
int openCom(char *Dev)
-
{
-
int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
-
if (-1 == fd)
-
{
-
perror("Can't Open Serial Port");
-
return -1;
-
}
-
else {
-
printf("OpenCom fd=%d\n",fd);
-
return fd;
-
}
-
}
-
-
int write_to_com(unsigned char *buffer,int buffer_length,int *written_length)
-
{
-
int fd=g_com_fd;
-
-
if (fd < 0||buffer==NULL||buffer_length<=0)
-
{
-
perror("invalid write parameters.");
-
return -1;
-
}
-
-
*written_length=write(fd,buffer,buffer_length);
-
printf("write_to_com length=%d\n",*written_length);
-
return 0;
-
-
}
-
-
-
int read_from_com(unsigned char *buffer,int read_length,int *read_len)
-
{
-
int fd=g_com_fd;
-
if (fd < 0||buffer==NULL||read_length<=0)
-
{
-
perror("invalid read parameters.");
-
return -1;
-
}
-
struct timeval tv;
-
fd_set rfds;
-
FD_ZERO(&rfds);
-
FD_SET(fd, &rfds);
-
tv.tv_sec=10;
-
tv.tv_usec=0;
-
int nread=0;
-
int needlength=read_length;
-
//while(needlength){
-
if (select(1+fd, &rfds, NULL, NULL, &tv)>0)
-
{
-
if (FD_ISSET(fd, &rfds))
-
{
-
nread=read(fd, buffer+(read_length-needlength), needlength);
-
// printf("readlength=%d\n", nread);
-
// buff[nread]='\0';
-
printf("read_from_com length=%d\n",nread);
-
needlength-=nread;
-
}
-
}
-
-
// }
-
*read_len=read_length-needlength;
-
return 0;
-
-
}
-
int closeCom(int fd)
-
{
-
close(fd);
-
}
-
-
-
-
-
-
-
/////////////////////////////////8662
-
-
-
long convert(char h[],int length)
-
{
-
int i; char c; long p=0;
-
for(i=0;i<length;i++)
-
{
-
c=h[i];
-
if(c>='0'&&c<='9') p=p*16+c-'0';
-
else if(c>='A'&&c<='F') p=p*16+10+(c-'A');
-
else if(c>='a'&&c<='f') p=p*16+10+(c-'a');
-
else printf("convert error!\n");
-
}
-
-
return(p);
-
}
-
/**
-
ret:
-
0 读到温度数据
-
-1 写或读串口数据失败
-
-2 读到数据但是不符合正确数据的格式
-
*/
-
int detect_sensors_per_channel(LTM8662 *pLTM8662){//确定哪个通道上有传感器 且 有几个$AA6
-
-
unsigned char buffer[5]={'$','0','1','6',0x0d};
-
unsigned char read_buffer[100];
-
memset(read_buffer,0,100);
-
-
int written_length=0,read_len=0,i=0;
-
int readcounts=2;
-
do{
-
if(write_to_com(buffer,5,&written_length)!=0){
-
perror("fail to write detect str.\n");
-
return -1;
-
-
}
-
//等待时间应该是多少呢?
-
sleep(2);
-
-
if(read_from_com(read_buffer,22,&read_len)!=0)
-
return -1;
-
printf("\ndetect read length:%d \n",read_len);
-
#if DEBUG_FUNCTION
-
printf("\ndetect read length:%d \n",read_len);
-
for(i=0;i<22;i++)
-
printf("%c",read_buffer[i]);
-
printf("\n");
-
-
#endif
-
if(readcounts<0)
-
return -1;
-
readcounts--;
-
-
}while(read_len<22);
-
-
if(read_len==22&&read_buffer[0]=='!'&&(read_buffer[3]>0||read_buffer[4]>0)){
-
-
for(i=0;i<8;i++){
-
#if DEBUG_FUNCTION
-
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));
-
#endif
-
pLTM8662->channel_sensors[i]=convert(read_buffer+5+i*2,2);
-
}
-
return 0;
-
}
-
-
return -2;
-
-
-
}
-
/**
-
ret:
-
0 读到温度数据
-
-1 写或读串口数据失败
-
-2 读到数据但是不符合正确数据的格式
-
*/
-
int get_xchannel_sensorid(LTM8662 *pLTM8662,char channelid){//获取x通道上各传感器的ID 通过ID来确定采取哪种温度数据解析方式&AAN
-
//&AAN
-
//>AA (ID数量)(ID)(CR)
-
int written_length=0,read_len=0,i=0,j,k;
-
unsigned char buffer[6]={38,'0','1','0',0x0d,0};
-
unsigned char read_buffer[100];
-
memset(read_buffer,0,100);
-
i=channelid;
-
-
if(pLTM8662->channel_sensors[i]>0){
-
buffer[3]=i+48;
-
#if DEBUG_FUNCTION
-
printf(" %s \n",buffer);
-
#endif
-
int readcounts=2;
-
do{
-
if(write_to_com(buffer,5,&written_length)!=0)
-
return -1;
-
-
//等待时间应该是多少呢?
-
sleep(2);
-
-
if(read_from_com(read_buffer,5,&read_len)!=0)
-
return -1;
-
#if DEBUG_FUNCTION
-
printf(" %c%c%c%x%x \n",read_buffer[0],read_buffer[1],read_buffer[2],read_buffer[3],read_buffer[4]);
-
printf("id head read_len:%d ids:%x%x \n",read_len,read_buffer[3],read_buffer[4]);
-
#endif
-
if(readcounts<0)
-
return -1;
-
readcounts--;
-
}while(read_len<5);
-
if(read_len==5&&read_buffer[0]=='>'){
-
j=read_buffer[4];
-
if(read_from_com(read_buffer,j*8+2,&read_len)!=0)
-
return -1;
-
#if DEBUG_FUNCTION
-
printf("id read content length:%d ox%x \n",read_len,read_buffer[0]);
-
#endif
-
for(k=0;k<j;k++){
-
pLTM8662->channel_sensorid[i]=read_buffer[k*8];
-
-
}
-
return 0;
-
}
-
-
return -2;
-
}
-
-
return -1;
-
-
}
-
-
int temprature_translate_28(unsigned char *temprature_hex,float *temprature_float){
-
unsigned char negative_d = 0xf8;//1111 1000 -d
-
unsigned char positive_d = 0;//0000 0000 d
-
unsigned char temp;
-
float ret=0;
-
-
temp=temprature_hex[1]&negative_d;
-
-
if(temp==negative_d){
-
-
ret=((256-temprature_hex[1])*256-temprature_hex[0])*(-0.0625);
-
*temprature_float=ret;
-
return 0;
-
-
}else if(temp==positive_d){
-
ret=((0x07&temprature_hex[1])*256+temprature_hex[0])*0.0625;
-
*temprature_float=ret;
-
return 0;
-
}else{
-
return -1;
-
}
-
}
-
-
-
int get_xchannel_temprature(LTM8662 *pLTM8662,char channelid,float * temprature_float){//获取x通道上的编号为n传感器的温度数据
-
int written_length=0,read_len=0,i=0,j,k;
-
unsigned char buffer[6]={'#','0','1','0',0x0d,0};
-
unsigned char read_buffer[100];
-
memset(read_buffer,0,100);
-
i=channelid;
-
float temp;
-
int readcounts=2;
-
if(pLTM8662->channel_sensors[i]>0){
-
-
buffer[3]=i+48;
-
#if DEBUG_FUNCTION
-
printf(" %s \n",buffer);
-
#endif
-
do{
-
if(write_to_com(buffer,5,&written_length)!=0)
-
return -1;
-
sleep(2);
-
-
-
if(read_from_com(read_buffer,5,&read_len)!=0)
-
return -1;
-
#if DEBUG_FUNCTION
-
printf(" %c%c%c%x%x \n",read_buffer[0],read_buffer[1],read_buffer[2],read_buffer[3],read_buffer[4]);
-
printf("id head read_len:%d ids:%x%x \n",read_len,read_buffer[3],read_buffer[4]);
-
#endif
-
if(readcounts<0)
-
return -1;
-
readcounts--;
-
}while(read_len<5);
-
-
if(read_len==5&&read_buffer[0]=='>'){
-
j=read_buffer[4];
-
if(read_from_com(read_buffer,j*4+2,&read_len)!=0)
-
return -1;
-
#if DEBUG_FUNCTION
-
printf("id read content length:%d ox%x \n",read_len,read_buffer[0]);
-
#endif
-
for(k=0;k<j;k++){
-
if(pLTM8662->channel_sensorid[channelid]==0x28)
-
temprature_translate_28(read_buffer,&temp);
-
-
*temprature_float=temp;
-
#if DEBUG_FUNCTION
-
printf("%d %f \n",k,temp);
-
#endif
-
}
-
-
}
-
}
-
-
return 0;
-
-
}
-
void setTermios(struct termios * pNewtio, int uBaudRate)
-
{
-
bzero(pNewtio, sizeof(struct termios)); /* clear struct for new port settings */
-
-
//8N1
-
pNewtio->c_cflag = uBaudRate | CS8 | CREAD | CLOCAL;
-
pNewtio->c_iflag = IGNPAR;
-
-
pNewtio->c_oflag = 0;
-
pNewtio->c_lflag = 0; //non ICANON
-
/*
-
initialize all control characters
-
default values can be found in /usr/include/termios.h, and
-
are given in the comments, but we don't need them here
-
*/
-
pNewtio->c_cc[VINTR] = 0; /* Ctrl-c */
-
pNewtio->c_cc[VQUIT] = 0; /* Ctrl-\ */
-
pNewtio->c_cc[VERASE] = 0; /* del */
-
pNewtio->c_cc[VKILL] = 0; /* @ */
-
pNewtio->c_cc[VEOF] = 4; /* Ctrl-d */
-
pNewtio->c_cc[VTIME] = 5; /* inter-character timer, timeout VTIME*0.1 */
-
pNewtio->c_cc[VMIN] = 0; /* blocking read until VMIN character arrives */
-
pNewtio->c_cc[VSWTC] = 0; /* '\0' */
-
pNewtio->c_cc[VSTART] = 0; /* Ctrl-q */
-
pNewtio->c_cc[VSTOP] = 0; /* Ctrl-s */
-
pNewtio->c_cc[VSUSP] = 0; /* Ctrl-z */
-
pNewtio->c_cc[VEOL] = 0; /* '\0' */
-
pNewtio->c_cc[VREPRINT] = 0; /* Ctrl-r */
-
pNewtio->c_cc[VDISCARD] = 0; /* Ctrl-u */
-
pNewtio->c_cc[VWERASE] = 0; /* Ctrl-w */
-
pNewtio->c_cc[VLNEXT] = 0; /* Ctrl-v */
-
pNewtio->c_cc[VEOL2] = 0; /* '\0' */
-
}
-
int ini_com_dev(){
-
g_com_fd=openCom(g_dev);
-
if(g_com_fd<0)
-
return -1;
-
printf("com open.\n");
-
struct termios oldtio, newtio;
-
/*set_speed(g_com_fd,9600);
-
-
if (set_Parity(g_com_fd,8,1,'N') == FALSE) {
-
printf("Set Parity Error\n");
-
return -1;
-
}
-
-
fcntl(g_com_fd, F_SETFL, FNDELAY);*/
-
-
setTermios(&newtio, B9600);
-
-
tcflush(g_com_fd, TCIFLUSH);
-
tcsetattr(g_com_fd, TCSANOW, &newtio);
-
-
-
return 0;
-
}
-
-
int ini_8662_dev(){
-
-
PLTM8662 pLTM8662=&g_ltm8662;
-
memset((char*)pLTM8662,0,sizeof(g_ltm8662));
-
char channelid;
-
-
detect_sensors_per_channel(pLTM8662);
-
-
for(channelid=0;channelid<8;channelid++){
-
if(pLTM8662->channel_sensors[channelid]>0){
-
get_xchannel_sensorid(pLTM8662,channelid);
-
}
-
}
-
-
for(channelid=0;channelid<8;channelid++){
-
printf("channel:%d sensors:%d sensorID:%x \n",channelid,pLTM8662->channel_sensors[channelid],pLTM8662->channel_sensorid[channelid]);
-
}
-
}
-
-
-
void init_sensor_module(){
-
ini_com_dev();
-
ini_8662_dev();
-
-
}
-
-
void un_init_sensor_module(){
-
closeCom(g_com_fd);
-
PLTM8662 pLTM8662=&g_ltm8662;
-
memset((char*)pLTM8662,0,sizeof(g_ltm8662));
-
}
-
-
/*
-
如果返回0 正常获取传感器值
-
如果返回1 此通道没有接入传感器
-
*/
-
int get_channel_temprature(char channelid,float *channel_temprature){
-
-
-
float temprature=0;
-
PLTM8662 pLTM8662=&g_ltm8662;
-
-
-
if(pLTM8662->channel_sensors[channelid]>0){
-
get_xchannel_temprature(pLTM8662,channelid,&temprature);
-
*channel_temprature=temprature;
-
return 0;
-
}else
-
return 1;
-
-
-
-
}
-
-
int main(void)
-
{
-
-
char channelid=0;
-
float temprature=0;
-
-
//gpio control
-
-
int gpio_fd = open("/dev/GPIO-Control",O_RDWR);
-
-
if(gpio_fd<0)
-
printf("fail to open /dev/gpios");
-
printf("turn com power on.\n");
-
ioctl(gpio_fd,4,0);
-
-
init_sensor_module();
-
printf("\ntemprature:\n");
-
for(channelid=0;channelid<8;channelid++){
-
if(get_channel_temprature(channelid,&temprature)==0)
-
printf("channel %d temprature:%f \n",channelid,temprature);
-
else
-
printf("channel %d no sensor.\n",channelid);
-
-
}
-
ioctl(gpio_fd,5,0);
-
printf("un_init_sensor_module\n");
-
un_init_sensor_module();
-
-
}
|
|