///////////////////////////////////////////////////////////////// // I-87120 User design for DeviceNet I/O control library demo // // Demo devices: I-87120 + DeviceNet devices: Support Polling // // IO Connection, produced size: 2 byte, consumed size: 2 byte// ///////////////////////////////////////////////////////////////// #include "..\..\c87s110.h" #include #include #include #define MasterMACID 0x0A unsigned long tx,rx,irq; void UserIrqFunc(char INTT) { // If CAN bus interrupt has occur, the irq++ will be excuted if (INTT) irq++; } void UserInitFunc(void) { // The function will be excuted first after I-87120 is power on tx=0; rx=0; irq=0; } void UserDefCmdFunc(unsigned int CmdLength, char * UserCmd) { // User define command, the UserCmd[0] must be '_' unsigned char data[8]; int Ret; if (CmdLength<2){ ToCom1Str(">CmdErr\r"); return; } switch (UserCmd[1]){ case 'A': //Send Allocate Connection message { //_AIDT: ID: device id, 0x00~0x3F, // T: Connection Type, 1: Explicit, 2: Polling char TempBuf[10],*endptr; unsigned long DeviceMACID; unsigned char ConnType; //Group 2 Only Unconnected Explicit Request Messages memcpy(TempBuf,&UserCmd[2],2); TempBuf[2]=0; DeviceMACID = (unsigned long)((strtol(TempBuf,&endptr,16)<<3)|0x406); ConnType=(unsigned char)ascii_to_hex(UserCmd[4]); memset(data,0,8); data[0] = MasterMACID; data[1] = 0x4B; data[2] = 0x03; data[3] = 0x01; data[4] = ConnType; data[5] = MasterMACID; Ret=SendCANMsg(0,DeviceMACID,0,6,data); if (Ret) ToCom1Str(">SendFaile\r"); else{ tx++; } } break; case 'S': //Set Connection Expected_Packet_Rate { //_SIDTEEEE: ID: device id, 0x00~0x3F, // T: Connection Type, 1: Explicit, 2: Polling // EEEE: Expected_Packet_Rate From 0x0000 ~ 0xFFFF char TempBuf[10],*endptr=NULL; unsigned long DeviceMACID; unsigned char ConnType; //Master's Explicit Request memcpy(TempBuf,&UserCmd[2],2); TempBuf[2]=0; DeviceMACID = (unsigned long)((strtol(TempBuf,&endptr,16)<<3)|0x404); ConnType=(unsigned char)ascii_to_hex(UserCmd[4]); memset(data,0,8); data[0] = MasterMACID; data[1] = 0x10; data[2] = 0x05; data[3] = ConnType; data[4] = 0x09; data[5] = (ascii_to_hex(UserCmd[5])<<4)+ascii_to_hex(UserCmd[6]); data[6] = (ascii_to_hex(UserCmd[7])<<4)+ascii_to_hex(UserCmd[8]); Ret=SendCANMsg(0,DeviceMACID,0,7,data); if (Ret) ToCom1Str(">SendFaile\r"); else{ tx++; } } break; case 'P': //Send Master's I/O Poll Command Message { //_PIDLData: ID: device id, 0x00~0x3F, // L: lengthe of data (value must be 0 to 8) // Data: at most 8 bytes data. char TempBuf[10],*endptr=NULL; unsigned long DeviceMACID; unsigned char DataLen,i,j; //Master's I/O Poll Command Message memcpy(TempBuf,&UserCmd[2],2); TempBuf[2]=0; DeviceMACID = (unsigned long)((strtol(TempBuf,&endptr,16)<<3)|0x405); DataLen=(unsigned char)ascii_to_hex(UserCmd[4]); if (DataLen>8) { ToCom1Str(">DataLen Parameter Error\r"); return; } memset(data,0,8); for(i=0,j=0;iSendFaile\r"); else{ tx++; } } break; case 'C': //Close Allocate Connection message { //_CIDT: ID: device id, 0x00~0x3F, // T: Connection Type, 1: Explicit, 2: Polling char TempBuf[10],*endptr; unsigned long DeviceMACID; unsigned char ConnType; //Group 2 Only Unconnected Explicit Request Messages memcpy(TempBuf,&UserCmd[2],2); TempBuf[2]=0; DeviceMACID = (unsigned long)((strtol(TempBuf,&endptr,16)<<3)|0x406); ConnType=(unsigned char)ascii_to_hex(UserCmd[4]); memset(data,0,8); data[0] = MasterMACID; data[1] = 0x4C; data[2] = 0x03; data[3] = 0x01; data[4] = ConnType; //data[5] = MasterMACID; Ret=SendCANMsg(0,DeviceMACID,0,6,data); if (Ret) ToCom1Str(">SendFaile\r"); else{ tx++; } } break; case 'T': switch(UserCmd[2]){ case 's':// Total send message count { char strtemp[50]; sprintf(strtemp,"Total send CAN message:%ld\r",tx); ToCom1Str(strtemp); } break; case 'r':// Total receive message count { char strtemp[50]; sprintf(strtemp,"Total receive CAN message:%ld\r",rx); ToCom1Str(strtemp); } break; } break; default: ToCom1Str(">CmdNoSupport\r"); } } void UserDefBinaryFunc(unsigned int CmdLength, char * UserCmd) { // Process user-defined command for binary code, the UserCmd[0] must be '|' if (CmdLength); switch (UserCmd[1]){ } } void UserLoopFunc(void) { // The function will be excuted in while loop after the I-87120 has be configured int Ret,i; unsigned char Mode,RTR,DataLen,Data[8]; unsigned long ID; char strtemp[100],cattemp[20]; Ret=GetCANMsg(&Mode,&ID,&RTR,&DataLen,Data); // Loop for receive CAN message if(!Ret){ rx++; if(RTR){ // RTR if(Mode){ // 29 bits id sprintf(strtemp,"RTR: id:%08X , ",ID); } else{ // 11 bits id sprintf(strtemp,"RTR: id:%03X , ",ID); } sprintf(cattemp,"dlen:%d",DataLen); strcat(strtemp,cattemp); if(DataLen){ sprintf(cattemp," , data: "); strcat(strtemp,cattemp); for(i=0;i