/******************************************************************************** INT_Ch.c: Multi-channels as rising or falling edge interrupt in single slot. Compiler: BC++ 3.1, Turbo C ++ 1.01(3.01) (free from http://community.borland.com/museum) MSC 6.0, MSVC 1.52. Compile mode: large Project: INT_Ch.c ..\Lib\(8000E.Lib,8000.Lib) [24 Mar,2005] by Bill *********************************************************************************/ #include "..\lib\ip8000\8000A.h" #include "..\lib\ip8000\8048w.h" #include "..\lib\ip8000\tcpip32.h" #include #define COUNTS 8 //count TS in module i8053W+i8048W #define TIMERETH 10000 //timer of Eth error for PLC server, this timer shows how often send message to server #define TIMERON 900000 // timer for send ON signal after OFF signal, sec #define TESTTIMER 30000 // test timer send ON #define PORT 10000 #define SERVERADDR_1 "10.1.0.99" #define MAXBUFSIZE 260 #define DEBUG struct SST{ int iSocket; int active; int con; }; void ISR_Function(int slot); int ConnectToServer(void); int MBFC15_request(unsigned char* request); void MBFC15_reply(unsigned char* reply, int rBytes); int iSlot8048=1, iSlot8053=0, r0=0, r1=0, r2=0, r3=0, r4=0, SignalON=0; int f0=0, f1=0, f2=0, f3=0, f4=0; unsigned char readyValue[COUNTS]; int er=0; void main(void) { unsigned long lStartTime=0, LastSend=0; unsigned int DI16_cur=0, di=0; int i, show_error=0, iRet=0; int ret,ver,SlotType; unsigned long wait; unsigned char cIP_Net[4]; unsigned char cMASK_Net[4]; unsigned char cGATEWAY_Net[4]; unsigned char cIP_Net2[4]; unsigned char cMASK_Net2[4]; unsigned char cGATEWAY_Net2[4]; int a=0; //initialization Print("------------Open 8000-----------\n\r"); ver=GetLibVersion(); Print("Library version is %d.%02d\n\r",ver>>8,ver&0xff); SlotType = GetNumberOfSlot();//slot4 or slot8 Print("Current Slot Type = %d\n\r",SlotType); InitLib(); Init5DigitLed(); Show5DigitLedSeg(1, 0x46); Show5DigitLedSeg(2,0x3e); Show5DigitLedSeg(3,0x76); Show5DigitLedSeg(4,0); Show5DigitLedSeg(5,0); //DIO_DO_8(0, 0); NetStart(); GetIp(cIP_Net); Print("IP= %d.%d.%d.%d\n",cIP_Net[0],cIP_Net[1],cIP_Net[2],cIP_Net[3]); GetMask(cMASK_Net); Print("MASK= %d.%d.%d.%d\n",cMASK_Net[0],cMASK_Net[1],cMASK_Net[2],cMASK_Net[3]); GetGateway(cGATEWAY_Net); Print("GATEWAY= %d.%d.%d.%d\n",cGATEWAY_Net[0],cGATEWAY_Net[1],cGATEWAY_Net[2],cGATEWAY_Net[3]); GetIp2(cIP_Net2); Print("IP2= %d.%d.%d.%d\n",cIP_Net2[0],cIP_Net2[1],cIP_Net2[2],cIP_Net2[3]); GetMask2(cMASK_Net2); Print("MASK2= %d.%d.%d.%d\n",cMASK_Net2[0],cMASK_Net2[1],cMASK_Net2[2],cMASK_Net2[3]); GetGateway2(cGATEWAY_Net2); Print("GATEWAY2= %d.%d.%d.%d\n",cGATEWAY_Net2[0],cGATEWAY_Net2[1],cGATEWAY_Net2[2],cGATEWAY_Net2[3]); //if(NetStart()<0) /* { Print("Initialize TCP Client failure\n\r"); return; } */ i8048W_Init(iSlot8048); InstallSlotLevelIsr(iSlot8048,ISR_Function); //Enable channel 0 as rising edge interrupt. i8048W_Set_FallingReg(iSlot8048,0,1); //Channel 0 , 1=Enable i8048W_Set_FallingReg(iSlot8048,1,1); //Channel 1 , 1=Enable i8048W_Set_FallingReg(iSlot8048,2,1); //Channel 2 , 1=Enable i8048W_Set_FallingReg(iSlot8048,3,1); //Channel 3 , 1=Enable i8048W_Set_FallingReg(iSlot8048,4,1); //Channel 4, 1=Enable i8048W_Set_RisingReg(iSlot8048,0,1); //Channel 0 , 1=Enable i8048W_Set_RisingReg(iSlot8048,1,1); //Channel 1 , 1=Enable i8048W_Set_RisingReg(iSlot8048,2,1); //Channel 2 , 1=Enable i8048W_Set_RisingReg(iSlot8048,3,1); //Channel 3 , 1=Enable i8048W_Set_RisingReg(iSlot8048,4,1); //Channel 4, 1=Enable // i8048W_UnFreezeINT(iSlot8048); memset(readyValue, 0, COUNTS); //end initialization for(;;) { #ifdef DEBUG if(Kbhit()&&Getch()==27) break; #endif //fill readyValue array if(r0||r1||r2||r3||r4) { Print("f0=%i f1=%i f2=%i f3=%i f4=%i\n\r", f0, f1, f2, f3, f4); Print("r0=%i r1=%i r2=%i r3=%i r4=%i\n\r", r0, r1, r2, r3, r4); readyValue[5]=r0|r1; readyValue[6]=r2|r3|r4; Print("readyValue=%i %i\n\r", readyValue[5], readyValue[6]); if (readyValue[6]) {SignalON=1; readyValue[6]=0;} r0=0; r1=0; r2=0; r3=0; r4=0; //f0=0; f1=0; f2=0; f3=0; f4=0; } //is_value else { readyValue[5]=1; readyValue[6]=1; } //fill anather signals if(NameOfModule[iSlot8053]==53&&ModuleType[iSlot8053]==195) // check on new module!!!!!!!!!!! readyValue[iSlot8053]=0; else readyValue[iSlot8053]=1; DI16_cur=DI_16(iSlot8053); di=DI16_cur<<14; di>>=15; readyValue[1]=di; di=DI16_cur<<13; di>>=15; readyValue[2]=di; di=DI16_cur<<12; di>>=15; readyValue[3]=di; di=DI16_cur<<11; di>>=15; readyValue[4]=di; if(NameOfModule[iSlot8048]==48 && (ModuleType[iSlot8048] & _PARALLEL)) readyValue[7]=0; else readyValue[7]=1; //??????? ????????? ??????? if(readyValue[5]==1) { if((iRet=ConnectToServer())>0) { lStartTime=GetTimeTicks(); LastSend=lStartTime; #ifdef DEBUG Print("send turnOFF signal at %lu lStertTime=%lu\n\r\n\r", LastSend, lStartTime); #endif er=0; } continue; } if(SignalON) { wait=GetTimeTicks(); SetLedL3(1); if(a==0) a=Print("Got SignalON at %lu\n\r", wait); if(wait-lStartTime>=TESTTIMER/*TIMERON*/) //15 min { readyValue[6]=1; SignalON=0; if((iRet=ConnectToServer())>0) { lStartTime=0; LastSend=GetTimeTicks(); SetLedL3(0); #ifdef DEBUG Print("Send turnON at %lu\n\r", LastSend); #endif er=0; a=0; } continue; } } SetLedL1(2); if((GetTimeTicks()-LastSend)>TIMERETH) { if((iRet=ConnectToServer())>0) { LastSend=GetTimeTicks(); #ifdef DEBUG Print("Send All Signals at %lu\n\r", LastSend); #endif er=0; } } for(i=0; i0) {show_error=1; break;} if(iRet==-1) {show_error=1; i=14; SetLedL1(0);} if(show_error==1) { Show5DigitLed(1, 14); Show5DigitLedSeg(2, 0x46); Show5DigitLedSeg(3, 0x46); Show5DigitLed(4, (i+1)/10); Show5DigitLed(5, (i+1)%10); show_error=0; } else { Show5DigitLedSeg(1, 0x46); Show5DigitLedSeg(2,0x3e); Show5DigitLedSeg(3,0x76); Show5DigitLedSeg(4,0); Show5DigitLedSeg(5,0); } } //end for(;;) Nterm(); i8048W_UnFreezeCPU(iSlot8048); UnInstallSlotLevelIsr(iSlot8048); } /******************************************************** Please don's use the Print or printCom1 function in ISR, these function will cause another interrupt problem. *********************************************************/ void ISR_Function(int slot) { // memset(i8048W_FallingEventCount[slot],0,8*sizeof(unsigned long)); if(i8048W_Read_FallingEvent(slot,0)) { f0=1; } if(i8048W_Read_FallingEvent(slot,1)) { f1=1; } if(i8048W_Read_FallingEvent(slot,2)) { f2=1; } if(i8048W_Read_FallingEvent(slot,3)) { f3=1; } if(i8048W_Read_FallingEvent(slot,4)) { f4=1; } _asm sti if(i8048W_Read_RisingEvent(slot,0)) { if(f0){r0=1; f0=0;} } if(i8048W_Read_RisingEvent(slot,1)) { if(f1){r1=1; f1=0;} } if(i8048W_Read_RisingEvent(slot,2)) { if(f2){r2=1; f2=0;} } if(i8048W_Read_RisingEvent(slot,3)) { if(f3){r3=1; f3=0;} } if(i8048W_Read_RisingEvent(slot,4)) { if(f4){r4=1; f4=0;} } i8048W_UnFreezeINT(slot); } int ConnectToServer() { int iRet=0, sBytes=0; unsigned char request[MAXBUFSIZE]; int iClient=0; struct sockaddr_in sktRemoteServer; unsigned long lStartTimeTick; memset(request, 0, MAXBUFSIZE); memset(&sktRemoteServer, 0, sizeof(sktRemoteServer)); // bzero is a unix system call iClient=socket(PF_INET,SOCK_STREAM,0); if(iClient<0) { #ifdef DEBUG Print("Create socket error=%d\n\r", errno); #endif Nterm(); return -1; } sktRemoteServer.sin_family = AF_INET; sktRemoteServer.sin_addr.s_addr = inet_addr(SERVERADDR_1); sktRemoteServer.sin_port = htons(PORT); iRet=connect(iClient, (struct sockaddr *)&sktRemoteServer, sizeof(sktRemoteServer)); if(iRet>=0) { sBytes=MBFC15_request(request); //check send(iClient, request, sBytes, 0); //6 bytes, 0:MSG_OOB YIELD(); sBytes=recv(iClient, request, MAXBUFSIZE, 0); YIELD(); if(sBytes) MBFC15_reply(request, sBytes); shutdown(iClient,0); closesocket(iClient); iRet=1; } else { if(er==0) {Print("Faulure connection %i\n\r",errno, iRet=-1); er=1;} } return iRet; } /* int ConnectToServer() { int iRet=0, sBytes=0; unsigned char request[MAXBUFSIZE]; struct SST socketStatus; struct sockaddr_in sktRemoteServer; unsigned long lStartTimeTick; memset(request, 0, MAXBUFSIZE); memset(&socketStatus, 0, sizeof(socketStatus)); if((socketStatus.iSocket=socket(PF_INET,SOCK_STREAM,0))>=0) { //Print("socketStatus.iSocket=%d\n\r", socketStatus.iSocket); socketStatus.active=1; YIELD(); memset(&sktRemoteServer, 0, sizeof(sktRemoteServer)); // bzero is a unix system call sktRemoteServer.sin_family = AF_INET; sktRemoteServer.sin_addr.s_addr = inet_addr(SERVERADDR_1); sktRemoteServer.sin_port = htons(PORT); YIELD(); SOCKET_NOBLOCK(socketStatus.iSocket); //Set Non-blocking mode if(connect(socketStatus.iSocket, (struct sockaddr *)&sktRemoteServer, sizeof(sktRemoteServer))) // unsuccessful connection not 0 { if(errno==EINPROGRESS) { lStartTimeTick=GetTimeTicks(); while((GetTimeTicks()-lStartTimeTick)<1000) { RefreshWDT(); YIELD(); if(SOCKET_ISOPEN(socketStatus.iSocket)) {socketStatus.con=1; break;} } if(socketStatus.con==0) { closesocket(socketStatus.iSocket); socketStatus.active=0;} } else { closesocket(socketStatus.iSocket); socketStatus.active=0; } } else { socketStatus.con=1; } } else socketStatus.active=0; // create socket faulure if(socketStatus.con) { SOCKET_BLOCK(socketStatus.iSocket); sBytes=MBFC15_request(request); //check YIELD(); send(socketStatus.iSocket, request, sBytes, 0); //6 bytes, 0:MSG_OOB YIELD(); sBytes=recv(socketStatus.iSocket, request, MAXBUFSIZE, 0); if(sBytes) MBFC15_reply(request, sBytes); shutdown(socketStatus.iSocket,0); closesocket(socketStatus.iSocket); socketStatus.active=0; socketStatus.con=0; return 1; } else { //Print("NOT SEND\n\r"); RefreshWDT(); return -1; } } */ /****************************** //ModbusTCP functions *****************************/ int MBFC15_request(unsigned char* request) { unsigned char temp=0; int i; int quantity=COUNTS; int size=0; //??????????? ???????? ???????? for(i=0; i>8;; request[11]=quantity; if((quantity%8)>0) //if the remainder is different of 0 then N=N+1; request[12]=quantity/8+1; else request[12]=quantity/8; request[13]=temp; size=14; return size; } void MBFC15_reply(unsigned char* reply, int rBytes) { if(rBytes>8) { #ifdef DEBUG if(reply[7]==0x8f) Print("Exception of FC15 %d\n\r", reply[8]); #endif } return; }