Пример #1
0
bool SerialService::openCom()
{
    if(my_com)
    {
        closeCom();
    }
    else
    {
        QString com_name = "/dev/ttySAC1";
        my_com = new Posix_QextSerialPort(com_name, QextSerialBase::Polling);
    }
    my_com->open(QIODevice::ReadWrite);
    if(my_com->isOpen())
    {
        com_state = OPEN;
        my_com->setBaudRate(BAUD115200);
        my_com->setDataBits(DATA_8);
        my_com->setParity(PAR_NONE);
        my_com->setStopBits(STOP_1);
        my_com->setFlowControl(FLOW_OFF);
        my_com->setTimeout(50);
        return true;
    }
    else
    {
        com_state = CLOSE;
        return false;
    }
}
Пример #2
0
void SerialService::releaseSerial()
{
    if(my_com)
    {
        closeCom();
        delete my_com;
        my_com = 0;
    }
}
//************************************
// Method:    sendDataViaCom
// FullName:  sendDataViaCom
// Access:    public 
// Returns:   DWORD WINAPI
// Qualifier:
// Parameter: void *
// 描述:确认通信正常,否则重启com口再尝试,失败后所有命令有效标识置FALSE
//		如果通信正常,那么把所得结果存到UPS_STATE结构体中的相应字段
//		
//************************************
DWORD WINAPI sendDataViaCom(void* dummy){
	DWORD tryTime;
	char com[20]={0};
	while(1){
		for(int i=0;i<NUM_OF_UPS;i++){
			if(_2023ups[i].UPS_SET_ACTIVE == TRUE){
				tryTime=CHECK_TIME;
				
				start_commu:
			//测试通信是否正常
				if(!sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_COMMUNICATION_INI,\
					UPS_COMMUNICATION_INI_DECODE,_2023ups[i].UPS_COMMUNICATION_INI_ANSWER,1) && \
						!sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_01,UPS_CMD_01_DECODE,_2023ups[i].UPS_CMD_01_ANSWER,10)){
						//不正常,清零结果
					memset(_2023ups[i].UPS_COMMUNICATION_INI_ANSWER,0,\
						sizeof(_2023ups[i].UPS_COMMUNICATION_INI_ANSWER));			//
					memset(_2023ups[i].UPS_CMD_01_ANSWER,0,sizeof(_2023ups[i].UPS_CMD_01_ANSWER));
					//不正常,重启com口,并设置
					if(tryTime--){			//最多尝试CHECK_TIME次
						Sleep(1000);
						printf("restart com%d\n",_2023ups[i].LINK_COM_NUM);
						closeCom(_2023ups[i].UPS_COM_HANDLE);
						memset(com,0,20);
						sprintf(com,"\\\\.\\COM%d",_2023ups[i].LINK_COM_NUM);
						_2023ups[i].UPS_COM_HANDLE=initialCom(com,1024);
		//				COMMTIMEOUTS timeouts={5,15,15,1,1};
						COMMTIMEOUTS timeouts={_2023ups[i].READ_INTERVAL,_2023ups[i].READ_MULTIPLIER,\
							_2023ups[i].READ_CONSTANT,_2023ups[i].WRITE_MULTIPLIER,_2023ups[i].WRITE_CONSTANT};
						if(setComTimeout(_2023ups[i].UPS_COM_HANDLE,timeouts))
							printf("set com timeout ok .....\n");//需要加入异常处理及日志记录
						if(setComPara(_2023ups[i].UPS_COM_HANDLE,_24_N_8_1))
							printf("set com parameter ok.....\n");	
						if(_2023ups[i].UPS_COM_HANDLE == INVALID_HANDLE_VALUE){		//无效句柄
							printf("Open Com Error,try %d times\n",CHECK_TIME);								//退出
							//
							exit(0);
						}
						goto start_commu;											//重新测试通信
					}
				//尝试CHECK_TIME次失败后,确认通信故障
					else{		//所有命令标识置为无效
						_2023ups[i].UPS_COMMUNICATE_NORMAL=FALSE;
						_2023ups[i].CMD_01_CHECK=FALSE;
						_2023ups[i].CMD_02_CHECK=FALSE;
						_2023ups[i].CMD_03_CHECK=FALSE;
						_2023ups[i].CMD_06_CHECK=FALSE;
						_2023ups[i].CMD_07_CHECK=FALSE;
						_2023ups[i].CMD_24_CHECK=FALSE;
						_2023ups[i].CMD_27_CHECK=FALSE;
						_2023ups[i].CMD_31_CHECK=FALSE;
						_2023ups[i].CMD_32_CHECK=FALSE;
						_2023ups[i].CMD_3B_CHECK=FALSE;
						_2023ups[i].CMD_42_CHECK=FALSE;
		//				if((log=fopen("log","w"))!=NULL){
		//					fprintf(log,"%s\n","通信故障");
		//					fclose(log);
		//				}
						break;			//通信故障,无需再发送其他命令,下一台ups
					}
				}
				_2023ups[i].UPS_COMMUNICATE_NORMAL=TRUE;	//确认通信正常,通信标识置为真
				printf("communicate:%s\n",_2023ups[i].UPS_COMMUNICATION_INI_ANSWER);
				printf("\nCmd1:\n");
				//发送命令,如果所得的结果字节少于预期,那么清空结果字段
				if(!sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_01,UPS_CMD_01_DECODE,\
					_2023ups[i].UPS_CMD_01_ANSWER,10)){
						memset(_2023ups[i].UPS_CMD_01_ANSWER,0,sizeof(_2023ups[i].UPS_CMD_01_ANSWER));
				}
				sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_ACCEPT,UPS_CMD_ACCEPT_DECODE,NULL,0);
				printf("cmd01:%s\n",_2023ups[i].UPS_CMD_01_ANSWER);
				Sleep(1000);		
				printf("\nCmd2:\n");
				if(!sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_02,UPS_CMD_02_DECODE,\
					_2023ups[i].UPS_CMD_02_ANSWER,9)){
						memset(_2023ups[i].UPS_CMD_02_ANSWER,0,sizeof(_2023ups[i].UPS_CMD_02_ANSWER));
				}
				sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_ACCEPT,UPS_CMD_ACCEPT_DECODE,NULL,0);
				printf("cmd02:%s\n",_2023ups[i].UPS_CMD_02_ANSWER);
				Sleep(1000);
				printf("\nCmd3:\n");
				//发送的命令或接收到的数据长度不正确,清空命令结果
				if(!sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_03,UPS_CMD_03_DECODE,\
					_2023ups[i].UPS_CMD_03_ANSWER,8)){
						memset(_2023ups[i].UPS_CMD_03_ANSWER,0,sizeof(_2023ups[i].UPS_CMD_03_ANSWER));
				}
				sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_ACCEPT,UPS_CMD_ACCEPT_DECODE,NULL,0);
				printf("cmd03:%s\n",_2023ups[i].UPS_CMD_03_ANSWER);
				Sleep(1000);
				printf("\nCmd06:\n");
				if(!sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_06,UPS_CMD_06_DECODE,\
					_2023ups[i].UPS_CMD_06_ANSWER,10)){
						memset(_2023ups[i].UPS_CMD_06_ANSWER,0,sizeof(_2023ups[i].UPS_CMD_06_ANSWER));
				}
				sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_ACCEPT,UPS_CMD_ACCEPT_DECODE,NULL,0);
				printf("cmd06:%s\n",_2023ups[i].UPS_CMD_06_ANSWER);
				Sleep(1000);
				printf("\nCmd07:\n");
				if(!sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_07,UPS_CMD_07_DECODE,\
					_2023ups[i].UPS_CMD_07_ANSWER,8)){
						memset(_2023ups[i].UPS_CMD_07_ANSWER,0,sizeof(_2023ups[i].UPS_CMD_07_ANSWER));
				}
				sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_ACCEPT,UPS_CMD_ACCEPT_DECODE,NULL,0);
				printf("cmd07:%s\n",_2023ups[i].UPS_CMD_07_ANSWER);
				Sleep(1000);
				printf("\nCmd24:\n");
				if(!sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_24,UPS_CMD_24_DECODE,\
					_2023ups[i].UPS_CMD_24_ANSWER,6)){
						memset(_2023ups[i].UPS_CMD_24_ANSWER,0,sizeof(_2023ups[i].UPS_CMD_24_ANSWER));
				}
				sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_ACCEPT,UPS_CMD_ACCEPT_DECODE,NULL,0);
				printf("cmd24:%s\n",_2023ups[i].UPS_CMD_24_ANSWER);

	/*printf("\nCmd27:\n");
			if(!sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_27,UPS_CMD_27_DECODE,\
				_2023ups[i].UPS_CMD_27_ANSWER,8)){
					memset(_2023ups[i].UPS_CMD_27_ANSWER,0,sizeof(_2023ups[i].UPS_CMD_27_ANSWER));
			}
			sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_ACCEPT,UPS_CMD_ACCEPT_DECODE,NULL,0);
	printf("cmd27:%s\n",_2023ups[i].UPS_CMD_27_ANSWER);*/			//无法得到回应,注释 2015/6/4


				Sleep(1000);
				printf("\nCmd31:\n");
				if(!sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_31,UPS_CMD_31_DECODE,\
					_2023ups[i].UPS_CMD_31_ANSWER,8)){
						memset(_2023ups[i].UPS_CMD_31_ANSWER,0,sizeof(_2023ups[i].UPS_CMD_31_ANSWER));
				}
				sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_ACCEPT,UPS_CMD_ACCEPT_DECODE,NULL,0);
				printf("cmd31:%s\n",_2023ups[i].UPS_CMD_31_ANSWER);
				Sleep(1000);
				printf("\nCmd32:\n");
				if(!sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_32,UPS_CMD_32_DECODE,\
					_2023ups[i].UPS_CMD_32_ANSWER,9)){
						memset(_2023ups[i].UPS_CMD_32_ANSWER,0,sizeof(_2023ups[i].UPS_CMD_32_ANSWER));
				}
				sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_ACCEPT,UPS_CMD_ACCEPT_DECODE,NULL,0);
				printf("cmd32:%s\n",_2023ups[i].UPS_CMD_32_ANSWER);
				Sleep(1000);
				printf("\nCmd3B:\n");
				if(!sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_3B,UPS_CMD_3B_DECODE,\
					_2023ups[i].UPS_CMD_3B_ANSWER,6)){
						memset(_2023ups[i].UPS_CMD_3B_ANSWER,0,sizeof(_2023ups[i].UPS_CMD_3B_ANSWER));
				}
				sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_ACCEPT,UPS_CMD_ACCEPT_DECODE,NULL,0);
				printf("cmd3B:%s\n",_2023ups[i].UPS_CMD_3B_ANSWER);
				Sleep(1000);
				printf("\nCmd42:\n");
				if(!sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_42,UPS_CMD_42_DECODE,\
					_2023ups[i].UPS_CMD_42_ANSWER,12)){
						memset(_2023ups[i].UPS_CMD_42_ANSWER,0,sizeof(_2023ups[i].UPS_CMD_42_ANSWER));
				}
				if(!sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_ACCEPT,UPS_CMD_ACCEPT_DECODE,\
					&_2023ups[i].UPS_CMD_42_ANSWER[12],5)){
						memset(_2023ups[i].UPS_CMD_42_ANSWER,0,sizeof(_2023ups[i].UPS_CMD_42_ANSWER));
				}
				sendCMD(_2023ups[i].UPS_COM_HANDLE,UPS_CMD_ACCEPT,UPS_CMD_ACCEPT_DECODE,NULL,0);
				printf("cmd42:%s\n",_2023ups[i].UPS_CMD_42_ANSWER);

				//将接收到的数据转换形式储存到UPS_STATE结构体中
				//设置通信正常标识
				if(_2023ups[i].UPS_COMMUNICATION_INI_ANSWER[0] == 0x16){
					_2023ups[i].UPS_COMMUNICATE_NORMAL=TRUE;
				}
				else{
					_2023ups[i].UPS_COMMUNICATE_NORMAL=FALSE;
				}
				//判断该应答是否有效(0x06,0x84打头),诺有效,依据应答填充UPS_STATE结构体
				//否则,命令有效标识置FALSE
				static const char VALID_ANSWER[3]={0x06,0x84,0x00};
				static const char VALID_CMD1_ANSWER[4]={0x06,0x84,0x66,0x00};
				static const char VALID_CMD2_ANSWER[4]={0x06,0x84,0x55,0x00};
				static const char VALID_CMD3_ANSWER[4]={0x06,0x84,0x44,0x00};
				static const char VALID_CMD6_ANSWER[4]={0x06,0x84,0x66,0x00};
				if(!strncmp(VALID_CMD1_ANSWER,_2023ups[i].UPS_CMD_01_ANSWER,3)){
					_2023ups[i].INPUT_POWER_ABNORMAL=(_2023ups[i].UPS_CMD_01_ANSWER[4]==0x00)?TRUE:FALSE;
					_2023ups[i].BATTERY_LOW_VOLTAGE=(_2023ups[i].UPS_CMD_01_ANSWER[5]==0x01)?TRUE:FALSE;
					_2023ups[i].DISCHARGE=(_2023ups[i].UPS_CMD_01_ANSWER[6]==0x00)?TRUE:FALSE;
					_2023ups[i].CMD_01_CHECK=TRUE;
				}
				else{
					_2023ups[i].CMD_01_CHECK=FALSE;
				}
				if(!strncmp(VALID_CMD2_ANSWER,_2023ups[i].UPS_CMD_02_ANSWER,3)){
					_2023ups[i].UPS_ERROR=(_2023ups[i].UPS_CMD_02_ANSWER[4]==0x01)?TRUE:FALSE;
					_2023ups[i].BATTERY_LINK_STATE=(_2023ups[i].UPS_CMD_02_ANSWER[5]==0x01)?TRUE:FALSE;
					_2023ups[i].OVERLOAD=(_2023ups[i].UPS_CMD_02_ANSWER[6]==0x00)?FALSE:TRUE;
					_2023ups[i].CMD_02_CHECK=TRUE;
				}
				else{
					_2023ups[i].CMD_02_CHECK=FALSE;
				}
				if(!strncmp(VALID_CMD3_ANSWER,_2023ups[i].UPS_CMD_03_ANSWER,3)){
					_2023ups[i].FAN_ERROR=(_2023ups[i].UPS_CMD_03_ANSWER[5]==0x00)?FALSE:TRUE;
					_2023ups[i].OVERHEAT=(_2023ups[i].UPS_CMD_03_ANSWER[6]==0x00)?FALSE:TRUE;
					_2023ups[i].CMD_03_CHECK=TRUE;
				}
				else{
					_2023ups[i].CMD_03_CHECK=FALSE;
				}
				if(!strncmp(VALID_CMD6_ANSWER,_2023ups[i].UPS_CMD_06_ANSWER,3)){
					_2023ups[i].BATTERY_CAPACITY=_2023ups[i].UPS_CMD_06_ANSWER[4];
					_2023ups[i].BATTERY_REMAIN_TIME=(	((unsigned char)_2023ups[i].UPS_CMD_06_ANSWER[5])+	\
						(((unsigned char)_2023ups[i].UPS_CMD_06_ANSWER[6])<<8)+	\
						(((unsigned char)_2023ups[i].UPS_CMD_06_ANSWER[7])<<16)+	\
						(((unsigned char)_2023ups[i].UPS_CMD_06_ANSWER[8])<<24)	)/60;
					_2023ups[i].CMD_06_CHECK=TRUE;
				}
				else{
					_2023ups[i].CMD_06_CHECK=FALSE;
				}

				if(!strncmp(VALID_ANSWER,_2023ups[i].UPS_CMD_07_ANSWER,2)){
					_2023ups[i].LOAD_PERCEN=_2023ups[i].UPS_CMD_07_ANSWER[4];
					_2023ups[i].BATTERY_VOLTATE=(unsigned char)_2023ups[i].UPS_CMD_07_ANSWER[5]+\
						((unsigned char)_2023ups[i].UPS_CMD_07_ANSWER[6]<<8);
					_2023ups[i].CMD_07_CHECK=TRUE;
				}
				else{
					_2023ups[i].CMD_07_CHECK=FALSE;
				}
				if(!strncmp(VALID_ANSWER,_2023ups[i].UPS_CMD_24_ANSWER,2)){
					_2023ups[i].BATTERY_SELFTEST=_2023ups[i].UPS_CMD_24_ANSWER[4];
					_2023ups[i].CMD_24_CHECK=TRUE;
				}
				else{
					_2023ups[i].CMD_24_CHECK=FALSE;
				}
				/*if(!strncmp(VALID_ANSWER,_2023ups[i].UPS_CMD_27_ANSWER,2)){
					_2023ups[i].BATTERY_LINK_STATE=(_2023ups[i].UPS_CMD_27_ANSWER[4]==0x01)?TRUE:FALSE;
					_2023ups[i].CHARGER_ERROR=(_2023ups[i].UPS_CMD_27_ANSWER[5]==0x01)?TRUE:FALSE;
					_2023ups[i].OVERCHARGE=(_2023ups[i].UPS_CMD_27_ANSWER[6]==0x01)?TRUE:FALSE;
					_2023ups[i].CMD_27_CHECK=TRUE;
				}
				else{
					_2023ups[i].CMD_27_CHECK=FALSE;
				}*/
				if(!strncmp(VALID_ANSWER,_2023ups[i].UPS_CMD_31_ANSWER,2)){
					_2023ups[i].INPUT_FREQUENCY=_2023ups[i].UPS_CMD_31_ANSWER[4];
					_2023ups[i].INPUT_VOLTAGE=(((unsigned char)_2023ups[i].UPS_CMD_31_ANSWER[6])<<8) +\
						(unsigned char)_2023ups[i].UPS_CMD_31_ANSWER[5];
					_2023ups[i].CMD_31_CHECK=TRUE;
				}
				else{
					_2023ups[i].CMD_31_CHECK=FALSE;
				}
				if(!strncmp(VALID_ANSWER,_2023ups[i].UPS_CMD_32_ANSWER,2)){
					_2023ups[i].INPUT_FREQUENCY_ABNORMAL=(_2023ups[i].UPS_CMD_32_ANSWER[4]==0x01)?TRUE:FALSE;
					_2023ups[i].INPUT_POWER_MODE=(_2023ups[i].UPS_CMD_32_ANSWER[6]==0x01)?TRUE:FALSE;
					_2023ups[i].INPUT_VOLTAGE_ABNORMAL=(_2023ups[i].UPS_CMD_32_ANSWER[7]==0x01)?TRUE:FALSE;
					_2023ups[i].CMD_32_CHECK=TRUE;
				}
				else{
					_2023ups[i].CMD_32_CHECK=FALSE;
				}
				if(!strncmp(VALID_ANSWER,_2023ups[i].UPS_CMD_3B_ANSWER,2)){
					_2023ups[i].BATTERY_MODE=(_2023ups[i].UPS_CMD_3B_ANSWER[4]==0x01)?TRUE:FALSE;
					_2023ups[i].CMD_3B_CHECK=TRUE;
				}
				else{
					_2023ups[i].CMD_3B_CHECK=FALSE;
				}
				if(_2023ups[i].UPS_CMD_42_ANSWER[0] == 0x06){
					_2023ups[i].OUTPUT_WATT=(unsigned char)_2023ups[i].UPS_CMD_42_ANSWER[4] + \
						((unsigned char)_2023ups[i].UPS_CMD_42_ANSWER[5]<<8);
					_2023ups[i].OUTPUT_VA=(unsigned char)_2023ups[i].UPS_CMD_42_ANSWER[6] + \
						((unsigned char)_2023ups[i].UPS_CMD_42_ANSWER[7]<<8);
					_2023ups[i].OUTPUT_CURRENT=(unsigned char)_2023ups[i].UPS_CMD_42_ANSWER[8]*0.1;
					_2023ups[i].OUTPUT_FREQUENCY=(unsigned char)_2023ups[i].UPS_CMD_42_ANSWER[9];
					_2023ups[i].OUTPUT_VOLTAGE=(unsigned char)_2023ups[i].UPS_CMD_42_ANSWER[14] + \
						((unsigned char)_2023ups[i].UPS_CMD_42_ANSWER[15]<<8);
					_2023ups[i].CMD_42_CHECK=TRUE;
				}
				else{
					_2023ups[i].CMD_42_CHECK=FALSE;
				}
			}
		}
		Sleep(15000);						//
	}
	return 0;
}
Пример #4
0
void rfidTestStop(void)
{
    g_rfidTestOn = 0;

    closeCom();
}
Пример #5
0
int main(int argc, char *argv[])
{



    // ASCII header
    printf("    [===============================================]\r\n");
    printf("    |    FLYSKY FS-i6 CUSTOM FIRMWARE UPLOADER      |\r\n");
    printf("    |    by Thom				    |\r\n");
    printf("    | 					            |\r\n");
    printf("    |    check out my blog for more:		    |\r\n");
    printf("    |    basejunction.wordpress.com	            |\r\n");
    printf("    [===============================================]\r\n\r\n");
    printf("\n!!! Please make sure the firmware file comes from basejunction.wordpress.com !!!\n\n");

    HANDLE hdlr = NULL; //port handle
    int n_com=1;
    int n_baudrate=115200;

    char arg1[256],arg2[256],arg3[256],arg4[256],arg5[256];
    char cmd[256]="";

    if(argc>=2)
        n_com=(int)strtol(argv[1],NULL,10);
    if(argc>=3)
        n_baudrate=(int)strtol(argv[2],NULL,10);

    //init
    printf("Opening COM%d at %d bauds : ",n_com,n_baudrate);
    if(openCom(n_com,n_baudrate,&hdlr))
        printf("SUCCESS");
    else
        printf("FAILED - verify your COM port number and baudrate settings");
    printf("\n\n");



    //main loop
    do
    {

        printf(">> ");
        fgets(cmd, sizeof(cmd), stdin);

        sscanf(cmd,"%s %s %s %s %s",arg1,arg2,arg3,arg4,arg5);



        if(!strcmp(arg1,"help")|| !strcmp(arg1,"h"))
        {
            printf("-- open [port] [baudrate] : open com port number [port] at [baudrate] bauds\n");
            printf("-- readb : read byte \n");
            printf("-- readf : read frame \n");
            printf("-- sendb [format] [byte] : send [byte] with [format{-b,-d,-h}]\n");
            printf("-- write [filename] [offset:h] [nbytes:d] : write [nbytes](decimal) bytes page at [offset](hex)\n");
            printf("-- flash [filename] : program [filename] firmware into TX flash memory (bootloader untouched)\n");
            printf("-- ping : send a ping request\n");
            printf("-- reset : send a reset TX request\n");
            printf("-- quit : quit the updater\n");

        }

        else if(!strcmp(arg1,"readb"))
        {
            unsigned char byte=0;
            if(readByte(&byte,&hdlr))
                printf("%x \n",byte);
            else
                printf("ERROR - reading byte from serial buffer\n");
        }
        else if(!strcmp(arg1,"readf"))
        {
            unsigned short size=0;
            unsigned short checksum=0;
            unsigned char frame[256];
            if(!readFramedbg(&size,&checksum,frame,&hdlr))
                printf("ERROR - reading frame from serial buffer\n");
            if(!sumcheck(frame,size))
                printf("ERROR - checksum mismatch\n");

        }
        else if(!strcmp(arg1,"sendb"))
        {
            unsigned char msg=0;
            if(!strcmp(arg2,"-h"))
                msg=(unsigned char)strtoul(arg3,NULL,16);
            if(!strcmp(arg2,"-d"))
                msg=(unsigned char)strtoul(arg3,NULL,10);
            if(!strcmp(arg2,"-b"))
                msg=(unsigned char)strtoul(arg3,NULL,2);
            printf("Sending byte: %d 0x%x\n",msg,msg);
            if(!sendByte(&msg,&hdlr))
                printf("ERROR - sending byte in serial buffer\n");
        }
        else if(!strcmp(arg1,"reset"))
        {
            printf("reset . . . \n");
            unsigned char msg[]={0x06,0x00,0xC1,0x00,0xFF,0xFF};//38FF
            sendFrame(msg,sizeof(msg),&hdlr);
        }
        else if(!strcmp(arg1,"ping"))
        {
            unsigned char msg[]={0x05,0x00,0xC0,0x3A,0xFF};
            unsigned short size=0;
            unsigned short checksum=0;
            unsigned char frame[256];

            printf("ping . . . \n");
            sendFrame(msg,sizeof(msg),&hdlr);

            if(!readFrame(&size,&checksum,frame,&hdlr))
                printf("ERROR - reading frame from serial buffer\n");
            if(!sumcheck(frame,size))
                printf("ERROR - checksum mismatch\n");
            printf("\nConnected : FLYSKY i6 \nFirmware version : %d.%d \n",frame[6],frame[4]);
        }
        else if(!strcmp(arg1,"write"))
        {
            unsigned int offset=strtoul(arg3,NULL,16);
            unsigned char msg[256];
            if(!readPage(msg,arg2,offset,256))
                printf("ERROR - reading file\n");
            writePage(msg,sizeof(msg),offset,&hdlr);
        }
        else if(!strcmp(arg1,"flash"))
        {
            BOOL cont = TRUE;
            unsigned char msg[256];
            int i=0x1800;
            for(i=0x1800;(i<0xF300)&&cont;i=i+0x100)
            {
                //11 00 C2 00 18 00 09 00 00 00 00 00 00 00 00 0B FF
                unsigned char msg2[17]={0x11,0x00,0xC2,0xFF,0xFF,0x00,0x09,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF};
                unsigned short reply_size=0;
                unsigned short reply_checksum=0;
                unsigned char reply[19];
                //55 13 00 C2 80 00 18 00 09 00 00 00 00 00 00 00 00 34 FE

                msg2[3]=(unsigned char)i;
                msg2[4]=(unsigned char)(i>>8);
                sendFrame(msg2,sizeof(msg2),&hdlr);

                unsigned char ok_reply[19]={0x55,0x13,0x00,0xC2,0x80,0xFF,0xFF,0x00,0x09,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF};
                ok_reply[5]=(unsigned char)i;
                ok_reply[6]=(unsigned char)(i>>8);


                unsigned int checksum=0xFFFF;
                int j=0;
                for(j=0;j<19-2;j++)
                {
                    checksum=checksum-ok_reply[j];
                }

                ok_reply[19-2]=(unsigned char)checksum;
                ok_reply[19-1]=(unsigned char)(checksum>>8);


                readFrame(&reply_size,&reply_checksum,reply,&hdlr);

                unsigned char reply_s[19];
                memcpy(reply_s,reply,reply_size);

                // compare received reply with correct one
                if(memcmp(reply_s,ok_reply,sizeof(reply_s))!=0)
                {
                    printf("ERROR - bad reply from TX\n");
                    cont=FALSE;
                }

                if(!readPage(msg,arg2,i,256))
                {
                    printf("ERROR - reading file\n");
                    cont=FALSE;
                }
                writePage(msg,sizeof(msg),i,&hdlr);
                i=i+0x100;
                if(!readPage(msg,arg2,i,256))
                {
                    printf("ERROR - reading file\n");
                    cont=FALSE;
                }
                writePage(msg,sizeof(msg),i,&hdlr);
                i=i+0x100;
                if(!readPage(msg,arg2,i,256))
                {
                    printf("ERROR - reading file\n");
                    cont=FALSE;
                }
                writePage(msg,sizeof(msg),i,&hdlr);
                i=i+0x100;
                if(!readPage(msg,arg2,i,256))
                {
                    printf("ERROR - reading file\n");
                    cont=FALSE;
                }
                writePage(msg,sizeof(msg),i,&hdlr);
            }
            //reset
            if(cont)
            {
                printf("Reset . . .\n");
                unsigned char msg3[]={0x06,0x00,0xC1,0x00,0xFF,0xFF};//38FF
                sendFrame(msg3,sizeof(msg3),&hdlr);
            }

        }

        else if(!strcmp(arg1,"open"))
        {
            closeCom(&hdlr);
            unsigned int n_com=strtoul(arg2,NULL,10);
            unsigned int n_baudrate=strtoul(arg3,NULL,10);
            printf("Opening COM%d at %d bauds : ",n_com,n_baudrate);
            if(openCom(n_com,n_baudrate,&hdlr))
                printf("SUCCESS");
            else
                printf("FAILED - verify your COM port number and baudrate settings");
            printf("\n\n");
        }

        else if(strcmp(arg1,"quit"))
        {
            printf("-- Unknown command \"%s\"\n",arg1);
            printf("-- Type \"help\" for a command list\n");
        }
    }while(strcmp(arg1,"quit"));