void decodeIt(void) { if(received[0] == SlaveAddress){ if(checkCRC()){ if(received[1] == 0x01){ readCoil(); } else if(received[1] == 0x02){ readInputCoil(); } else if(received[1] == 0x03){ readReg(); } else if(received[1] == 0x04){ readInputReg(); } else if(received[1] == 0x05){ writeCoil(); } else if(received[1] == 0x06){ writeReg(); } else if(received[1] == 0x10){ writeMultipleRegs(); } else if(received[1] == 0x0F){ writeMultipleCoils(); } else{ response[0] = 0; //error this does nothing though.. } } } modbusMessage = 0; }
//检查uart0数据 void checkComm0Modbus(void) { uint16 crcData; uint16 tempData; if(receCount > 4) { switch(receBuf[1]) { case 1://读取线圈状态(读取点 16位以内) case 3://读取保持寄存器(一个或多个) case 5://强制单个线圈 case 6://设置单个寄存器 if(receCount >= 8) {//接收完成一组数据 //应该关闭接收中断 if(receBuf[0]==localAddr && checkoutError==0) { crcData = crc16(receBuf,6); if(crcData == receBuf[7]+(receBuf[6]<<8)) {//校验正确 if(receBuf[1] == 1) {//读取线圈状态(读取点 16位以内) readCoil(); } else if(receBuf[1] == 3) {//读取保持寄存器(一个或多个) readRegisters(); } else if(receBuf[1] == 5) {//强制单个线圈 forceSingleCoil(); } else if(receBuf[1] == 6) { //presetSingleRegister(); } } } receCount = 0; checkoutError = 0; } break; case 15://设置多个线圈 tempData = receBuf[6]; tempData += 9; //数据个数 if(receCount >= tempData) { if(receBuf[0]==localAddr && checkoutError==0) { crcData = crc16(receBuf,tempData-2); if(crcData == (receBuf[tempData-2]<<8)+ receBuf[tempData-1]) { //forceMultipleCoils(); } } receCount = 0; checkoutError = 0; } break; case 16://设置多个寄存器 tempData = (receBuf[4]<<8) + receBuf[5]; tempData = tempData * 2; //数据个数 tempData += 9; if(receCount >= tempData) { if(receBuf[0]==localAddr && checkoutError==0) { crcData = crc16(receBuf,tempData-2); if(crcData == (receBuf[tempData-2]<<8)+ receBuf[tempData-1]) { presetMultipleRegisters(); } } receCount = 0; checkoutError = 0; } break; default: break; } } }//void checkComm0(void)