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; }
/* Private function ----------------------------------------------------------*/ int main(void) { GPIO_InitTypeDef GPIO_InitStructure; uint8_t cnt=0; /* Set the Vector Table base adress at 0x8004000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x4000); /* LED0 -> PB0 LED1 -> PB1 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); // Initialize protocol stack in RTU mode for a slave with address 10 = 0x0A // MB_RTU, Device ID: 1, USART portL: 1 (este configurat in portserial.h, Baud rate: 38400, Parity: NONE) eMBInit(MB_RTU, 1, 1, 38400, MB_PAR_NONE); // Enable the Modbus Protocol Stack. eMBEnable(); while(1) { // Reset the flag. It will only be set if the modbus pooling has a request Modbus_Request_Flag = 0; // Call the main polling loop of the Modbus protocol stack. eMBPoll(); if (Modbus_Request_Flag) GPIO_SetBits(GPIOB , GPIO_Pin_0); Delay(0xffff); if (Modbus_Request_Flag) GPIO_ResetBits(GPIOB , GPIO_Pin_0); cnt++; if (cnt == 4) \ { writeInputRegister(1, 111); writeInputRegister(2, 222); writeInputRegister(98, 111); writeInputRegister(99, 222); writeHoldingRegister(1, 333); writeHoldingRegister(2, 444); writeHoldingRegister(98, 333); writeHoldingRegister(99, 444); writeCoil(1, 0); writeCoil(2, 1); writeCoil(58, 1); writeCoil(59, 0); } if (cnt == 8) { writeInputRegister(1, 222); writeInputRegister(2, 111); writeInputRegister(98, 222); writeInputRegister(99, 111); writeHoldingRegister(1, 444); writeHoldingRegister(2, 333); writeHoldingRegister(98, 444); writeHoldingRegister(99, 333); writeCoil(1, 1); writeCoil(2, 0); writeCoil(58, 0); writeCoil(59, 1); cnt = 0; } } }