⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bms1.c

📁 基于Labwindows的电池故障诊断系统
💻 C
字号:
#include <windows.h>
#include <ansi_c.h>
#include <formatio.h>
#include <utility.h>
#include <rs232.h>
#include <cvirte.h>		
#include <userint.h>
#include <easytab.h> 
#include "bms1.h"

#define	package_receive_size  11
#define	package_send_size  4 

static int 		panelHandle		= 0;
static int 		ConfigHandle	= 0;
static int 		collectionHandle= 0;
static int     	tsqHandle ;

struct Package_Send
{
	BYTE address;
	BYTE signal;
	BYTE life_high;
	BYTE life_low;
}  Command_send;

BYTE *Command; 

struct Package_receive
{
	unsigned short int address;
	BYTE datalength;
	BYTE DATA[8];
} package_receive[2];


unsigned short int	cycle = 0;
int     TabControl=0;                   
int     ControlPoint;
int     CurrentControlMode;

static 	int 	CVICALLBACK AcquireDataThread (void *unused);  
static int 		AcquireDataThreadId = 0; 
HANDLE 			CommandStoreIsOK, ExitEvent;    

static int InitApplication (void)
{
	int CmtStatus;
	
	/*  Turn off Hardware handshaking (loopback test will not function with it on) */	
    if (SetCTSMode (1, LWRS_HWHANDSHAKE_OFF) < 0)return -1;
    if (SetXMode (1, 0) < 0)return -1;
    
    /*  Make sure Serial buffers are empty */                                                         
    if (FlushInQ (1) < 0)return -1;
    if (FlushOutQ (1) < 0)return -1;
    
    
	CommandStoreIsOK = CreateEvent(NULL, FALSE, FALSE, NULL);
	if( CommandStoreIsOK == NULL)return -1;	
	
	
	
	ExitEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
	if( ExitEvent == NULL)return -1;
										 
	/* Install a callback to acquire and plot the acquired data. */ 
	CmtStatus = CmtScheduleThreadPoolFunctionAdv (DEFAULT_THREAD_POOL_HANDLE,
												  AcquireDataThread,
												  NULL,
												  THREAD_PRIORITY_HIGHEST,
												  NULL,
												  0,
												  NULL,
												  0,
												  &AcquireDataThreadId
												  );
	if( CmtStatus <0)return -1;
	
   /*  CmtStatus = CmtInstallTSQCallback (tsqHandle, 
            			               EVENT_TSQ_ITEMS_IN_QUEUE,
                                       NUM_ITEMS_IN_QUEUE_READ_BLOCK,
                                       PlotStripchartCallback,
                                       NULL,
                                       CmtGetCurrentThreadID(),  
                                       &PlotStripchartCallbackId);
	if( CmtStatus <0)return -1;					 */
	
    return 0;
}

int main (int argc, char *argv[])
{
	if (InitCVIRTE (0, argv, 0) == 0)
		return -1;	/* out of memory */
	if ((panelHandle = LoadPanel (0, "bms1.uir", PANEL)) < 0)
		return -1;
		
	TabControl = EasyTab_ConvertFromCanvas(panelHandle,PANEL_CANVAS);
	EasyTab_SetAttribute (panelHandle,TabControl,ATTR_EASY_TAB_ORIENTATION, VAL_EASY_TAB_BTNS_ON_BOTTOM);
	
	EasyTab_LoadPanels (panelHandle,TabControl,1,"bms1.uir",__CVIUserHInst,collection,&collectionHandle,Config,&ConfigHandle,0);
																						 
																								 
	if (OpenComConfig (1, "COM1",9600, 0, 8, 1, 400, 400) <0) return -1;
	SetCtrlAttribute (panelHandle, PANEL_TIMER , ATTR_ENABLED , 0);
	
	InitApplication(); 
		
	DisplayPanel (panelHandle);
	RunUserInterface ();
	DiscardPanel (panelHandle);
	
	
	return 0;
}

static int CVICALLBACK AcquireDataThread (void *unused)
{
    int    	ReadBytes, FreeSpace, index=0,j=0;
    int    	dwHandleSignaled,DataFileHandle =-1, CommandFileHandle =-1;
    HANDLE 	HandlesToWaitFor[2];
    int		TableIndex=1,NumberOfRows=0;
    BYTE    ReplyMsg[22] = {0};
    
    float volt_aver, volt_min, volt_max, temperature,   temp_min, temp_max;
    int	  false;
    BYTE	DATA[2];
    float	pMsg[15] = {0} ;
    BYTE   	*WritePtr = NULL ;
 
    HandlesToWaitFor[0] = ExitEvent;
    HandlesToWaitFor[1] = CommandStoreIsOK;
    FlushInQ (1);
    
    while (1)
    {   
    	 
    	dwHandleSignaled = WaitForMultipleObjects(2, HandlesToWaitFor, FALSE, INFINITE);
    	
    		switch(dwHandleSignaled)
			{
			case WAIT_OBJECT_0:     // CloseEvent signaled!
				 
			     return 0;
			
			case WAIT_OBJECT_0 + 1: // DataStoreIsOK Wait finished.
    		
				 ReadBytes = GetInQLen(1);
				 ComRd(1, ReplyMsg, ReadBytes);
				
				  
				 
				 if(ReadBytes > package_receive_size*2 )
				 {
				 	FlushInQ (1);
				 	MessagePopup ("注意", "读数据错误!请重新设置。");
				 	continue;
				 }
				 
				 else if(ReadBytes == package_receive_size*2)
				 {
				 	
				 /*	if (CmtGetTSQWritePtr(tsqHandle, &WritePtr, &FreeSpace) >= 0)
    						{
    							if(FreeSpace >0)memcpy(WritePtr, pMsg, package_receive_size*2);
    							CmtReleaseTSQWritePtr (tsqHandle, package_receive_size*2);
    						}		*/
    				
    				
    				
				 	
    				index = index+2; 
    				
				 	
				 	//SetCtrlVal(panelHandle, PANEL_address_1, 	ReplyMsg[index++]);
				 	GetNumTableRows (collectionHandle,collection_CurrentTable,&NumberOfRows);   
				 	if(TableIndex>NumberOfRows)
				 	{
				 		InsertTableRows (collectionHandle,collection_CurrentTable,-1,1,VAL_CELL_NUMERIC); 
				 		//TableIndex = 1;
				 	 }
				 //	for(TableIndex=1; TableIndex<=NumberOfRows; TableIndex++)
				 //	{
				 	
				 	SetTableCellVal (collectionHandle,collection_CurrentTable, MakePoint (1,TableIndex),(float)ReplyMsg[index++]);
				 	
				 //	pMsg = &ReplyMsg[index]; 
				
				    pMsg[0] = (float)ReplyMsg[index++];
				    pMsg[1] = (float)ReplyMsg[index++];
				 	volt_aver =  (pMsg[0]*16*16+pMsg[1])*2.5/4096;
				 	SetTableCellVal (collectionHandle,collection_CurrentTable, MakePoint (2,TableIndex),volt_aver); 
				 	
				
				 	pMsg[2] = (float)ReplyMsg[index++];
				    pMsg[3] = (float)ReplyMsg[index++];
				 	
				 	volt_max =  (pMsg[2]*16*16+pMsg[3])*2.5/4096;    
				 	SetTableCellVal (collectionHandle,collection_CurrentTable, MakePoint (3,TableIndex),volt_max); 
				 	
				 	
				 
				    pMsg[4] = (float)ReplyMsg[index++];
				    pMsg[5] = (float)ReplyMsg[index++];
				 	volt_min = (pMsg[4]*16*16+pMsg[5])*2.5/4096;
				 	SetTableCellVal (collectionHandle,collection_CurrentTable, MakePoint (4,TableIndex),volt_min); 
				 	
				 	
				
				 	pMsg[6] = (float)ReplyMsg[index++];
				    pMsg[7] = (float)ReplyMsg[index++];
				 	temperature = (pMsg[6]*16*16+pMsg[7])/10; 
				 	SetTableCellVal (collectionHandle,collection_CurrentTable, MakePoint (5,TableIndex),temperature); 
				 	
				 	
				 	index = index+2;
				 	//SetCtrlVal(panelHandle,  PANEL_address_2, 	pMsg[8] = ReplyMsg[index++]);
				 	//pMsg[8] = (float)ReplyMsg[index++];
				 	SetTableCellVal (collectionHandle,collection_CurrentTable, MakePoint (6,TableIndex),(float)ReplyMsg[index++]);//pMsg[8]
				
				 	
				 	pMsg[9] = (float)ReplyMsg[index++];
				    pMsg[10] = (float)ReplyMsg[index++];
				 	temp_max = (pMsg[9]*16*16+pMsg[10])/10;  
				 	SetTableCellVal (collectionHandle,collection_CurrentTable, MakePoint (7,TableIndex),temp_max);
				 	
				 	
				 	
				 	pMsg[11] = (float)ReplyMsg[index++];
				    pMsg[12] = (float)ReplyMsg[index++];
				 	temp_min = (pMsg[11]*16*16+pMsg[12])/10;
				 	SetTableCellVal (collectionHandle,collection_CurrentTable, MakePoint (8,TableIndex),temp_min);
				 	
				 
				 	DATA[0] = ReplyMsg[index++];
				    DATA[1] = ReplyMsg[index++];
				    false = DATA[0]<<8 + DATA[1];
				    if(false == 0x00)
				       SetCtrlVal(panelHandle, PANEL_LED_21, 1);
				    else 
				     {
				     	SetCtrlVal(panelHandle, PANEL_LED_22, 1); 	
				 		if(false == 0x601)
				 	   		SetCtrlVal(panelHandle, PANEL_LED, 1); 
				 		else if(false = 0x701)
				 			SetCtrlVal(panelHandle, PANEL_LED_2, 1);  	
				 		else if(false == 0xA01)
				 			SetCtrlVal(panelHandle, PANEL_LED_3, 1);  
				 		else if(false == 0x602)
				 			SetCtrlVal(panelHandle, PANEL_LED_4, 1); 
				 		else if(false == 0xd02)
				 			SetCtrlVal(panelHandle, PANEL_LED_5, 1);
				 		else if(false == 0x702)
				 		 	SetCtrlVal(panelHandle, PANEL_LED_6, 1);
				 		else if(false == 0xb02)
				 		 	SetCtrlVal(panelHandle, PANEL_LED_7, 1);
				 		else if(false == 0xa02)
						  	SetCtrlVal(panelHandle, PANEL_LED_8, 1);
						else if(false == 0x603)
						 	SetCtrlVal(panelHandle, PANEL_LED_9, 1); 
				 		else if(false == 0xd03)
				 			SetCtrlVal(panelHandle, PANEL_LED_10, 1);
				 		else if(false == 0x703)
				 		 	SetCtrlVal(panelHandle, PANEL_LED_11, 1);
				 		else if(false == 0xb03)
				 		 	SetCtrlVal(panelHandle, PANEL_LED_12, 1);
				 		else if(false == 0xa03)
						  	SetCtrlVal(panelHandle, PANEL_LED_13, 1);
						else if(false == 0x604)
						 	SetCtrlVal(panelHandle, PANEL_LED_14, 1); 
				 		else if(false == 0xd04)
				 			SetCtrlVal(panelHandle, PANEL_LED_15, 1);
				 		else if(false == 0x704)
				 		 	SetCtrlVal(panelHandle, PANEL_LED_16, 1);
				 		else if(false == 0xb04)
				 		 	SetCtrlVal(panelHandle, PANEL_LED_17, 1);
				 		else if(false == 0xa04)
						  	SetCtrlVal(panelHandle, PANEL_LED_18, 1);
						  	}
					//SetTableCellVal (collectionHandle,collection_CurrentTable, MakePoint (9,TableIndex),false);
				 	
				 	//SetCtrlVal(panelHandle, PANEL_houbu1, 		pMsg[15] = ReplyMsg[index++]);     
				 	//SetCtrlVal(panelHandle, PANEL_houbu2, 		pMsg[16] = ReplyMsg[index++]);
				 	TableIndex++;
				 
					cycle--;
					for(index=0; index<15; index++)
						ReplyMsg[index] = 0;
					index=0;
					}
			      //	TableIndex = 0;
				//	}
				
				else ; 
			
			
			}
	
	}
		  return 0;
  }  			
    	
  

int CVICALLBACK set_command (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{

	
	switch (event)
		{
		case EVENT_COMMIT:
			GetCtrlVal (ConfigHandle,  Config_address, 		&Command_send.address);
		    GetCtrlVal (ConfigHandle,  Config_signal, 		&Command_send.signal);   	 
			GetCtrlVal (ConfigHandle,  Config_life_high, 	&Command_send.life_high); 
			GetCtrlVal (ConfigHandle,  Config_life_low, 	&Command_send.life_low); 
			GetCtrlVal (ConfigHandle,  Config_cycle, 		&cycle);
			
			Command = (BYTE *)&Command_send;      //(BYTE *)  Command_send
			
			//ComWrt (1, Command, package_send_size);                   //strlen(Command_send));
			//SetEvent(CommandStoreIsOK); 
			SetCtrlAttribute (panelHandle, PANEL_TIMER , ATTR_ENABLED , 1); 
			
			break;
		}
	return 0;
}

int CVICALLBACK quit (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	switch (event)
		{
		case EVENT_COMMIT:
			SetCtrlAttribute (panelHandle, PANEL_TIMER , ATTR_ENABLED , 0);    
			QuitUserInterface (0);
			break;
		}
	return 0;
}



int CVICALLBACK timer (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	float time;
	switch (event)
		{
		case EVENT_TIMER_TICK:
		if(cycle)
			{
				GetCtrlVal (ConfigHandle, Config_timer, &time);
				SetCtrlAttribute (panelHandle, PANEL_TIMER , ATTR_ENABLED , 1);    
				SetCtrlAttribute (panelHandle, PANEL_TIMER , ATTR_INTERVAL, time);
				SetEvent(CommandStoreIsOK);
				ComWrt (1, Command, package_send_size);  
			}
		else
			{
				ResetEvent(CommandStoreIsOK);
				SetCtrlAttribute (panelHandle, PANEL_TIMER , ATTR_ENABLED , 0); 
			}
			break;
		}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -