📄 dex_me.c
字号:
/*
DLE SOH to VMD
*/
fprintf(OutFile, "DLE SOH -> VMD\n");
printf("DLE SOH -> VMD\n");
WriteCharTimed( port, DEX_DLE, SMALL_DELAY );
delay(15);
WriteCharTimed( port, DEX_SOH, SMALL_DELAY );
delay(15);
/*
Communication ID, Operation Request, Revision & Level
*/
fprintf(OutFile, "CommID=%s, len=%d\n", CommID, strlen(CommID));
printf("CommID=%s, len=%d\n", CommID, strlen(CommID));
BCC=0;
for(i=0; i<strlen(CommID); i++)
{
crc_16( (unsigned char)CommID[i] );
WriteCharTimed( port, CommID[i], SMALL_DELAY );
}
/*
DLE ETX CRC, execlude any DLE,,..
*/
fprintf(OutFile, "DLE ETX CRC -> VMD\n");
printf("DLE ETX CRC -> VMD\n");
WriteCharTimed( port, DEX_DLE, SMALL_DELAY );
delay(15);
WriteCharTimed( port, DEX_ETX, SMALL_DELAY );
delay(15);
crc_16( DEX_ETX );
WriteCharTimed( port, (unsigned char)(BCC&0xff), SMALL_DELAY );
delay(15);
WriteCharTimed( port, (unsigned char)((BCC&0xff00)>>8), SMALL_DELAY );
delay(75);
fprintf(OutFile, "BCC=0x%x\n", BCC);
printf("BCC=0x%x\n", BCC);
// delay(200); // delay(100); dose not work
/*
wait DLE '1' !
*/
fprintf(OutFile, "wait for DLE'1'!\n");
printf("wait for DLE'1'!\n");
Reset_ErrorC();
for(;;)
{
if(kbhit())
{
if(getch()==0x1b)
return 0;
}
c=ReadCharTimed( port, READ_CHAR_DELAY);
if(c>=ASSUCCESS)
{
Reset_ErrorC();
c&=0xff;
if(c== DEX_DLE)
{
c=ReadCharTimed( port, READ_CHAR_DELAY );
if(c>=ASSUCCESS)
{
c&=0xff;
fprintf(OutFile, "0x%02x [%c]\n", c, c);
printf("0x%02x [%c]\n", c, c);
if( (c==DEX_ACK0) || (c==DEX_ACK1) )
{
fprintf(OutFile, "DEX_ACK received!\n");
printf("DEX_ACK received!\n");
break;
}
else if(c==DEX_WACK)
{
fprintf(OutFile, "Just received WACK\n");
printf("Just received WACK\n");
delay(250); /* delay 100 between 500*/
c=ForWack();
fprintf(OutFile, "ForWack=[%c] 0x%x\n", c, c);
printf("ForWack=[%c] 0x%x\n", c, c);
switch(c)
{
case DEX_ACK0:
case DEX_ACK1:
fprintf(OutFile, "send EOT to VMD\n");
printf("send EOT to VMD\n");
WriteCharTimed(port, DEX_EOT, SMALL_DELAY);
delay(SMALL_DELAY);
return 1;
case DEX_NAK:
fprintf(OutFile, "Got a [NAK]\n");
printf("Got a [NAK]\n");
default:
fprintf(OutFile, "ForWack() =0x%02x\n", c);
printf("ForWack() =0x%02x\n", c);
WriteCharTimed(port, DEX_EOT, SMALL_DELAY); // ok?
delay(SMALL_DELAY);
return 0;
}
}
}
else
{
fprintf(OutFile, "[0x%02x] ", c);
printf("[0x%02x] ", c);
}
}
else
{
fprintf(OutFile, "0x%02x ", c);
printf("0x%02x ", c);
}
}
else
{
if( Check_ErrorC()==0 )
return 0;
}
}
fprintf(OutFile, "send EOT to VMD\n");
printf("send EOT to VMD\n");
WriteCharTimed(port, DEX_EOT, SMALL_DELAY);
delay(SMALL_DELAY);
return 1;
}
/*
*/
int VMD_Master_HandShake(void)
{
int i;
int R1, R2;
int c;
unsigned int CRC_From_VMD;
BCC=0;
fprintf(OutFile, "-------- DC_Slave_HandShake()!\n");
printf("-------- DC_Slave_HandShake()!\n");
Reset_Alternate_Answer();
/* DC <- VMD (DEX_ENQ) */
c=ReadCharTimed(port, READ_CHAR_DELAY);
if(c>=ASSUCCESS)
{
c&=0xff;
if(c==DEX_ENQ)
{
/* DC got it! (DEX_ENQ)*/
fprintf(OutFile, "DC got (DEX_ENQ)!\n");
printf("DC got (DEX_ENQ)!\n");
}
else
{
return 0;
}
}
else
{
return 0;
}
/* DC -> VMD (DEX_DLE, '0') */
#if 1
DC_Alternate_Answer();
#else
delay(SMALL_DELAY);
WriteCharTimed(port, DEX_DLE, SMALL_DELAY); // DEX_DLE
WriteCharTimed(port, DEX_ACK0, SMALL_DELAY); // DEX_ACK0
#endif
/* DC <- VMD (DLE SOH) */
delay(BIG_DELAY);
R1=ReadCharTimed(port, READ_CHAR_DELAY);
R2=ReadCharTimed(port, READ_CHAR_DELAY);
if(R1>=ASSUCCESS && R2>=ASSUCCESS)
{
R1&=0xff;
R2&=0xff;
}
if( (R1==DEX_DLE) && (R2==DEX_SOH) )
{
fprintf(OutFile, "DC got (DLE, SOH)!\n");
printf("DC got (DLE, SOH)!\n");
}
else
{
fprintf(OutFile, "R1=0x%x, R2=0x%x\n", R1, R2);
printf("R1=0x%x, R2=0x%x\n", R1, R2);
}
#if 0 // Response Code =0
/* DC <- VMD (Response Code) */
R1=ReadCharTimed(port, READ_CHAR_DELAY);
if(R1>=ASSUCCESS)
{
R1&=0xff;
fprintf(OutFile, "Response code=0x%x\n", R1);
}
#endif
CommID_index=0;
/* DC <- VMD (Communication ID) 10-digit ? */
for(i=0; i<17; i++)
{
c=ReadCharTimed(port, READ_CHAR_DELAY);
if(c>=ASSUCCESS)
{
c&=0xff;
crc_16(c);
CommID_Buffer[CommID_index++]=c;
}
else
{
fprintf(OutFile, "CommID Err=0x%x\n", c);
printf("CommID Err=0x%x\n", c);
}
}
CommID_Buffer[CommID_index]=0;
if(CommID_index!=18)
{
fprintf(OutFile, "CommID too short, len=%d\n", CommID_index);
printf("CommID too short, len=%d\n", CommID_index);
}
fprintf(OutFile, "VMD ID=[%s]\n", CommID_Buffer);
printf("VMD ID=[%s]\n", CommID_Buffer);
/* DC <- VMD (DLE ETX) */
R1=ReadCharTimed(port, READ_CHAR_DELAY);
R2=ReadCharTimed(port, READ_CHAR_DELAY);
if( (R1>=ASSUCCESS) && (R2>=ASSUCCESS) )
{
R1&=0xff;
R2&=0xff;
if( (R1 == DEX_DLE) && (R2==DEX_ETX) )
{
fprintf(OutFile, "DC got (DLE, ETX)!\n");
printf("DC got (DLE, ETX)!\n");
}
else
{
fprintf(OutFile, "R1=0x%x, R2=0x%x\n", R1, R2);
printf("R1=0x%x, R2=0x%x\n", R1, R2);
}
}
CRC_From_VMD=GetCRC();
if(VerifyCRC(CRC_From_VMD)==1)
{
fprintf(OutFile, "CRC correct!\n");
printf("CRC correct!\n");
}
else
{
fprintf(OutFile, "CRC error!\n");
printf("CRC error!\n");
}
/*
DC -> VMD DLE '1'
*/
#if 1
DC_Alternate_Answer();
#else
delay(SMALL_DELAY);
WriteCharTimed(port, DEX_DLE, SMALL_DELAY); // DEX_DLE
WriteCharTimed(port, DEX_ACK1, SMALL_DELAY); // DEX_ACK1
#endif
/*
*/
R1=ReadCharTimed(port, READ_CHAR_DELAY);
if(R1>=ASSUCCESS)
{
R1&=0xff;
switch(R1)
{
case DEX_EOT:
return 1;
case DEX_WACK:
ForWack();
return 1;
default:
return 0;
}
}
else
{
fprintf(OutFile, "W");
printf("W");
return 0;
}
return 1;
}
/*
*/
void Reset_Alternate_Answer(void)
{
DC_Answer=0x30;
}
/*
*/
void DC_Alternate_Answer(void)
{
fprintf(OutFile, "DC DLE %c\n", DC_Answer);
printf("DC DLE %c\n", DC_Answer);
// delay(15);
// delay(SMALL_DELAY);
WriteChar(port, DEX_DLE);
// delay(SMALL_DELAY);
WriteChar(port, DC_Answer);
DC_Answer^=1;
}
/*
*/
unsigned int GetCRC(void)
{
int i;
int c1, c2;
unsigned int CRC_Value;
// for(i=0; i<5; i++)
{
c1=ReadCharTimed(port, READ_CHAR_DELAY); // lo
c2=ReadCharTimed(port, READ_CHAR_DELAY); // hi
if( (c1>=ASSUCCESS) && (c2>=ASSUCCESS) )
{
c1&=0xff;
c2&=0xff;
CRC_Value=(unsigned char)(c2&0xff);
CRC_Value<<=8;
CRC_Value|=(unsigned char)(c1&0xff);
return CRC_Value;
}
}
// fprintf(OutFile, "CRC=0x%04x\n", CRC_Value);
return 0xffff;
}
/*
*/
int VerifyCRC(unsigned int CRC)
{
unsigned int CRC_Value;
CRC_Value=GetCRC();
fprintf(OutFile, "CRC=0x%04x, CRC_VMD=0x%04x\n", CRC, CRC_Value);
printf("CRC=0x%04x, CRC_VMD=0x%04x\n", CRC, CRC_Value);
if(CRC_Value==CRC)
return 1;
return 0;
}
/*
*/
int DataRecordTransfer(void)
{
int c, R1, R2;
fprintf(OutFile, "-------- DataRecordTransfer()!\n");
printf("-------- DataRecordTransfer()!\n");
Reset_Alternate_Answer();
Reset_ErrorC();
for(;;)
{
if(kbhit())
{
if(getch()==0x1b)
return 0;
}
c=ReadCharTimed(port, READ_CHAR_DELAY);
if(c>=ASSUCCESS)
{
Reset_ErrorC();
c&=0xff;
// printf("c=%02x\n",c);
switch(c)
{
case DEX_ENQ:
fprintf(OutFile, "[ENQ]\n");
printf("[ENQ]\n");
DC_Alternate_Answer();
break;
case DEX_STX: // DLE STX (Start of a Data block)
fprintf(OutFile, "[STX]\n");
printf("[STX]\n");
Dex_Buffer_index=0; // point to the Buffer's start
BCC=0; // Set BCC = 0, for CRC-16
break;
case DEX_ETB: // DLE ETB CRC (End of Data block)
fprintf(OutFile, "[ETB]\n");
printf("[ETB]\n");
fprintf(OutFile, "%s", Dex_Buffer);
printf("%s", Dex_Buffer);
/* CRC */
crc_16(DEX_ETB);
if(VerifyCRC(BCC)==1)
{
fprintf(OutFile, "CRC correct!\n");
printf("CRC correct!\n");
}
else
{
fprintf(OutFile, "CRC error!\n");
printf("CRC error!\n");
}
DC_Alternate_Answer();
break;
case DEX_ETX: // DLE ETX (indicate the Final Data block)
fprintf(OutFile, "[ETX]\n");
printf("[ETX]\n");
fprintf(OutFile, "%s", Dex_Buffer);
printf("%s", Dex_Buffer);
crc_16(DEX_ETX);
/* CRC */
if(VerifyCRC(BCC)==1)
{
fprintf(OutFile, "CRC correct!\n");
printf("CRC correct!\n");
}
else
{
fprintf(OutFile, "CRC error!\n");
printf("CRC error!\n");
}
DC_Alternate_Answer();
break;
case DEX_EOT: // DLE EOT (End of DataRecordTransfer)
fprintf(OutFile, "[EOT]\n");
printf("[EOT]\n");
return 1;
case DEX_DLE:
case DEX_SOH:
// Control Byte, just ignore
break;
default:
// Bytes for Data Record;
#if 0
if(c<0x20)
{
// invalid character
}
else
#endif
{
// put this Character into the buffer
if( (Dex_Buffer_index+1)< sizeof(Dex_Buffer) )
{
crc_16(c);
Dex_Buffer[Dex_Buffer_index++]=c;
}
else
{
Dex_Buffer[sizeof(Dex_Buffer)-1]=0;
fprintf(OutFile, "[Error] Dex_Buffer full!\n");
printf("[Error] Dex_Buffer full!\n");
fprintf(OutFile, "%s\n", Dex_Buffer);
printf("%s\n", Dex_Buffer);
Dex_Buffer_index=0;
}
Dex_Buffer[Dex_Buffer_index]=0;
}
break;
}
}
else
{
if( Check_ErrorC()==0 )
return 0;
}
}
return 1;
}
/*
*/
int dex_loop(void)
{
/* 1 */
dex_mode=WhoIsMaster();
if(dex_mode==DC_UNKNOWN)
return 0;
/* 2 */
if(dex_mode==DC_SLAVE)
{
if (DC_master_handshake(CHANGER_IS_SLAVE) == 1)
{
fprintf(OutFile, "DC_Master_handshake Ok!\n");
printf("DC_Master_handshake Ok!\n");
}
else if (DC_master_handshake(CHANGER_IS_SLAVE) == 0)
{
fprintf(OutFile, "DC_Master handshake failed!\n");
printf("DC_Master handshake failed!\n");
return 0;
}
/* 3 */
if(DC_Slave_HandShake()==1)
{
fprintf(OutFile, "DC_Slave_HandShake() ok!\n");
printf("DC_Slave_HandShake() ok!\n");
}
else
{
fprintf(OutFile, "DC_Slave_HandShake() Failed!\n");
printf("DC_Slave_HandShake() Failed!\n");
return 0;
}
if(DataRecordTransfer()==1)
{
fprintf(OutFile, "DataRecordTransfer() Ok!\n");
printf("DataRecordTransfer() Ok!\n");
}
else
{
fprintf(OutFile, "DataRecordTransfer() Failed!\n");
printf("DataRecordTransfer() Failed!\n");
return 0;
}
}
else if(dex_mode==DC_MASTER)
{
if (VMD_Master_HandShake() == 1)
{
fprintf(OutFile, "VMD_Master_Handshake Ok!\n");
printf("VMD_Master_handshake Ok!\n");
}
else
{
fprintf(OutFile, "VMD_Master handshake failed!\n");
printf("VMD_Master handshake failed!\n");
return 0;
}
/* 3 */
if(VMD_Slave_handshake(VMD_IS_MASTER)==1)
{
fprintf(OutFile, "VMD_Slave_HandShake() ok!\n");
printf("DC_Slave_HandShake() ok!\n");
}
else
{
fprintf(OutFile, "VMD_Slave_HandShake() Failed!\n");
printf("DC_Slave_HandShake() Failed!\n");
return 0;
}
if(DataRecordTransfer()==1)
{
fprintf(OutFile, "DataRecordTransfer() Ok!\n");
printf("DataRecordTransfer() Ok!\n");
}
else
{
fprintf(OutFile, "DataRecordTransfer() Failed!\n");
printf("DataRecordTransfer() Failed!\n");
return 0;
}
}
else if(dex_mode==DC_UNKNOWN)
{
fprintf(OutFile, "dex_mode=DC_UNKNOWN\n");
return 0;
}
else
return 0;
return 1;
}
/*
Entry point
*/
void main()
{
int c;
int TotalCnt=0;
int FailedCnt=0;
int i;
int FileCnt;
char OutFileName[20];
PassCount=0;
dexloop_cnt=0;
printf("PortOpenGreenleaf()!\n");
port = PortOpenGreenleaf( COM1, 9600L, 'N', 8, 1 );
if ( port->status < ASSUCCESS )
{
printf( "Failed to open the port. Status = %s\n",
CommErrorName( port->status ) );
exit( 1 );
}
for(FileCnt=0; FileCnt<2000; FileCnt++)
{
if(kbhit())
{
if(getch()==0x1b)
break;
}
TotalCnt++;
dex_mode=DC_UNKNOWN;
sprintf(OutFileName, "DEX%05d.log", FileCnt);
OutFile=fopen(OutFileName, "wt");
if(OutFile==NULL)
continue;
fprintf(OutFile, "------------ %d -----------\n", dexloop_cnt);
printf("------------ %d -----------\n", dexloop_cnt++);
#if 1
if( dex_loop()==0 )
FailedCnt++;
#endif
ClearRXBuffer( port );
printf("------------ dex_loop finished -----------\n");
fclose(OutFile);
printf("TotalCnt=%d FailedCnt=%d\n", TotalCnt, FailedCnt);
printf("Sleep 10 seconds!\n");
for(i=0; i<10; i++)
{
if( kbhit() )
{
if( getch()==0x1b )
break;
}
printf("%02d\r", 30-i);
sleep(1);
}
}
printf("PortClose()!\n");
PortClose( port );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -