#include #include #include"..\lib\7188.h" /* 7188 send to s7 format (for D/I change state) (C1,C2) --> module address C3 --> 0=D/I OFF, 1=D/I ON C4 --> D/I channel number C5 --> 0x0d s7 send to 7188 format (for D/O change state) (C1,C2) --> module address C3 --> 0=D/I OFF, 1=D/I ON (C4,C5) --> D/O channel number C6 --> 0x0d (C1-C5) show to LED 1-5 */ int read_7052(char cmd[]); int send_to_s7(int address, int new, int old); int read_7053(char cmd[]); int write_7043(char str[]); int ascii_to_hex(char c); /* ------------------------------------------------------------------- */ void main() { int now_i1,now_i2,now_i3,now_i4, now_i7; int old_i1,old_i2,old_i3,old_i4, old_i7; int j; char c,str[80]; InitLib(); /* driver initial */ InstallCom(1,9600L,8,0,1); /* COM1, RS-232, full-duplex mode, INT */ /* Note : JP1 must select RS-232 */ /* connect to SIEMEMS S7 */ InstallCom(2,9600L,8,0,1); /* COM2, RS-485, half-duplex mode, INT */ /* --> need to control 485 direction */ /* --> initial 485 direction = Receive */ /* --> address 1-4 = 7052 */ /* --> address 5-6 = 7043 */ /* COM4, RS-232, connect to PC's COM port for download */ Init5DigitLed(); /* initial & blank the 5-digit LED */ Show5DigitLed(3,17); /* - */ LedOn(); /* red-LED on */ old_i1=old_i2=old_i3=old_i4=-1; j=0; for (;;) { j++; j=j&0x01; if (j==0) LedOn(); else LedOff(); now_i1=read_7052("$016"); if (now_i1 != old_i1) { printf("\nNew i1=%x",now_i1); /* show to COM4 for debug */ send_to_s7(1,now_i1,old_i1); /* send to S7 for D/I change state */ old_i1=now_i1; } now_i2=read_7052("$026"); if (now_i2 != old_i2) { printf("\nNew i2=%x",now_i2); /* show to COM4 for debug */ send_to_s7(2,now_i2,old_i2); /* send to S7 for D/I change state */ old_i2=now_i2; } now_i3=read_7052("$036"); if (now_i3 != old_i3) { printf("\nNew i3=%x",now_i3); /* show to COM4 for debug */ send_to_s7(3,now_i3,old_i3); /* send to S7 for D/I change state */ old_i3=now_i3; } now_i4=read_7052("$046"); if (now_i4 != old_i4) { printf("\nNew i4=%x",now_i4); /* show to COM4 for debug */ send_to_s7(4,now_i4,old_i4); /* send to S7 for D/I change state */ old_i4=now_i4; } now_i7=read_7053("@07"); if (now_i7 != old_i7) { printf("\nNew i7=%x",now_i7); /* show to COM4 for debug */ send_to_s7(7,now_i7,old_i7); /* send to S7 for D/I change state */ old_i4=now_i4; } if (IsCom(1)!=0) { /* s7 send data to 7188 */ ReceiveResponseFrom7000(1, str, 60000L, 0); write_7043(str); /* echo OK to s7 ToCom(1,'0'); ToCom(1,'1'); ToCom(1,0x0d); */ } DelayTimeMs(100); if (kbhit()) { /* is user press PC's keyboard from COM4 */ c=getch(); /* if this key is Q or Q --> exit this program */ if ((c=='q') || (c=='Q')) { RestoreCom(1); RestoreCom(2); return; } } } } /* ------------------------------------------------------------------- */ int read_7052(char cmd[]) { char str[80],h,l; SendCmdTo7000(2, cmd, 0); ReceiveResponseFrom7000(2, str, 60000L, 0); h=ascii_to_hex(str[1]); l=ascii_to_hex(str[2]); return(h*16+l); } /* ------------------------------------------------------------------- */ int read_7053(char cmd[]) { char str[80],i1,i2,i3,i4; SendCmdTo7000(2, cmd, 0); ReceiveResponseFrom7000(2, str, 60000L, 0); i1=ascii_to_hex(str[1]); i2=ascii_to_hex(str[2]); i3=ascii_to_hex(str[3]); i4=ascii_to_hex(str[4]); return(i4+i3*16+i2*16*16+i1*16*16*16); } /* ------------------------------------------------------------------- */ /* S7 is connected to COM1 */ int send_to_s7(int address, int new, int old) { char cmd[80],h,l,c; char str[8],c1,c2; int a1,a2,v1,v2,xor; xor=new^old; printf("\nxor=%x",xor); v2=0; while (xor != 0) { c=xor&0x01; if (c==1) { v1=new&0x01; break; } xor=xor>>1; new=new>>1; v2++; } a2=address&0x0f; address=address>>4; a1=address&0x0f; Show5DigitLed(1,a1); Show5DigitLed(2,a2); Show5DigitLed(3,v1); Show5DigitLed(4,v2); Show5DigitLed(5,0x0d); cmd[0]=hex_to_ascii[a1]; /* address H-nibble */ cmd[1]=hex_to_ascii[a2]; /* address L-nibble */ cmd[2]=hex_to_ascii[v1]; /* value H-nibble */ cmd[3]=hex_to_ascii[v2]; /* value L-nibble */ cmd[4]=0; SendCmdTo7000(1, cmd, 0); /* c1 & c2 for check /* ReceiveResponseFrom7000(2, str, 60000L, 0); */ /* c1=str[0]; */ /* c2=str[1]; */ /* if (c1!='0') return(-1); */ /* check error */ /* if (c2!='1') return(-1); */ /* check error */ /* return(0); */ /* check OK */ } /* ------------------------------------------------------------------- */ /* command format of 7043 #AAA?0(v1)(od) */ int write_7043(char str[]) { int a1,a2,v1,v2,v3,channel; char cmd[80]; printf("\n7188 receive from COM1=%s",str); cmd[0]='#'; cmd[1]=str[0]; /* AA */ cmd[2]=str[1]; a1=ascii_to_hex(str[0]); a2=ascii_to_hex(str[1]); v1=ascii_to_hex(str[2]); v2=ascii_to_hex(str[3]); v3=ascii_to_hex(str[4]); Show5DigitLed(1,a1); Show5DigitLed(2,a2); Show5DigitLed(3,v1); Show5DigitLed(4,v2); Show5DigitLed(5,v3); channel=v2*10+v3; if (channel>7) { cmd[3]='B'; cmd[4]=hex_to_ascii[channel-8]; } else { cmd[3]='A'; cmd[4]=hex_to_ascii[channel]; } cmd[5]='0'; cmd[6]=str[2]; cmd[7]=0; printf("\nsend to 7053 = %s",cmd); SendCmdTo7000(2, cmd, 0); ReceiveResponseFrom7000(2, str, 60000L, 0); printf("\nresponse of 7053 = %s",str); return NoError; }