diff --git a/components/net/KConfig b/components/net/KConfig index d6c2c2101a78cd64a845ceca95b70a43492f47be..5b9c46a4beb0d6a280ba17a411128c64c427c8ec 100644 --- a/components/net/KConfig +++ b/components/net/KConfig @@ -122,4 +122,6 @@ config RT_USING_LWIP endmenu +source "$RTT_DIR/components/net/freemodbus/KConfig" + endmenu diff --git a/components/net/freemodbus/KConfig b/components/net/freemodbus/KConfig new file mode 100644 index 0000000000000000000000000000000000000000..9321c61a0eae5322a82253f4a36c70ea7b62e49a --- /dev/null +++ b/components/net/freemodbus/KConfig @@ -0,0 +1,17 @@ +menu "Modbus master and slave stack" + +config RT_USING_MODBUS + bool "Enable Modbus stack" + default n + + if RT_USING_MODBUS + config RT_MODBUS_MASTER_RTU + bool "RTU master mode" + default n + + config RT_MODBUS_SLAVE_RTU + bool "RTU slave mode" + default n + endif + +endmenu diff --git a/components/net/freemodbus/modbus/functions/mbfunccoils_m.c b/components/net/freemodbus/modbus/functions/mbfunccoils_m.c index f6bb51cdf626cf2cc986f72c9640c37b70927259..93801de06f40372af901cf58d6bb2f23979e4c79 100644 --- a/components/net/freemodbus/modbus/functions/mbfunccoils_m.c +++ b/components/net/freemodbus/modbus/functions/mbfunccoils_m.c @@ -196,7 +196,7 @@ eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData, LO UCHAR *ucMBFrame; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; - if ( usCoilAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG; + if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG; else if ( ( usCoilData != 0xFF00 ) && ( usCoilData != 0x0000 ) ) eErrStatus = MB_MRE_ILL_ARG; else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else diff --git a/components/net/freemodbus/modbus/functions/mbfuncholding.c b/components/net/freemodbus/modbus/functions/mbfuncholding.c index e74a0624dc65a622f0ca0a549105079350c03348..7921302f965b7a399680e63fafc32ed300f443a5 100644 --- a/components/net/freemodbus/modbus/functions/mbfuncholding.c +++ b/components/net/freemodbus/modbus/functions/mbfuncholding.c @@ -183,7 +183,7 @@ eMBFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen ) usRegAddress++; usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 ); - usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] ); + usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] ); /* Check if the number of registers to read is valid. If not * return Modbus illegal data value exception. diff --git a/components/net/freemodbus/port/portserial.c b/components/net/freemodbus/port/portserial.c index 317c7f4605264f33ff0eb183b944df6709264fba..11173579ad9dd8cb7b6b03e771f3897eaf48a6c9 100644 --- a/components/net/freemodbus/port/portserial.c +++ b/components/net/freemodbus/port/portserial.c @@ -99,14 +99,14 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, serial->ops->configure(serial, &(serial->config)); /* open serial device */ - if (!serial->parent.open(&serial->parent, - RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX )) { - serial->parent.rx_indicate = serial_rx_ind; + if (!rt_device_open(&serial->parent, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX)) { + rt_device_set_rx_indicate(&serial->parent, serial_rx_ind); } else { return FALSE; } /* software initialize */ + rt_event_init(&event_serial, "slave event", RT_IPC_FLAG_PRIO); rt_thread_init(&thread_serial_soft_trans_irq, "slave trans", serial_soft_trans_irq, @@ -115,7 +115,6 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, sizeof(serial_soft_trans_irq_stack), 10, 5); rt_thread_startup(&thread_serial_soft_trans_irq); - rt_event_init(&event_serial, "slave event", RT_IPC_FLAG_PRIO); return TRUE; } diff --git a/components/net/freemodbus/port/portserial_m.c b/components/net/freemodbus/port/portserial_m.c index 8ec0d081f1baa424037a400cc5cb37466e0e9469..366dc799835e0759bd02e2477bf39c3453dfbc5e 100644 --- a/components/net/freemodbus/port/portserial_m.c +++ b/components/net/freemodbus/port/portserial_m.c @@ -36,7 +36,7 @@ static rt_uint8_t serial_soft_trans_irq_stack[512]; static struct rt_thread thread_serial_soft_trans_irq; /* serial event */ static struct rt_event event_serial; -/* modbus slave serial device */ +/* modbus master serial device */ static rt_serial_t *serial; /* ----------------------- Defines ------------------------------------------*/ @@ -100,23 +100,22 @@ BOOL xMBMasterPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, serial->ops->configure(serial, &(serial->config)); /* open serial device */ - if (!serial->parent.open(&serial->parent, - RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX )) { - serial->parent.rx_indicate = serial_rx_ind; + if (!rt_device_open(&serial->parent, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX)) { + rt_device_set_rx_indicate(&serial->parent, serial_rx_ind); } else { return FALSE; } /* software initialize */ + rt_event_init(&event_serial, "master event", RT_IPC_FLAG_PRIO); rt_thread_init(&thread_serial_soft_trans_irq, - "slave trans", + "master trans", serial_soft_trans_irq, RT_NULL, serial_soft_trans_irq_stack, sizeof(serial_soft_trans_irq_stack), 10, 5); rt_thread_startup(&thread_serial_soft_trans_irq); - rt_event_init(&event_serial, "slave event", RT_IPC_FLAG_PRIO); return TRUE; } diff --git a/components/net/freemodbus/port/porttimer.c b/components/net/freemodbus/port/porttimer.c index 6f017d87594ca0a63ae51e8525c1556956f75022..8c51ad06d5f619792860146aa76cd8c7cf43f254 100644 --- a/components/net/freemodbus/port/porttimer.c +++ b/components/net/freemodbus/port/porttimer.c @@ -37,7 +37,7 @@ BOOL xMBPortTimersInit(USHORT usTim1Timerout50us) rt_timer_init(&timer, "slave timer", timer_timeout_ind, /* bind timeout callback function */ RT_NULL, - (50*usTim1Timerout50us)/(1000*1000/RT_TICK_PER_SECOND), + (50 * usTim1Timerout50us) / (1000 * 1000 / RT_TICK_PER_SECOND) + 1, RT_TIMER_FLAG_ONE_SHOT); /* one shot */ return TRUE; } diff --git a/components/net/freemodbus/port/porttimer_m.c b/components/net/freemodbus/port/porttimer_m.c index 00441bfd1cb16e22dd494bd33f0b721777abf32e..ebf80004d857b4fdd52230be48bb64729d67fb54 100644 --- a/components/net/freemodbus/port/porttimer_m.c +++ b/components/net/freemodbus/port/porttimer_m.c @@ -46,7 +46,7 @@ BOOL xMBMasterPortTimersInit(USHORT usTimeOut50us) rt_timer_init(&timer, "master timer", timer_timeout_ind, /* bind timeout callback function */ RT_NULL, - (50 * usT35TimeOut50us) / (1000 * 1000 / RT_TICK_PER_SECOND), + (50 * usT35TimeOut50us) / (1000 * 1000 / RT_TICK_PER_SECOND) + 1, RT_TIMER_FLAG_ONE_SHOT); /* one shot */ return TRUE;