|
楼主 |
发表于 2012-10-11 20:05:41
|
显示全部楼层
abnerle 发表于 2012-10-11 17:48
电脑上运行Modbus Poll
STM32板子运行程序,接收到的数据,就是图上的,
第一个问题,请详细检查modbus poll的校验位设置
第二个问题,定时器4设定为间隔时间50us,是freemodbus的要求。
有了这个50us的基准,那么可以更具不同的波特率设定帧与帧之间的间隔。请看一下代码!- eMBErrorCode
- eMBRTUInit( UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
- {
- eMBErrorCode eStatus = MB_ENOERR;
- ULONG usTimerT35_50us;
- ( void )ucSlaveAddress;
- ENTER_CRITICAL_SECTION();
- /* Modbus RTU uses 8 Databits. */
- if( xMBPortSerialInit( ucPort, ulBaudRate, 8, eParity ) != TRUE )
- {
- eStatus = MB_EPORTERR;
- }
- else
- {
- /* If baudrate > 19200 then we should use the fixed timer values
- * t35 = 1750us. Otherwise t35 must be 3.5 times the character time.
- */
- /* 如果波特率超过19200 使用固定的时间间隔,1750us
- *其他情况,则要进行计算。
- */
- if( ulBaudRate > 19200 )
- {
- usTimerT35_50us = 35; /* 1750us. */
- }
- else
- {
- /* The timer reload value for a character is given by:
- *
- * ChTimeValue = Ticks_per_1s / ( Baudrate / 11 )
- * = 11 * Ticks_per_1s / Baudrate
- * = 220000 / Baudrate
- * The reload for t3.5 is 1.5 times this value and similary
- * for t3.5.
- */
- usTimerT35_50us = ( 7UL * 220000UL ) / ( 2UL * ulBaudRate );
- }
-
- /* 初始化定时器 */
- if( xMBPortTimersInit( ( USHORT ) usTimerT35_50us ) != TRUE )
- {
- eStatus = MB_EPORTERR;
- }
- }
- EXIT_CRITICAL_SECTION( );
- return eStatus;
- }
复制代码 |
|