diff --git a/components/net/freemodbus/port/portserial.c b/components/net/freemodbus/port/portserial.c index 7c546a1691..cbd846d7ff 100644 --- a/components/net/freemodbus/port/portserial.c +++ b/components/net/freemodbus/port/portserial.c @@ -52,6 +52,8 @@ static void serial_soft_trans_irq(void* parameter); BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity) { + rt_device_t dev = RT_NULL; + char uart_name[20]; /** * set 485 mode receive and transmit control IO * @note MODBUS_SLAVE_RT_CONTROL_PIN_INDEX need be defined by user @@ -60,22 +62,19 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, rt_pin_mode(MODBUS_SLAVE_RT_CONTROL_PIN_INDEX, PIN_MODE_OUTPUT); #endif /* set serial name */ - if (ucPORT == 1) { -#if defined(RT_USING_UART1) || defined(RT_USING_REMAP_UART1) - extern struct rt_serial_device serial1; - serial = &serial1; -#endif - } else if (ucPORT == 2) { -#if defined(RT_USING_UART2) - extern struct rt_serial_device serial2; - serial = &serial2; -#endif - } else if (ucPORT == 3) { -#if defined(RT_USING_UART3) - extern struct rt_serial_device serial3; - serial = &serial3; -#endif + rt_snprintf(uart_name,sizeof(uart_name), "uart%d", ucPORT); + + dev = rt_device_find(uart_name); + if(dev == RT_NULL) + { + /* can not find uart */ + return FALSE; } + else + { + serial = (struct rt_serial_device*)dev; + } + /* set serial configure parameter */ serial->config.baud_rate = ulBaudRate; serial->config.stop_bits = STOP_BITS_1; diff --git a/components/net/freemodbus/port/portserial_m.c b/components/net/freemodbus/port/portserial_m.c index f68794a617..47a70dd905 100644 --- a/components/net/freemodbus/port/portserial_m.c +++ b/components/net/freemodbus/port/portserial_m.c @@ -53,6 +53,9 @@ static void serial_soft_trans_irq(void* parameter); BOOL xMBMasterPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity) { + rt_device_t dev = RT_NULL; + char uart_name[20]; + /** * set 485 mode receive and transmit control IO * @note MODBUS_MASTER_RT_CONTROL_PIN_INDEX need be defined by user @@ -60,24 +63,20 @@ BOOL xMBMasterPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, #if defined(RT_MODBUS_MASTER_USE_CONTROL_PIN) rt_pin_mode(MODBUS_MASTER_RT_CONTROL_PIN_INDEX, PIN_MODE_OUTPUT); #endif - /* set serial name */ - if (ucPORT == 1) { -#if defined(RT_USING_UART1) || defined(RT_USING_REMAP_UART1) - extern struct rt_serial_device serial1; - serial = &serial1; -#endif - } else if (ucPORT == 2) { -#if defined(RT_USING_UART2) - extern struct rt_serial_device serial2; - serial = &serial2; -#endif - } else if (ucPORT == 3) { -#if defined(RT_USING_UART3) - extern struct rt_serial_device serial3; - serial = &serial3; -#endif + rt_snprintf(uart_name,sizeof(uart_name), "uart%d", ucPORT); + + dev = rt_device_find(uart_name); + if(dev == RT_NULL) + { + /* can not find uart */ + return FALSE; } + else + { + serial = (struct rt_serial_device*)dev; + } + /* set serial configure parameter */ serial->config.baud_rate = ulBaudRate; serial->config.stop_bits = STOP_BITS_1;