CTS_ctrl.cpp
上传用户:gelin96
上传日期:2017-01-08
资源大小:20993k
文件大小:11k
- /*****************************************************************************
- * Copyright Statement:
- * --------------------
- * This software is protected by Copyright and the information contained
- * herein is confidential. The software may not be copied and the information
- * contained herein may not be used or disclosed except with the written
- * permission of MediaTek Inc. (C) 2005
- *
- * BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
- * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
- * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
- * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
- * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
- * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
- * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
- * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
- * NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
- * SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
- *
- * BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
- * LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
- * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
- * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
- * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
- *
- * THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
- * WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
- * LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
- * RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
- * THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
- *
- *****************************************************************************/
- /*****************************************************************************
- *
- * Filename:
- * ---------
- * CTS_ctrl.cpp
- *
- * Project:
- * --------
- * Maui META APP
- *
- * Description:
- * ------------
- * Clima temperature system (CTS) control function source
- *
- * Author:
- * -------
- * Andy Ueng (mtk00490)
- *
- *============================================================================
- * HISTORY
- * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
- *------------------------------------------------------------------------------
- * $Revision$
- * $Modtime$
- * $Log$
- *
- *------------------------------------------------------------------------------
- * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
- *============================================================================
- ****************************************************************************/
- #include <vcl.h>
- #pragma hdrstop
- #ifndef _CTS_CTRL_H_
- #include "cts_ctrl.h"
- #endif
- #ifndef _FT_UTILS_H_
- #include "ft_utils.h"
- #endif
- /*---------------------------------------------------------------------------*/
- static const char CTS_CMD_BEGIN = 0x02;
- static const char CTS_CMD_ADR = 0x81;
- static const char CTS_CMD_START = 0xF0;
- static const char CTS_CMD_STOP = 0xF0;
- static const char CTS_CMD_TEMP_CHANNEL = 0xB0;
- static const char CTS_CMD_SET_ANALOG = 0xE1;
- static const char CTS_CMD_GET_ANALOG = 0xC1;
- static const char CTS_CHANNEL_TEMP = 0xB0;
- static const char CTS_CMD_END = 0x03;
- static const char CTS_BLANK = 0xA0;
- #define CTS_CMD_BUF_SIZE 256
- //===========================================================================
- CCTSCTRL::CCTSCTRL( void )
- {
- }
- //---------------------------------------------------------------------------
- CCTSCTRL::~CCTSCTRL()
- {
- }
- //=============================================================================
- bool CCTSCTRL::CTS_Init(int com_port)
- {
- sUART uart;
- uart.com_port = com_port;
- uart.baud_rate = CBR_19200;
- uart.byte_size = 8;
- uart.parity = ODDPARITY;
- uart.stop_bits = ONESTOPBIT;
- if(! UART_Obj.UART_Open( hCOM, uart ) ) return false;
- return true;
- }
- //--------------------------------------------------------------------------
- bool CCTSCTRL::CTS_Close(void)
- {
- UART_Obj.UART_Close( hCOM );
- return true;
- }
- //--------------------------------------------------------------------------
- bool CCTSCTRL::CTS_Start( void )
- {
- char cmd_data[CTS_CMD_BUF_SIZE];
- char cmd[CTS_CMD_BUF_SIZE];
- cmd_data[0] = CTS_CMD_ADR;
- cmd_data[1] = CTS_CMD_START;
- cmd_data[2] = 0xB0;
- cmd_data[3] = 0xB0;
- cmd_data[4] = 0xB1;
- cmd_data[5] = ' ';
- CTS_ComposeCmd( cmd_data, cmd );
- UART_Obj.UART_SendData( hCOM, cmd );
- return true;
- }
- //--------------------------------------------------------------------------
- bool CCTSCTRL::CTS_Stop( void )
- {
- char cmd_data[CTS_CMD_BUF_SIZE];
- char cmd[CTS_CMD_BUF_SIZE];
- cmd_data[0] = CTS_CMD_ADR;
- cmd_data[1] = CTS_CMD_STOP;
- cmd_data[2] = 0xB0;
- cmd_data[3] = 0xB0;
- cmd_data[4] = 0xB0;
- cmd_data[5] = ' ';
- CTS_ComposeCmd( cmd_data, cmd );
- UART_Obj.UART_SendData( hCOM, cmd );
- return true;
- }
- //--------------------------------------------------------------------------
- bool CCTSCTRL::CTS_SetTemp( double temp )
- {
- char cmd_temp[6];
- char cmd_data[CTS_CMD_BUF_SIZE];
- char cmd[CTS_CMD_BUF_SIZE];
- int i=0;
- double_To_Array_ASCII_char( temp, cmd_temp );
- cmd_temp[5] = ' ';
- while( i<sizeof(cmd_temp) && cmd_temp[i]!=' ' )
- {
- cmd_temp[i] |= 0x80;
- i++;
- }
- cmd_data[0] = CTS_CMD_ADR;
- cmd_data[1] = CTS_CMD_SET_ANALOG;
- cmd_data[2] = CTS_CMD_TEMP_CHANNEL;
- cmd_data[3] = CTS_BLANK;
- cmd_data[4] = ' ';
- strcat(cmd_data, cmd_temp);
- CTS_ComposeCmd( cmd_data, cmd );
- UART_Obj.UART_SendData( hCOM, cmd );
- return true;
- }
- //--------------------------------------------------------------------------
- bool CCTSCTRL::CTS_GetTemp( double &effective_temp, double &index_temp )
- {
- char cmd_data[CTS_CMD_BUF_SIZE];
- char cmd[CTS_CMD_BUF_SIZE];
- char received_data[CTS_CMD_BUF_SIZE];
- DWORD dw_byte_read;
- cmd_data[0] = CTS_CMD_ADR;
- cmd_data[1] = CTS_CMD_GET_ANALOG;
- cmd_data[2] = CTS_CMD_TEMP_CHANNEL;
- cmd_data[3] = ' ';
- CTS_ComposeCmd( cmd_data, cmd );
- UART_Obj.UART_SendData( hCOM, cmd );
- for(int i=0; i<5; i++)
- {
- Sleep(500);
- if( UART_Obj.UART_ReceiveData( hCOM, received_data, dw_byte_read ) ) break;
- }
- #if 0
- received_data[0] = 0x02;
- received_data[1] = 0x81;
- received_data[2] = 0xC1;
- received_data[3] = 0xB0;
- received_data[4] = 0xA0;
- received_data[5] = 0xAD;
- received_data[6] = 0xB1;
- received_data[7] = 0xB4;
- received_data[8] = 0xAE;
- received_data[9] = 0xB5;
- received_data[10] = 0xA0;
- received_data[11] = 0xAD;
- received_data[12] = 0xB1;
- received_data[13] = 0xB3;
- received_data[14] = 0xAE;
- received_data[15] = 0xB8;
- received_data[16] = 0xFA;
- received_data[17] = 0x03;
- dw_byte_read = 18;
- #endif
- if(! CTS_ExtractTemp( received_data, dw_byte_read, effective_temp, index_temp ) ) return false;
- return true;
- }
- //--------------------------------------------------------------------------
- bool CCTSCTRL::CTS_ExtractTemp( char* received_data, DWORD dw_byte_read, double &effective_temp, double &index_temp )
- {
- char Temp_Header[5];
- char effective_temp_array[6];
- char index_temp_array[6];
- int i;
- if( ! CTS_TempHeaderBytesCheck(received_data) ||
- ! CTS_TempChkByteCheck( received_data, dw_byte_read, *(received_data+16) ) ||
- ! CTS_TempTailByteCheck( received_data )
- )
- return false;
- for(i=0;i<sizeof(effective_temp_array)-1; i++)
- {
- effective_temp_array[i] = *(received_data+5+i);
- }
- effective_temp_array[i]=' ';
- ASCII_char_To_Array_double( effective_temp_array, effective_temp );
- for(i=0;i<sizeof(index_temp_array)-1; i++)
- {
- index_temp_array[i] = *(received_data+11+i);
- }
- index_temp_array[i] = ' ';
- ASCII_char_To_Array_double( index_temp_array, index_temp );
- return true;
- }
- //--------------------------------------------------------------------------
- bool CCTSCTRL::CTS_TempHeaderBytesCheck( char* received_data )
- {
- char Temp_Header[2];
- Temp_Header[0] = 0x02;
- Temp_Header[1] = 0x81;
- // Temp_Header[2] = 0xC1;
- // Temp_Header[3] = 0xB0;
- // Temp_Header[4] = 0xA0;
- for(int i=0; i<sizeof(Temp_Header); i++)
- {
- if( *(received_data+i) != Temp_Header[i] ) return false;
- }
- return true;
- }
- //--------------------------------------------------------------------------
- bool CCTSCTRL::CTS_TempChkByteCheck( char* received_data, DWORD dw_byte_read, char &r_chk )
- {
- char c_chk=0;
- for (unsigned int i=1; i<dw_byte_read-2; i++)
- {
- c_chk ^= *(received_data+i);
- }
- if(c_chk != r_chk) return false;
- return true;
- }
- //--------------------------------------------------------------------------
- bool CCTSCTRL::CTS_TempTailByteCheck( char* received_data )
- {
- if( *(received_data+17) != CTS_CMD_END ) return false;
- return true;
- }
- //--------------------------------------------------------------------------
- bool CCTSCTRL::CTS_ComposeCmd( char *cmd_data, char* cmd )
- {
- char cmd_chk=0;
- int i=0;
- int len;
- while( cmd_data[i]!=0 && i<CTS_CMD_BUF_SIZE )
- {
- cmd_chk ^= cmd_data[i];
- i++;
- }
- cmd_chk |= 0x80;
- cmd[0] = CTS_CMD_BEGIN;
- cmd[1] = ' ';
- strcat(cmd, cmd_data);
- len = strlen(cmd);
- cmd[len]= cmd_chk;
- cmd[len+1] = CTS_CMD_END;
- cmd[len+2] = ' ';
- return true;
- }
- //===========================================================================
- //////////////////////////// Global information ///////////////////////////
- //============================================================================
- HANDLE CCTSCTRL::CTS_GetComHandle( void )
- {
- return hCOM;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- DCB CCTSCTRL::Get_OriginalDCB( void )
- {
- DCB dcb = UART_Obj.Get_OriginalDCB( );
- return dcb;
- }
- //---------------------------------------------------------------------------
- bool CCTSCTRL::Set_OriginalDCB( DCB &dcb )
- {
- UART_Obj.Set_OriginalDCB( dcb );
- return true;
- }