7.Modbus RTU通信示例
1.采用Modbus-RTU从方式
2.地址:地址设定范围1 - 247(出厂预设值为01H)
3.波特率可设置为1200bps,2400bps,4800bps,9600bps(出厂预设值),19200bps,38400bps
4.字节格式:
数据位:8位
起始位:1位
停止位:1位
5.同位检查位:
可设定为偶校验(默认)、奇数同位检查、不校验
•Slave address/Slave ID设备端ID当要读写slave时,必须先知道该设备的address/ID
•常用的modbus功能码(function code)–0×03 0×03功能码(function code)所定义的功能为读取多个寄存器,用来读取一连续位址的数据。
•master读取格式:设备ID(slave address/ID)+ 0×03 +读取起始位置(2 byte)+读取的数量(2byte)+ CRC16
•slave回复格式:设备ID(slave address/ID)+ 0×03 +回复数据的byte数+数据1(4byte)+…+数据n(byte)+ CRC16
8.Master package/Slave package
9.Freemodbus 移植
•freemodbus.berlios.de
–使用從機功能
–提供RTU, ASCII mode
–需要資源
串口
計時器
10.portserial.c
•void vMBPortSerialEnable(BOOL xRxEnable,BOOL xTxEnable)
接收、传送中断的开启或关闭功能
•void vMBPortClose(void)
关闭串口
•BOOL xMBPortSerialInit(UCHAR ucPORT,ULONG ulBaudRate,UCHAR ucDataBits,eMBParity eParity)
串口初始化、波特率、数据位长度、奇偶校验位
•BOOL xMBPortSerialPutByte(CHAR ucByte)
传送字元
•BOOL xMBPortSerialGetByte(CHAR * pucByte)
接收字元
•void UART0_IRQHandler(void)
串口中断处理
11.porttimer.c
•BOOL xMBPortTimersInit(USHORT usTim1Timerout50us)
设定定时器,时间间格单位为50us
1.采用Modbus-RTU从方式
2.地址:地址设定范围1 - 247(出厂预设值为01H)
3.波特率可设置为1200bps,2400bps,4800bps,9600bps(出厂预设值),19200bps,38400bps
4.字节格式:
数据位:8位
起始位:1位
停止位:1位
5.同位检查位:
可设定为偶校验(默认)、奇数同位检查、不校验
•Slave address/Slave ID设备端ID当要读写slave时,必须先知道该设备的address/ID
•常用的modbus功能码(function code)–0×03 0×03功能码(function code)所定义的功能为读取多个寄存器,用来读取一连续位址的数据。
•master读取格式:设备ID(slave address/ID)+ 0×03 +读取起始位置(2 byte)+读取的数量(2byte)+ CRC16
•slave回复格式:设备ID(slave address/ID)+ 0×03 +回复数据的byte数+数据1(4byte)+…+数据n(byte)+ CRC16
8.Master package/Slave package
9.Freemodbus 移植
•freemodbus.berlios.de
–使用從機功能
–提供RTU, ASCII mode
–需要資源
串口
計時器
10.portserial.c
•void vMBPortSerialEnable(BOOL xRxEnable,BOOL xTxEnable)
接收、传送中断的开启或关闭功能
•void vMBPortClose(void)
关闭串口
•BOOL xMBPortSerialInit(UCHAR ucPORT,ULONG ulBaudRate,UCHAR ucDataBits,eMBParity eParity)
串口初始化、波特率、数据位长度、奇偶校验位
•BOOL xMBPortSerialPutByte(CHAR ucByte)
传送字元
•BOOL xMBPortSerialGetByte(CHAR * pucByte)
接收字元
•void UART0_IRQHandler(void)
串口中断处理
11.porttimer.c
•BOOL xMBPortTimersInit(USHORT usTim1Timerout50us)
设定定时器,时间间格单位为50us
•void vMBPortTimersEnable(void)
致能timer
•void vMBPortTimersDisable(void)
关闭timer
•void TMR0_IRQHandler(void)
定时器中断
12. main
/* ----------------------- Defines ------------------------------------------*/
#define REG_INPUT_START 1000
#define REG_INPUT_NREGS 4
#define PLLCON_SETTING CLK_PLLCON_50MHz_HXT
#define PLL_CLOCK 50000000
/* ----------------------- Static variables ---------------------------------*/
static USHORT usRegInputStart = REG_INPUT_START;
static USHORT usRegInputBuf[REG_INPUT_NREGS];
int32_t main()
{
eMBErrorCode eStatus;
/* Unlock protected registers */
SYS_UnlockReg();
/* Enable IRC22M clock */
CLK->PWRCON |= CLK_PWRCON_IRC22M_EN_Msk;
/* Waiting for IRC22M clock ready */
CLK_WaitClockReady(CLK_CLKSTATUS_IRC22M_STB_Msk);
/* Enable the Modbus Protocol Stack. */
eStatus = eMBEnable( );
for( ;; )
{
( void )eMBPoll( );
/* Here we simply count the number of poll cycles. */
usRegInputBuf[0]++;
}
}
Program Size: Code=6112 RO-data=940 RW-data=260 ZI-data=2436
•Project Path:
NUC230_240BSPv3 freemodbus\SampleCode\Modbus_UART\KEIL
13.Nuedu Connection
14.测试
•Modpoll
http://www.modbusdriver.com/modpoll.html
modpoll.exe -m rtu -a 10 -r 1000 -c 4 -t 3 -b 38400 -d 8 -p none COM4
评论