本帖最后由 zyy83 于 2011-5-8 21:03 编辑
为什么温湿度采集后显示总是溢出呢?源码如下:
#include "toolbox.h"
//#include <utility.h>
#include <ansi_c.h>
#include<windows.h>
#include <rs232.h>
#include <cvirte.h>
#include <userint.h>
#include "panel.h"
#include "UPS.h"
#include <utility.h>
//#include "m7.h"
#define TRUE 1
#define FALSE 0
BOOL OpenCom1_Flag=TRUE;
extern BOOL show_par_chart;
/*产生CRC校验*/
unsigned int crc=0xffff;/*CRC校验*/
extern int UPS_start_flag;
extern int panelHandle, ups_panel, ups_panel_chart;
extern HANDLE poolHandle;
extern int Com1,Com2;
extern int flag_THD_Start;
extern char ups_analogy_state[13][100];
extern char ups_analogy_name[13][50];
extern double ac_input_volt,ac_output_volt,dc_input_volt,ac_output_current,ac_input_freq,output_freq; //码表图需要参数
void calccrc(BYTE *crcbuf,int n);
int CVICALLBACK MyThreadFunction();
//extern int Com_On_Off; //串口打开标志
int DataAcqThreadFunction(void);
void showtime(void);//显示时间
void decode_rec_buf(unsigned char *rec_buf); //模拟参数返回值解码函数
float temp=0;
float humd=0;
char tempe[100];
char humid[100];
int CVICALLBACK MyThreadFunction()
{
unsigned char dataa[100]; //温湿度
unsigned char digcr[100]="";
unsigned char digcr1[100]="";
unsigned int temp_crc=0; //根据UPS返回计算的CRC值
unsigned int rec_crc=0; //接收到的从端CRC
int i;
long int digital;
long int digital1;
int flag1=0; // 温湿度标志位
//int num=0;
//OpenComConfig(Com1, "", 9600, 0, 8, 1, 512, 512);
if(OpenComConfig(Com1, "", 9600, 0, 8, 1, 512, 512)<0) //测试时候将其注释 ren 2007-10-29
{
OpenCom1_Flag=FALSE;
//MessagePopup ("警告!", "无法打开该串口设备!"); // ren 2007-10-29
flag_THD_Start=0;
}
else
{
OpenCom1_Flag=TRUE;
}
while(flag_THD_Start)
{//++++
//SetSleepPolicy (VAL_SLEEP_MORE);
////////////////////////////////////////////////////////////////////////
Sleep(100);
//获得温湿度
FlushInQ(Com1);
ComWrtByte (Com1,0x41 ); /*对应地址0x01*/
for (i=0;i<10;i++)
dataa='\0';
Delay(0.5); ///////////ren
ComRd (Com1,dataa ,9 );
for (i=0;i<10;i++)
{
if((dataa!=0)&&(dataa!='0'))
{
flag1=1;
break;
}
else
flag1=0;
}
//////////////////////////////////////////////////////////////////
//===============================================================
if (flag1==0)
{
ComWrtByte (Com1,0x41 ); /*对应地址0x01*/
for (i=0;i<10;i++)
dataa='\0';
Delay(0.5); ///////////ren
ComRd (Com1,dataa ,9 );
for (i=0;i<10;i++)
{
if((dataa!=0)&&(dataa!='0'))
{
flag1=1;
break;
}
else
flag1=0;
}
}//if
//===============================================================
flag1=1;/////////////////////////////////////////////////////// ren
//===============================================================
if(flag1==0)
{
//flag_THD_Start=0;
SetCtrlVal (panelHandle, PANEL_TEXTMSG_TEMP, "请检查线路是否连接正常");
SetCtrlAttribute (panelHandle, PANEL_TEXTMSG_TEMP, ATTR_TEXT_COLOR, VAL_RED);
}
else
{
SetCtrlVal (panelHandle, PANEL_TEXTMSG_TEMP, "温湿度测量正常");
SetCtrlAttribute (panelHandle, PANEL_TEXTMSG_TEMP, ATTR_TEXT_COLOR, VAL_BLUE);
if((dataa[3]&0x80)==0x00)
temp=(dataa[3]*256+dataa[4])/10.0;/*得到温度值*/
else
temp=-1*((~dataa[3])*256+(~dataa[4]))/10.0;
humd=(dataa[5]*256+dataa[6])/10.0;/*得到湿度值*/
sprintf(tempe,"%5.2f",temp);
strcat(tempe,"℃");
SetCtrlAttribute(panelHandle,PANEL_TEMPERATURE,ATTR_VISIBLE,1);
SetCtrlVal(panelHandle,PANEL_TEMPERATURE,tempe);
sprintf(humid,"%5.2f",humd);
strcat(humid,"% ");
SetCtrlAttribute(panelHandle,PANEL_HUMIDITY,ATTR_VISIBLE,1);
SetCtrlVal(panelHandle,PANEL_HUMIDITY,humid);
}
|