#include #include #include "..\lib\7188.h" #define NoError 0 #define Error 1 #define NoCard 2 char szCmd1[80]; int ComPort=3; /* this value can be 1/3/4 --> COM 1/3/4 */ int read_touch7(int *TouchType, int *TouchAddr, int *TouchValue); int write_to_easyview_1(int addr, unsigned int value); int write_to_easyview_2(int addr, int onoff); int ascii_to_hex(char c); /* ------------------------------------------------------------------- */ void main() { int now_i1,now_i2,now_i3,now_i4; int old_i1,old_i2,old_i3,old_i4; int i,k,tt,LedMode=1; char c,rep; int TouchType, TouchAddr, TouchValue; int quit=0; unsigned long tled; InitLib(); /* driver initial */ InstallCom(ComPort,9600L,8,0,1); /* COM3, RS-232, full-duplex mode, INT */ /* connect to EasyView */ Init5DigitLed(); /* initial & blank the 5-digit LED */ LedOn(); /* red-LED on */ TimerOpen(); StopWatchStart(0); StopWatchStart(1); tt=0; while(!quit){ StopWatchReadValue(0,&tled); if(tled>=500){ StopWatchStart(0); LedMode=!LedMode; if(LedMode) LedOn(); else LedOff(); } if (IsCom(ComPort)) { if (ComPort!=4) printf("\nReceive COM_%d",ComPort); c=ReadCom(ComPort); if(c==2){ /* receive STX from easyview */ k=read_touch7(&TouchType,&TouchAddr,&TouchValue); if(k==NoError) { if ((TouchType==1) && (TouchValue==0)) { quit=1; } } } } if (ComPort != 4){ if (kbhit4()) { /* is user press PC's keyboard from COM4 */ c=getch4(); /* if this key is Q or Q --> exit this program */ if ((c=='q') || (c=='Q')) { quit=1; } } } StopWatchReadValue(1,&tled); if(tled>=1000){ StopWatchStart(1); tt++; tt&=0x0f; if (ComPort!=4) printf("\nwrite %x to address 6",tt); write_to_easyview_1(6,tt); } } TimerClose(); RestoreCom(ComPort); } /* ------------------------------------------------------------------- */ int read_touch7(int *TouchType, int *TouchAddr, int *TouchValue) { int i,rep,t; char c,str[80]; int retval[20],maxi; char type,onoff; int addr; unsigned int value; if (ComPort!=4) printf(" -> is touch7"); ToCom(ComPort,0x06); /* send ACK to EASYVIEW */ i=0; rep=0; for (;;) { t=0; while (IsCom(ComPort)==0) { t++; if (t>32760) return(1); /* time out */ } c=ReadCom(ComPort); if(i==0) { if (c==1) {type=1; maxi=9;} else if (c==2) {type=2; maxi=7;} else return(2); /* error cmd */ } else if (type==1) { /* DLE will repeat */ if ((i>=1) && (i<=5) && (c==0x10)) { if (rep==0) rep=1; else rep=0; } } if (rep==0) { szCmd1[i]=c; i++; if (i>=maxi) break; } } rec_ok: ToCom(ComPort,0x06); /* send ACK to EASYVIEW */ if (type==1) { Show5DigitLed(1,1); Show5DigitLed(2,szCmd1[1]); Show5DigitLed(ComPort,szCmd1[2]); Show5DigitLed(4,szCmd1[4]); Show5DigitLed(5,szCmd1[5]); addr=szCmd1[1]+szCmd1[2]*256; value=(szCmd1[4]&0xff)+(szCmd1[5]&0xff)*256; *TouchType=1; *TouchAddr=addr; *TouchValue=value; if (ComPort!=4) printf(" * type=1, addr=%d, value=%d",addr,value); write_to_easyview_1(0,value); } else if (type==2) { Show5DigitLed(1,2); Show5DigitLed(2,szCmd1[1]); Show5DigitLed(ComPort,szCmd1[2]); Show5DigitLed(4,szCmd1[3]); addr=szCmd1[1]+szCmd1[2]*256; value=szCmd1[3]; *TouchType=2; *TouchAddr=addr; *TouchValue=value; if (ComPort!=4) printf(" * type=2, addr=%d, value=%d",addr,value); write_to_easyview_2(addr,value); } return(NoError); /* receive OK */ } /* ------------------------------------------------------------------- */ int ascii_to_hex(char c) { if (c<='9') return(c-'0'); else return(c-'A'+10); } /* ------------------------------------------------------------------- */ char cCmd1[9]={ 1, /* 01 */ 0, /* addr_l */ 0, /* addr_h */ 2, /* # */ 0, /* data_l */ 0, /* data_h */ 0x10, /* DLE */ 0x03, /* ETX */ 0, /* checksum */ }; int write_to_easyview_1(int addr, unsigned int value) { int iRet,i,xor,j,retval[20],hh,ll,val_l,val_h,t; char c; ToCom(ComPort,0x02); /* send ACK to EASYVIEW */ t=0; while (IsCom(ComPort)==0){ t++; if (t>100) return(1); /* time out */ DelayTimeMs(1); } c=ReadCom(ComPort); if (c==6) goto rec_ack; /* receive a ACK from easyview */ return(2); rec_ack: ll=addr&0xff; hh=(addr>>8)&0xff; val_l=value&0xff; val_h=(value>>8)&0xff; /* cCmd1[0]=1;*/ /* 01 */ cCmd1[1]=ll; /* addr_l */ cCmd1[2]=hh; /* addr_h */ /* cCmd1[3]=2; */ /* # */ cCmd1[4]=val_l; /* data_l */ cCmd1[5]=val_h; /* data_h */ /* cCmd1[6]=0x10;*/ /* DLE */ /* cCmd1[7]=0x03;*/ /* ETX */ xor=0; for (i=0; i<=7; i++) { xor = xor ^ cCmd1[i]; } cCmd1[8]=xor; /* CheckSum */ for (i=0; i<=8; i++) { retval[i]=ToCom(ComPort,cCmd1[i]); } t=0; while (IsCom(ComPort)==0) { t++; if (t>100) return(1); /* time out */ DelayTimeMs(1); } c=ReadCom(ComPort); if (c==6) goto rec_ack2; /* receive a ACK from easyview */ return(2); rec_ack2: return(0); } /* ------------------------- */ char cCmd2[7]={ 2, /* 02 */ 0, /* addr_l */ 0, /* addr_h */ 0, /* #1/0 */ 0x10, /* DLE */ 0x03, /* ETX */ 0, /* checksum */ }; int write_to_easyview_2(int addr, int onoff) { int iRet,i,xor,j,retval[20],hh,ll,val_l,val_h,t; char c; ToCom(ComPort,0x02); /* send ACK to EASYVIEW */ t=0; while (IsCom(ComPort)==0) { if (++t>100) return(1); /* time out */ DelayTimeMs(1); } c=ReadCom(ComPort); if (/*(iRet==0) &&*/ (c==6)) goto rec_ack; /* receive a ACK from easyview */ return(2); rec_ack: ll=addr&0xff; hh=(addr>>8)&0xff; /*cCmd2[0]=2;*/ /* 02 */ cCmd2[1]=ll; /* addr_l */ cCmd2[2]=hh; /* addr_h */ cCmd2[3]=onoff; /* #1/0 */ /*cCmd2[4]=0x10; */ /* DLE */ /*cCmd2[5]=0x03; */ /* ETX */ xor=0; for (i=0; i<=5; i++) { xor = xor ^ cCmd2[i]; } cCmd2[6]=xor; /* CheckSum */ for (i=0; i<=6; i++) { retval[i]=ToCom(ComPort,cCmd2[i]); } t=0; while (IsCom(ComPort)==0) { if (++t>100) return(1); /* time out */ DelayTimeMs(1); } c=ReadCom(ComPort); if (c==6) goto rec_ack2; /* receive a ACK from easyview */ return(2); rec_ack2: return(0); }