#include "modbusSlave.h" #include "modbus_crc.h" #include "string.h" #include "stdio.h" uint16_t tcp_ledcnt = 0; uint16_t u232_ledcnt = 0; uint16_t u485_ledcnt = 0; static uint8_t SLAVE_ID = 7; static uint16_t Holding_Registers_Database[3000] = { 0 }; void set_slaveid(uint8_t val) { SLAVE_ID = val; } uint8_t get_slaveid() { return SLAVE_ID; } void get_register_range(uint16_t start, uint16_t size, uint16_t *buf) { for (uint16_t i = 0; i < size; i++) { buf[i] = Holding_Registers_Database[start + i]; } } void get_register_range_to_u8(uint16_t start, uint16_t count, uint8_t *buf) { for (int i = 0; i < count; ++i) { // 获取当前 uint16_t 值 uint16_t current = Holding_Registers_Database[start + i]; // 高字节放在低地址 if (get_register(1009) == 10) { buf[2 * i] = (uint8_t) (current >> 8); // 高字节 buf[2 * i + 1] = (uint8_t) (current); // 低字节 } else { buf[2 * i] = (uint8_t) (current); // 低字节 buf[2 * i + 1] = (uint8_t) (current >> 8); // 高字节 } } } void set_register_range(uint16_t start, uint16_t size, uint16_t *buf) { for (uint16_t i = 0; i < size; i++) { Holding_Registers_Database[start + i] = buf[i]; } } void set_register_range_u8(uint16_t start, uint16_t size, uint8_t *buf) { for (uint16_t i = 0; i < size; i++) { Holding_Registers_Database[start + i] = buf[i]; } } void set_register(uint16_t index, uint16_t value) { Holding_Registers_Database[index] = value; } void set_register_u32(uint16_t index, uint32_t value) { uint16_t high = (value >> 16) & 0xFFFF; uint16_t low = value & 0xFFFF; Holding_Registers_Database[index] = low; Holding_Registers_Database[index + 1] = high; } uint32_t get_register_u32(uint16_t index) { uint32_t low = Holding_Registers_Database[index]; uint32_t high = Holding_Registers_Database[index + 1]; return (high << 16) | low; } void set_register_float(uint16_t index, float value) { FloatToUint16 converter; converter.value = value; Holding_Registers_Database[index] = converter.u16[0]; Holding_Registers_Database[index + 1] = converter.u16[1]; } uint16_t get_register(uint16_t index) { return Holding_Registers_Database[index]; } float get_register_float(uint16_t index) { FloatToUint16 converter; converter.u16[0] = Holding_Registers_Database[index]; converter.u16[1] = Holding_Registers_Database[index + 1]; return converter.value; } // *************************** MODBUS TCP start************************ uint16_t readHoldingRegs_tcp(uint8_t *rx_buf, uint8_t *tx_buf) { uint16_t startAddr = ((rx_buf[8] << 8) | rx_buf[9]); uint16_t numRegs = ((rx_buf[10] << 8) | rx_buf[11]); uint16_t frame_size = numRegs * 2 + 3; tx_buf[0] = rx_buf[0]; tx_buf[1] = rx_buf[1]; tx_buf[2] = rx_buf[2]; tx_buf[3] = rx_buf[3]; tx_buf[4] = (frame_size >> 8) & 0xFF; tx_buf[5] = frame_size & 0xFF; tx_buf[6] = rx_buf[6]; tx_buf[7] = rx_buf[7]; tx_buf[8] = numRegs * 2; int index = 9; for (int i = 0; i < numRegs; i++) { tx_buf[index++] = (Holding_Registers_Database[startAddr] >> 8) & 0xFF; tx_buf[index++] = (Holding_Registers_Database[startAddr]) & 0xFF; startAddr++; } return numRegs * 2 + 9; } uint16_t writeMultiRegs_tcp(uint8_t *rx_buf, uint8_t *tx_buf) { uint16_t startAddr = ((rx_buf[8] << 8) | rx_buf[9]); uint8_t write_size = rx_buf[12] / 2; uint8_t write_value_idx = 13; while (write_size > 0) { uint8_t left = rx_buf[write_value_idx++]; uint8_t right = rx_buf[write_value_idx++]; uint16_t this_value = (left << 8) | right; Holding_Registers_Database[startAddr++] = this_value; write_size--; } memcpy(tx_buf, rx_buf, 12); tx_buf[4] = 0x00; tx_buf[5] = 6; return 12; } uint16_t writeSingleReg_tcp(uint8_t *rx_buf, uint8_t *tx_buf) { uint16_t startAddr = ((rx_buf[8] << 8) | rx_buf[9]); uint8_t left = rx_buf[10]; uint8_t right = rx_buf[11]; uint16_t this_value = (left << 8) | right; Holding_Registers_Database[startAddr] = this_value; memcpy(tx_buf, rx_buf, 12); tx_buf[4] = 0x00; tx_buf[5] = 0x06; return 12; } uint16_t handleModbusTcp(uint8_t *rx_buf, uint8_t *tx_buf) { switch (rx_buf[7]) { case 0x03: return readHoldingRegs_tcp(rx_buf, tx_buf); break; case 0x06: return writeSingleReg_tcp(rx_buf, tx_buf); break; case 0x10: return writeMultiRegs_tcp(rx_buf, tx_buf); break; default: break; } return 0; } // *************************** MODBUS TCP end************************ uint8_t readHoldingRegs_rtu(uint8_t *rx_buf, uint8_t *tx_buf) { uint16_t startAddr = ((rx_buf[2] << 8) | rx_buf[3]); // start Register Address uint16_t numRegs = ((rx_buf[4] << 8) | rx_buf[5]); // number to registers master has requested tx_buf[0] = SLAVE_ID; // slave ID tx_buf[1] = rx_buf[1]; // function code tx_buf[2] = numRegs * 2; // Byte count int endIdx = 3; // we need to keep track of how many bytes has been stored in MDTxData Buffer for (int i = 0; i < numRegs; i++) // Load the actual data into MDTxData buffer { tx_buf[endIdx++] = (Holding_Registers_Database[startAddr] >> 8) & 0xFF; // extract the higher byte tx_buf[endIdx++] = (Holding_Registers_Database[startAddr]) & 0xFF; // extract the lower byte startAddr++; // increment the register address } return endIdx; } uint8_t writeSingleReg_rtu(uint8_t *rx_buf, uint8_t *tx_buf) { uint16_t startAddr = ((rx_buf[2] << 8) | rx_buf[3]); // start Register Address uint16_t this_value = (rx_buf[4] << 8) | rx_buf[5]; Holding_Registers_Database[startAddr] = this_value; memcpy(tx_buf, rx_buf, 6); return 6; } uint8_t writeMultiRegs_rtu(uint8_t *rx_buf, uint8_t *tx_buf) { uint16_t startAddr = ((rx_buf[2] << 8) | rx_buf[3]); // start Register Address uint16_t numRegs = ((rx_buf[4] << 8) | rx_buf[5]); // number to registers master has requested int endIdx = 7; // we need to keep track of index in MDRxData for (int i = 0; i < numRegs; i++) { uint16_t highByte = rx_buf[endIdx++] << 8; uint16_t lowByte = rx_buf[endIdx++]; Holding_Registers_Database[startAddr++] = highByte | lowByte; } memcpy(tx_buf, rx_buf, 6); return 6; } uint8_t handleModbusRtu(uint8_t *rx_buf, uint8_t *tx_buf) { // if(huart == &huart3){ // u232_ledcnt =0; // }else if(huart == &huart4){ // u485_ledcnt =0; // } switch (rx_buf[1]) { case 0x03: return readHoldingRegs_rtu(rx_buf, tx_buf); break; case 0x06: return writeSingleReg_rtu(rx_buf, tx_buf); break; case 0x10: return writeMultiRegs_rtu(rx_buf, tx_buf); break; default: break; } return 0; } ///////////////////////////////////////////↓485从站功能↓/////////////////////////////////////// void handleModbusRtu_NEW(uint8_t *rx_buf, uint8_t *tx_buf, uint8_t rxSize, UART_HandleTypeDef *huart) { uint8_t txSize; uint16_t crc = crc16(rx_buf, rxSize - 2); if (crc != ((rx_buf[rxSize - 2] << 8) | rx_buf[rxSize - 1])) { txSize = 0; } if (rx_buf[0] == SLAVE_ID) { tcp_ledcnt = 0; switch (rx_buf[1]) { case 0x03: txSize = readHoldingRegs_rtu(rx_buf, tx_buf); break; case 0x06: txSize = writeSingleReg_rtu(rx_buf, tx_buf); break; case 0x10: txSize = writeMultiRegs_rtu(rx_buf, tx_buf); break; default: break; } } if (txSize > 0) { HAL_GPIO_WritePin(re485_GPIO_Port, re485_Pin, GPIO_PIN_SET); crc = crc16(tx_buf, txSize); tx_buf[txSize] = crc & 0xFF; tx_buf[txSize + 1] = (crc >> 8) & 0xFF; HAL_UART_Transmit_DMA(huart, tx_buf, txSize + 2); } else { HAL_GPIO_WritePin(re485_GPIO_Port, re485_Pin, GPIO_PIN_RESET); HAL_UARTEx_ReceiveToIdle_DMA(huart, rx_buf, 1024); } } ///////////////////////////////////////////↑485从站功能↑/////////////////////////////////////// //extern uint8_t MDRxData[256]; //extern uint8_t MDTxData[256]; //extern UART_HandleTypeDef huart1; //void modbusException (uint8_t exceptioncode) //{ // //| SLAVE_ID | FUNCTION_CODE | Exception code | CRC | // //| 1 BYTE | 1 BYTE | 1 BYTE | 2 BYTES | // // MDTxData[0] = MDRxData[0]; // slave ID // MDTxData[1] = MDRxData[1]|0x80; // adding 1 to the MSB of the function code // MDTxData[2] = exceptioncode; // Load the Exception code // sendData(MDTxData, 3); // send Data... CRC will be calculated in the function //} // //void check_exception(uint16_t numRegs, uint16_t endAddr){ // if ((numRegs<1)||(numRegs>125)) // { // modbusException (ILLEGAL_DATA_VALUE); // return 0; // } // // uint16_t endAddr = startAddr+numRegs-1; // if (endAddr>999) // { // modbusException(ILLEGAL_DATA_ADDRESS); // return 0; // } //} //void sendData (uint8_t *data, int size) //{ // // we will calculate the CRC in this function itself // uint16_t crc = crc16(data, size); // data[size] = crc&0xFF; // data[size+1] = (crc>>8)&0xFF; // HAL_UART_Transmit_DMA(&huart6, data, size+2); //} // //void test_freq(void) //{ // if(test_idx>9998){ // test_idx = 0; // } // Holding_Registers_Database[0] = test_idx; // test_idx++; //} // //void test_spi(uint16_t data){ // Holding_Registers_Database[0] = data; //} // //void set_holdR_data(uint16_t* data, uint8_t size) //{ // memcpy(Holding_Registers_Database, data, size); // //} // //void set_holdR_value(uint16_t idx, uint16_t value){ // // Holding_Registers_Database[idx] = value; //} // //uint16_t get_holdR_value(uint16_t idx){ // return Holding_Registers_Database[idx]; //} // // // //float get_hold_float(uint8_t idx){ // union FloatToBytes converter; // uint16_t left = Holding_Registers_Database[idx]; // uint16_t right = Holding_Registers_Database[idx+1]; // converter.byteValues[0] = right; // converter.byteValues[1] = right<<8; // converter.byteValues[2] = left; // converter.byteValues[3] = left<<8; // return converter.floatValue; //} // //void set_hold_float(uint8_t idx, float value){ // union FloatToBytes converter; // converter.floatValue = value; // uint16_t highValue = (converter.byteValues[1] << 8) | converter.byteValues[0]; // uint16_t lowValue = (converter.byteValues[3] << 8) | converter.byteValues[2]; // Holding_Registers_Database[idx] = highValue; // Holding_Registers_Database[idx+1] = lowValue; //} // //void setup_defalut_values(float* default_values){ // for(uint8_t i=0; i<7;i++){ // union FloatToBytes converter; // converter.floatValue = default_values[i]; // uint16_t highValue = (converter.byteValues[1] << 8) | converter.byteValues[0]; // uint16_t lowValue = (converter.byteValues[3] << 8) | converter.byteValues[2]; // Holding_Registers_Database[i*2] = highValue; // Holding_Registers_Database[i*2+1] = lowValue; // } // //} // //void setup_default_uint16(uint16_t* default_values,uint8_t size){ // for(uint8_t i=0; i125)) // maximum no. of Registers as per the PDF // { // modbusException (ILLEGAL_DATA_VALUE); // send an exception // return 0; // } // // uint16_t endAddr = startAddr+numRegs-1; // end Register // if (endAddr>1000) // { // modbusException(ILLEGAL_DATA_ADDRESS); // send an exception // return 0; // } // // // MDTxData[0] = SLAVE_ID; // slave ID // MDTxData[1] = MDRxData[1]; // function code // MDTxData[2] = numRegs*2; // Byte count // int indx = 3; // we need to keep track of how many bytes has been stored in MDTxData Buffer // // for (int i=0; i>8)&0xFF; // extract the higher byte // MDTxData[indx++] = (Holding_Registers_Database[startAddr])&0xFF; // extract the lower byte // startAddr++; // increment the register address // } // // sendData(MDTxData, indx); // send data... CRC will be calculated in the function itself // // return 1; // success //} // //uint8_t readHoldingRegs_with_targetHuart (UART_HandleTypeDef *targetHuart) //{ // uint16_t startAddr = ((MDRxData[2]<<8)|MDRxData[3]); // start Register Address // // uint16_t numRegs = ((MDRxData[4]<<8)|MDRxData[5]); // number to registers master has requested // if ((numRegs<1)||(numRegs>125)) // maximum no. of Registers as per the PDF // { // modbusException (ILLEGAL_DATA_VALUE); // send an exception // return 0; // } // // uint16_t endAddr = startAddr+numRegs-1; // end Register // if (endAddr>1000) // { // modbusException(ILLEGAL_DATA_ADDRESS); // send an exception // return 0; // } // // // MDTxData[0] = SLAVE_ID; // slave ID // MDTxData[1] = MDRxData[1]; // function code // MDTxData[2] = numRegs*2; // Byte count // int indx = 3; // we need to keep track of how many bytes has been stored in MDTxData Buffer // // for (int i=0; i>8)&0xFF; // extract the higher byte // MDTxData[indx++] = (Holding_Registers_Database[startAddr])&0xFF; // extract the lower byte // startAddr++; // increment the register address // } // // sendData_with_target_huart(MDTxData, indx, targetHuart); // send data... CRC will be calculated in the function itself // // return 1; // success //} // // //uint8_t readInputRegs (void) //{ // uint16_t startAddr = ((MDRxData[2]<<8)|MDRxData[3]); // start Register Address // // uint16_t numRegs = ((MDRxData[4]<<8)|MDRxData[5]); // number to registers master has requested // if ((numRegs<1)||(numRegs>125)) // maximum no. of Registers as per the PDF // { // modbusException (ILLEGAL_DATA_VALUE); // send an exception // return 0; // } // // uint16_t endAddr = startAddr+numRegs-1; // end Register // if (endAddr>49) // end Register can not be more than 49 as we only have record of 50 Registers in total // { // modbusException(ILLEGAL_DATA_ADDRESS); // send an exception // return 0; // } // // // Prepare MDTxData buffer // // //| SLAVE_ID | FUNCTION_CODE | BYTE COUNT | DATA | CRC | // //| 1 BYTE | 1 BYTE | 1 BYTE | N*2 BYTES | 2 BYTES | // // MDTxData[0] = SLAVE_ID; // slave ID // MDTxData[1] = MDRxData[1]; // function code // MDTxData[2] = numRegs*2; // Byte count // int indx = 3; // we need to keep track of how many bytes has been stored in MDTxData Buffer // // for (int i=0; i>8)&0xFF; // extract the higher byte // MDTxData[indx++] = (Input_Registers_Database[startAddr])&0xFF; // extract the lower byte // startAddr++; // increment the register address // } // // sendData(MDTxData, indx); // send data... CRC will be calculated in the function itself // return 1; // success //} // //uint8_t writeHoldingRegs (void) //{ // uint16_t startAddr = ((MDRxData[2]<<8)|MDRxData[3]); // start Register Address // // uint16_t numRegs = ((MDRxData[4]<<8)|MDRxData[5]); // number to registers master has requested // if ((numRegs<1)||(numRegs>123)) // maximum no. of Registers as per the PDF // { // modbusException (ILLEGAL_DATA_VALUE); // send an exception // return 0; // } // // uint16_t endAddr = startAddr+numRegs-1; // end Register // if (endAddr>49) // end Register can not be more than 49 as we only have record of 50 Registers in total // { // modbusException(ILLEGAL_DATA_ADDRESS); // send an exception // return 0; // } // // /* start saving 16 bit data // * Data starts from MDRxData[7] and we need to combine 2 bytes together // * 16 bit Data = firstByte<<8|secondByte // */ // int indx = 7; // we need to keep track of index in MDRxData // for (int i=0; i123)) // maximum no. of Registers as per the PDF // { // modbusException (ILLEGAL_DATA_VALUE); // send an exception // return 0; // } // // uint16_t endAddr = startAddr+numRegs-1; // end Register // if (endAddr>49) // end Register can not be more than 49 as we only have record of 50 Registers in total // { // modbusException(ILLEGAL_DATA_ADDRESS); // send an exception // return 0; // } // // /* start saving 16 bit data // * Data starts from MDRxData[7] and we need to combine 2 bytes together // * 16 bit Data = firstByte<<8|secondByte // */ // int indx = 7; // we need to keep track of index in MDRxData // for (int i=0; i999) // The Register Address can not be more than 49 as we only have record of 50 Registers in total // { // modbusException(ILLEGAL_DATA_ADDRESS); // send an exception // return 0; // } // // /* Save the 16 bit data // * Data is the combination of 2 bytes, MDRxData[4] and MDRxData[5] // */ // uint16_t this_value = (MDRxData[4]<<8)|MDRxData[5]; // Holding_Registers_Database[startAddr] = this_value; // // //// if(startAddr==0){ //// htim2.Instance->CCR1 = this_value; // 25-125 = 0-180 //// } // // // Prepare Response // // //| SLAVE_ID | FUNCTION_CODE | Start Addr | Data | CRC | // //| 1 BYTE | 1 BYTE | 2 BYTE | 2 BYTES | 2 BYTES | // // MDTxData[0] = SLAVE_ID; // slave ID // MDTxData[1] = MDRxData[1]; // function code // MDTxData[2] = MDRxData[2]; // Start Addr HIGH Byte // MDTxData[3] = MDRxData[3]; // Start Addr LOW Byte // MDTxData[4] = MDRxData[4]; // Reg Data HIGH Byte // MDTxData[5] = MDRxData[5]; // Reg Data LOW Byte // // sendData(MDTxData, 6); // send data... CRC will be calculated in the function itself // return 1; // success //} // // //uint8_t writeSingleReg_with_targetHuart (UART_HandleTypeDef *targetHuart) //{ // uint16_t startAddr = ((MDRxData[2]<<8)|MDRxData[3]); // start Register Address // // if (startAddr>999) // The Register Address can not be more than 49 as we only have record of 50 Registers in total // { // modbusException(ILLEGAL_DATA_ADDRESS); // send an exception // return 0; // } // // /* Save the 16 bit data // * Data is the combination of 2 bytes, MDRxData[4] and MDRxData[5] // */ // uint16_t this_value = (MDRxData[4]<<8)|MDRxData[5]; // Holding_Registers_Database[startAddr] = this_value; // // //// if(startAddr==0){ //// htim2.Instance->CCR1 = this_value; // 25-125 = 0-180 //// } // // // Prepare Response // // //| SLAVE_ID | FUNCTION_CODE | Start Addr | Data | CRC | // //| 1 BYTE | 1 BYTE | 2 BYTE | 2 BYTES | 2 BYTES | // // MDTxData[0] = SLAVE_ID; // slave ID // MDTxData[1] = MDRxData[1]; // function code // MDTxData[2] = MDRxData[2]; // Start Addr HIGH Byte // MDTxData[3] = MDRxData[3]; // Start Addr LOW Byte // MDTxData[4] = MDRxData[4]; // Reg Data HIGH Byte // MDTxData[5] = MDRxData[5]; // Reg Data LOW Byte // // sendData_with_target_huart(MDTxData, 6, targetHuart); // send data... CRC will be calculated in the function itself // return 1; // success //} // // //uint8_t readCoils (void) //{ // uint16_t startAddr = ((MDRxData[2]<<8)|MDRxData[3]); // start Coil Address // // uint16_t numCoils = ((MDRxData[4]<<8)|MDRxData[5]); // number to coils master has requested // if ((numCoils<1)||(numCoils>2000)) // maximum no. of coils as per the PDF // { // modbusException (ILLEGAL_DATA_VALUE); // send an exception // return 0; // } // // uint16_t endAddr = startAddr+numCoils-1; // Last coils address // if (endAddr>199) // end coil can not be more than 199 as we only have record of 200 (0-199) coils in total // { // modbusException(ILLEGAL_DATA_ADDRESS); // send an exception // return 0; // } // // // //reset MDTxData buffer // memset (MDTxData, '\0', 256); // // // Prepare MDTxData buffer // // //| SLAVE_ID | FUNCTION_CODE | BYTE COUNT | DATA | CRC | // //| 1 BYTE | 1 BYTE | 1 BYTE | N*2 BYTES | 2 BYTES | // // MDTxData[0] = SLAVE_ID; // slave ID // MDTxData[1] = MDRxData[1]; // function code // MDTxData[2] = (numCoils/8) + ((numCoils%8)>0 ? 1:0); // Byte count // int indx = 3; // we need to keep track of how many bytes has been stored in MDTxData Buffer // // /* The approach is simple. We will read 1 bit at a time and store them in the Txdata buffer. // * First find the offset in the first byte we read from, for eg- if the start coil is 13, // * we will read from database[1] with an offset of 5. This bit will be stored in the MDTxData[0] at 0th position. // * Then we will keep shifting the database[1] to the right and read the bits. // * Once the bitposition has crossed the value 7, we will increment the startbyte // * When the indxposition exceeds 7, we increment the indx variable, so to copy into the next byte of the MDTxData // * This keeps going until the number of coils required have been copied // */ // int startByte = startAddr/8; // which byte we have to start extracting the data from // uint16_t bitPosition = startAddr%8; // The shift position in the first byte // int indxPosition = 0; // The shift position in the current indx of the MDTxData buffer // // // Load the actual data into MDTxData buffer // for (int i=0; i> bitPosition) &0x01) << indxPosition; // indxPosition++; bitPosition++; // if (indxPosition>7) // if the indxposition exceeds 7, we have to copy the data into the next byte position // { // indxPosition = 0; // indx++; // } // if (bitPosition>7) // if the bitposition exceeds 7, we have to increment the startbyte // { // bitPosition=0; // startByte++; // } // } // // if (numCoils%8 != 0)indx++; // increment the indx variable, only if the numcoils is not a multiple of 8 // sendData(MDTxData, indx); // send data... CRC will be calculated in the function itself // return 1; // success //} // // //uint8_t readInputs (void) //{ // uint16_t startAddr = ((MDRxData[2]<<8)|MDRxData[3]); // start Register Address // // uint16_t numCoils = ((MDRxData[4]<<8)|MDRxData[5]); // number to coils master has requested // if ((numCoils<1)||(numCoils>2000)) // maximum no. of coils as per the PDF // { // modbusException (ILLEGAL_DATA_VALUE); // send an exception // return 0; // } // // uint16_t endAddr = startAddr+numCoils-1; // Last coils address // if (endAddr>199) // end coil can not be more than 199 as we only have record of 200 (0-199) coils in total // { // modbusException(ILLEGAL_DATA_ADDRESS); // send an exception // return 0; // } // // // //reset MDTxData buffer // memset (MDTxData, '\0', 256); // // // Prepare MDTxData buffer // // //| SLAVE_ID | FUNCTION_CODE | BYTE COUNT | DATA | CRC | // //| 1 BYTE | 1 BYTE | 1 BYTE | N*2 BYTES | 2 BYTES | // // MDTxData[0] = SLAVE_ID; // slave ID // MDTxData[1] = MDRxData[1]; // function code // MDTxData[2] = (numCoils/8) + ((numCoils%8)>0 ? 1:0); // Byte count // int indx = 3; // we need to keep track of how many bytes has been stored in MDTxData Buffer // // /* The approach is simple. We will read 1 bit at a time and store them in the Txdata buffer. // * First find the offset in the first byte we read from, for eg- if the start coil is 13, // * we will read from database[1] with an offset of 5. This bit will be stored in the MDTxData[0] at 0th position. // * Then we will keep shifting the database[1] to the right and read the bits. // * Once the bitposition has crossed the value 7, we will increment the startbyte // * When the indxposition exceeds 7, we increment the indx variable, so to copy into the next byte of the MDTxData // * This keeps going until the number of coils required have been copied // */ // int startByte = startAddr/8; // which byte we have to start extracting the data from // uint16_t bitPosition = startAddr%8; // The shift position in the first byte // int indxPosition = 0; // The shift position in the current indx of the MDTxData buffer // // // Load the actual data into MDTxData buffer // for (int i=0; i> bitPosition) &0x01) << indxPosition; // indxPosition++; bitPosition++; // if (indxPosition>7) // if the indxposition exceeds 7, we have to copy the data into the next byte position // { // indxPosition = 0; // indx++; // } // if (bitPosition>7) // if the bitposition exceeds 7, we have to increment the startbyte // { // bitPosition=0; // startByte++; // } // } // // if (numCoils%8 != 0)indx++; // increment the indx variable, only if the numcoils is not a multiple of 8 // sendData(MDTxData, indx); // send data... CRC will be calculated in the function itself // return 1; // success //} // // //uint8_t writeSingleCoil (void) //{ // uint16_t startAddr = ((MDRxData[2]<<8)|MDRxData[3]); // start Coil Address // // if (startAddr>199) // The Coil Address can not be more than 199 as we only have record of 200 Coils in total // { // modbusException(ILLEGAL_DATA_ADDRESS); // send an exception // return 0; // } // // /* Calculation for the bit in the database, where the modification will be done */ // int startByte = startAddr/8; // which byte we have to start writing the data into // uint16_t bitPosition = startAddr%8; // The shift position in the first byte // // // /* The next 2 bytes in the MDRxData determines the state of the coil // * A value of FF 00 hex requests the coil to be ON. // * A value of 00 00 requests it to be OFF. // * All other values are illegal and will not affect the coil. // */ // // if ((MDRxData[4] == 0xFF) && (MDRxData[5] == 0x00)) // { // Coils_Database[startByte] |= 1<1968)) // maximum no. of coils as per the PDF // { // modbusException (ILLEGAL_DATA_VALUE); // send an exception // return 0; // } // // uint16_t endAddr = startAddr+numCoils-1; // Last coils address // if (endAddr>199) // end coil can not be more than 199 as we only have record of 200 (0-199) coils in total // { // modbusException(ILLEGAL_DATA_ADDRESS); // send an exception // return 0; // } // // /* Calculation for the bit in the database, where the modification will be done */ // int startByte = startAddr/8; // which byte we have to start writing the data into // uint16_t bitPosition = startAddr%8; // The shift position in the first byte // int indxPosition = 0; // The shift position in the current indx of the MDRxData buffer // // int indx = 7; // we need to keep track of index in MDRxData // // /* The approach is simple. We will read 1 bit (starting from the very first bit in the MDRxData Buffer) // * at a time and store them in the Database. // * First find the offset in the first byte we write into, for eg- if the start coil is 13, // * we will Write into database[1] with an offset of 5. This bit is read from the MDRxData[indx] at 0th indxposition. // * Then we will keep shifting the MDRxData[indx] to the right and read the bits. // * Once the bitposition has crossed the value 7, we will increment the startbyte and start modifying the next byte in the database // * When the indxposition exceeds 7, we increment the indx variable, so to copy from the next byte of the MDRxData // * This keeps going until the number of coils required have been modified // */ // // // Modify the bits as per the Byte received // for (int i=0; i>indxPosition)&0x01) == 1) // { // Coils_Database[startByte] |= 1<7) // if the indxposition exceeds 7, we have to copy the data into the next byte position // { // indxPosition = 0; // indx++; // } // if (bitPosition>7) // if the bitposition exceeds 7, we have to increment the startbyte // { // bitPosition=0; // startByte++; // } // } // // // Prepare Response // // //| SLAVE_ID | FUNCTION_CODE | Start Addr | Data | CRC | // //| 1 BYTE | 1 BYTE | 2 BYTE | 2 BYTES | 2 BYTES | // // MDTxData[0] = SLAVE_ID; // slave ID // MDTxData[1] = MDRxData[1]; // function code // MDTxData[2] = MDRxData[2]; // Start Addr HIGH Byte // MDTxData[3] = MDRxData[3]; // Start Addr LOW Byte // MDTxData[4] = MDRxData[4]; // num of coils HIGH Byte // MDTxData[5] = MDRxData[5]; // num of coils LOW Byte // // sendData(MDTxData, 6); // send data... CRC will be calculated in the function itself // return 1; // success //} // // // // //uint8_t modbus0x06_tcp (uint8_t* rx_buf) //{ // uint16_t startAddr = ((rx_buf[8]<<8)|rx_buf[9]); // uint8_t left = rx_buf[10]; // uint8_t right = rx_buf[11]; // uint16_t this_value = (left<<8)|right; // Holding_Registers_Database[startAddr] = this_value; // return 1; //}