📄 dex_me.c
字号:
/*
$Log: C:/program/vendin~1/source/dex_me/vcs/dex_me.c_v $
*
* Rev 1.9 01 May 2006 13:29:16
*
* Rev 1.8 29 Dec 2004 11:42:38
* Before VMD master mode!
*
* Rev 1.7 08 Sep 2004 17:15:34
*
* Rev 1.6 28 Apr 2004 14:20:00
*
* Rev 1.5 13 Apr 2004 19:26:24
*
* Rev 1.4 12 Apr 2004 20:55:52
* crc correct!
*
* Rev 1.3 12 Apr 2004 20:31:18
*
* Rev 1.2 12 Apr 2004 19:53:18
* Work!
*
* Rev 1.1 10 Apr 2004 0:58:20
* 2nd handshak ok!
*
* Rev 1.0 10 Apr 2004 0:02:26
* Initial revision.
*/
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include "asiports.h"
#include "asciidef.h"
#include "ibmkeys.h"
/*
*/
//char CHANGER_IS_SLAVE[]="1234567890RR01L01"; /* COM_ID R REV_LEVEL */
char CHANGER_IS_SLAVE[]="XYZ1234567RR01L01"; /* COM_ID R REV_LEVEL */
char VMD_IS_MASTER[]="001234567890R01L01";
int CommID_index;
char CommID_Buffer[36];
int Dex_Buffer_index;
char Dex_Buffer[256];
char DC_Answer;
FILE *OutFile;
int dexloop_cnt;
int ErrorCnt;
/*
*/
#define MAX_ERROR_CNT 10
/*
*/
#define DEX_SOH 0x01
#define DEX_STX 0x02
#define DEX_ETX 0x03
#define DEX_EOT 0x04
#define DEX_ENQ 0x05
#define DEX_LF 0x0a
#define DEX_CR 0x0d
#define DEX_DLE 0x10
#define DEX_NAK 0x15
#define DEX_SYN 0x16
#define DEX_ETB 0x17
#define DEX_ACK0 0x30 // DLE 0
#define DEX_ACK1 0x31 // DLE 1
#define DEX_WACK 0x3b // WACK
/*
Response codes
*/
#define DEX_R_OK 0x00
#define DEX_R_UN_COMID 0x01
#define DEX_R_UN_REV 0x02
#define DEX_R_OP_CONFLIC 0x03
#define DEX_R_NO_DATA 0x04
#define DEX_R_UN_ERR 0x05
#define DEX_R_MAN_S 0x90
#define SMALL_DELAY 50
#define BIG_DELAY 150
#define READ_CHAR_DELAY 200
#define DC_MASTER 0
#define DC_SLAVE 1
#define DC_UNKNOWN 2
static int dex_mode;
static int PassCount;
static PORT *port;
unsigned int BCC, BCC_0, BCC_1, BCC_14, DATA_0, X2, X15, X16;
/*
*/
unsigned int GetCRC(void);
int VerifyCRC(unsigned int CRC);
void DC_Alternate_Answer(void);
void Reset_Alternate_Answer(void);
void Reset_ErrorC(void);
char Check_ErrorC(void);
////////////////////////////////////////////////////////////////////////
/* CRC check routine */
void crc_16(unsigned char data)
{
int j;
for (j=0; j<8; j++)
{
DATA_0 = (data >> j) & 0x0001;
BCC_0 = (BCC & 0x0001);
BCC_1 = (BCC >> 1) & 0x0001;
BCC_14 = (BCC >> 14) & 0x0001;
X16 = (BCC_0 ^ DATA_0) & 0x0001; // bit15 of BCC after shift
X15 = (BCC_1 ^ X16) & 0x0001; // bit0 of BCC after shift
X2 = (BCC_14 ^ X16) & 0x0001; // bit13 of BCC after shift
BCC = BCC >> 1;
BCC = BCC & 0x5FFE;
BCC = BCC | (X15);
BCC = BCC | (X2 << 13);
BCC = BCC | (X16 << 15);
}
}
/*
*/
int WhoIsMaster(void)
{
int c;
PassCount=0;
Reset_ErrorC();
ClearRXBuffer( port );
for(;;)
{
if(kbhit())
{
if(getch()==0x1b)
return DC_UNKNOWN;
}
WriteChar( port, DEX_ENQ ); // Send a ENQ to VMD
delay(200);
c=ReadCharTimed( port, READ_CHAR_DELAY );
if(c>=ASSUCCESS)
{
// got it!
Reset_ErrorC();
c&=0xff;
fprintf(OutFile, "0x%02x\n", c);
printf("0x%02x\n", c);
}
else
{
// Time out
fprintf(OutFile, "ReadCharTimed() out!\n");
printf("ReadCharTimed() out!\n");
if(Check_ErrorC()==0)
{
return DC_UNKNOWN;
}
}
switch(c)
{
case DEX_NAK:
printf("Got a NAK\n");
return DC_UNKNOWN;
case DEX_ENQ:
return DC_MASTER;
case DEX_DLE:
fprintf(OutFile, "dex_mode=DC_SLAVE\n");
printf("dex_mode=DC_SLAVE\n");
c=ReadCharTimed( port, READ_CHAR_DELAY );
if(c>=ASSUCCESS)
{
// got it!
c&=0xff;
fprintf(OutFile, "0x%02x [%c]\n", c, c);
printf("0x%02x [%c]\n", c, c);
return DC_SLAVE;
}
else
{
// Time out
fprintf(OutFile, "ReadCharTimed() out!\n");
printf("ReadCharTimed() out!\n");
}
break;
defaule:
fprintf(OutFile, "PassCount=%d\n", PassCount++);
printf("PassCount=%d\n", PassCount++);
break;
}
}
}
/*
*/
unsigned char ForWack(void)
{
int c=0;
WriteCharTimed(port, DEX_ENQ, SMALL_DELAY);
c=ReadCharTimed(port, READ_CHAR_DELAY);
if(c>=ASSUCCESS)
{
c&=0xff;
if(c==DEX_DLE)
{
c=ReadCharTimed(port, READ_CHAR_DELAY);
if(c>=ASSUCCESS)
{
c&=0xff;
return c;
}
}
}
return c;
}
/*
*/
void Reset_ErrorC(void)
{
ErrorCnt=0;
}
/*
*/
char Check_ErrorC(void)
{
fprintf(OutFile, "W");
printf("W");
ErrorCnt++;
if( ErrorCnt>MAX_ERROR_CNT )
return 0;
return 1;
}
/*
*/
int DC_master_handshake(char *CommID)
{
int i;
int c;
// fprintf(OutFile, "-------- DC=master_handshake()!\n");
// printf("-------- DC_master_handshake()!\n");
// WriteChar( port, DEX_ENQ );
// delay(100);
Reset_ErrorC();
for(;;)
{
if(kbhit())
{
if(getch()==0x1b)
return 0;
}
WriteChar( port, DEX_ENQ );
delay(200);
c=ReadCharTimed( port, READ_CHAR_DELAY );
if(c>=ASSUCCESS)
{
Reset_ErrorC();
c&=0xff;
fprintf(OutFile, "0x%02x [%c]", c, c);
printf("0x%02x [%c]", c, c);
if(c==DEX_DLE)
{
c=ReadCharTimed( port, READ_CHAR_DELAY );
if(c>=ASSUCCESS)
{
c&=0xff;
if(c==DEX_ACK0)
{
fprintf(OutFile, "Got DLE 0!\n");
printf("Got DLE 0!\n");
break;
}
}
}
}
else
{
if(Check_ErrorC()==0)
return 0;
}
fprintf(OutFile, "\n");
printf("\n");
}
/*
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);
return 0;
}
}
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 DC_Slave_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<18; 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;
}
/*
----------------------------------------------------------------------
VMD master mode
----------------------------------------------------------------------
*/
/*
*/
int VMD_Slave_handshake(char *CommID)
{
int i;
int c;
fprintf(OutFile, "-------- DC=master_handshake()!\n");
printf("-------- DC_master_handshake()!\n");
// WriteChar( port, DEX_ENQ );
Reset_ErrorC();
for(;;)
{
if(kbhit())
{
if(getch()==0x1b)
return 0;
}
WriteChar( port, DEX_ENQ );
delay(100);
c=ReadCharTimed( port, READ_CHAR_DELAY );
if(c>=ASSUCCESS)
{
Reset_ErrorC();
c&=0xff;
fprintf(OutFile, "0x%02x [%c]", c, c);
printf("0x%02x [%c]", c, c);
if(c==DEX_DLE)
{
c=ReadCharTimed( port, READ_CHAR_DELAY );
if(c>=ASSUCCESS)
{
c&=0xff;
if(c==DEX_ACK0)
{
fprintf(OutFile, "Got DLE 0!\n");
printf("Got DLE 0!\n");
break;
}
}
}
}
else
{
if(Check_ErrorC()==0)
return 0;
}
fprintf(OutFile, "\n");
printf("\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -