CANoe 与其他设备进行交互时,会用到各种各样的协议;Mobus就是在其中最常见的一种;
但是CAPL没有现成的库可以调用,因此,在研究modbus的基础上,编写了基于capl的Modbus协议,实现了 写单寄存器、写多寄存器、读多寄存器的功能 可以方便地对被控设备进行调试;
capl的Modbus的实现协议以及测试代码如下:
/*@!Encoding:936*/
variables
{
int ModBus232_PortNum = 2; //端口号
word ModBus232_BaudRate = 19200; //端口号
byte Rx_Buf[512];
byte Tx_Buf[512];
byte tmp_Rx_Buf;
word RxCount = 0;
word TxCount = 0;
word crc = 0;
word crc_check=0;
word i;
byte uchCRCHi = 0xFF;
byte uchCRCLo = 0xFF;
byte index0=0;
word uIndex;
int rx_flag=0;
// CRC 高位字节值 表
byte auchCRCHi[256] =
{
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
} ;
// CRC 低位字节值 表
byte auchCRCLo[256]
= {
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,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E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
0x43, 0x83, 0x41, 0x81, 0x80, 0x40
};
/* 私有类型定义 --------------------------------------------------------------*/
struct REG_DATA
{
word DATA_01H;
word DATA_02H;
word DATA_03H;
word DATA_04H;
word DATA_05H;
word DATA_06H;
word DATA_10H[64];
}reg_value;
byte MB_Recevive_Buffer[2000];
int MB_Recevive_Size=2000;
byte MB_SLAVEADDR_0=3;
byte MB_SLAVEADDR_1=4;
word HoldingReg=0;
word Receive_Data_Result[10][100];
int Read_Slave_ID=0;// 区分 第一/2块板卡
//void MB_ReadHoldingReg_03H(byte _addr, word _reg, word _num)
int Read_Start_Reg_Num=0;
int Read_Reg_Num=0;
int Is_RS232_InUse=0;
}
/**
*函数功能: Modbus CRC16 校验计算函数
*输入参数
*返回值
*说明
*/
word MB_CRC16(byte pushMsg[],byte usDataLen)
{
uchCRCHi = 0xFF;
uchCRCLo = 0xFF;
//uIndex=0;
index0=0;
while(usDataLen--)
{
uIndex = uchCRCLo ^ pushMsg[index0];
uchCRCLo = uchCRCHi^auchCRCHi[uIndex];
uchCRCHi = auchCRCLo[uIndex];
index0++;
}
return (uchCRCHi<<8|uchCRCLo);
}
/**
* 函数功能: 写N个保持寄存器(HoldingRegister)
* 输入参数: _addr:从站地址,_reg:寄存器地址,_num:待写入的寄存器数量,_databuf:待写入的寄存器数据
* 返 回 值: 无
* 说 明: 填充数据发送缓存区,然后发送._databuf的长度需 >= _num*2
*/
void MB_WriteNumHoldingReg_10H(byte _addr, word _reg, word _num,word _databuf[])
{
TxCount = 0;
crc = 0;
Tx_Buf[TxCount++] = _addr; /* 从站地址 */
Tx_Buf[TxCount++] = 0x10; /* 功能码 */
Tx_Buf[TxCount++] = _reg >> 8; /* 寄存器地址 高字节 */
Tx_Buf[TxCount++] = _reg; /* 寄存器地址 低字节 */
Tx_Buf[TxCount++] = _num >> 8; /* 寄存器(16bits)个数 高字节 */
Tx_Buf[TxCount++] = _num; /* 低字节 */
Tx_Buf[TxCount++] = _num*2; /* 数据个数 */
for (i = 0; i < _num; i++)
{
Tx_Buf[TxCount++] = (_databuf[i]>>8)&0xff;
Tx_Buf[TxCount++] = (byte)_databuf[i]&0xff;
}
crc = MB_CRC16(Tx_Buf,TxCount);
Tx_Buf[TxCount++] = crc; /* crc 低字节 */
Tx_Buf[TxCount++] = crc>>8; /* crc 高字节 */
ModeBusSend(ModBus232_PortNum,Tx_Buf,TxCount);
}
void ModeBusSend( dword ModBus232_PortNum, byte Tx_Buf[], dword TxCount )
{
while(Is_RS232_InUse==1)
{
}
if(Is_RS232_InUse==0)
{
Is_RS232_InUse=1;
if(rs232Send(ModBus232_PortNum,Tx_Buf,TxCount)==1)
{
write("232Send Successlly");
}
else
{
write("232Send Failed");
}
Is_RS232_InUse=0;
}
}
// 2 -3
//功能码06 写单个寄存器
// _addr 地址 , _reg 寄存器 , _data 数据值
void MB_WriteHoldingReg_06H(byte _addr, word _reg, word _data)
{
TxCount=0;
// crc = 0;
Tx_Buf[TxCount++] = _addr;
Tx_Buf[TxCount++] = 0x06;
Tx_Buf[TxCount++] = _reg >> 8;
Tx_Buf[TxCount++] = _reg;
Tx_Buf[TxCount++] = _data >> 8;
Tx_Buf[TxCount++] = _data;
crc = MB_CRC16(Tx_Buf,TxCount);
Tx_Buf[TxCount++] = crc;
Tx_Buf[TxCount++] = crc>>8;
ModeBusSend(ModBus232_PortNum,Tx_Buf,TxCount);
// if(rs232Send(ModBus232_PortNum,Tx_Buf,TxCount)==1)
// {
// write("232Send Successlly");
// }
// else
// {
// write("232Send Failed");
// }
}
/**
* 函数功能: 读保持寄存器(HoldingRegister)
* 输入参数: _addr:从站地址,_reg:寄存器地址,_num:待读取的寄存器数量
* 返 回 值: 无
* 说 明: 填充数据发送缓存区,然后发送
*/
void MB_ReadHoldingReg_03H(byte _addr, word _reg, word _num)
{
rx_flag=2;
Read_Slave_ID=_addr;
Read_Start_Reg_Num=_reg;
Read_Reg_Num=_num;
TxCount = 0;
crc = 0;
Tx_Buf[TxCount++] = _addr; /* 从站地址 */
Tx_Buf[TxCount++] = 0x03; /* 功能码 */
Tx_Buf[TxCount++] = _reg >> 8; /* 寄存器地址 高字节 */
Tx_Buf[TxCount++] = _reg; /* 寄存器地址 低字节 */
Tx_Buf[TxCount++] = _num >> 8; /* 寄存器(16bits)个数 高字节 */
Tx_Buf[TxCount++] = _num; /* 低字节 */
crc = MB_CRC16(Tx_Buf,TxCount);
Tx_Buf[TxCount++] = crc; /* crc 低字节 */
Tx_Buf[TxCount++] = crc>>8; /* crc 高字节 */
ModeBusSend(ModBus232_PortNum,Tx_Buf,TxCount);
// if(rs232Send(ModBus232_PortNum,Tx_Buf,TxCount)==1)
// {
// write("232Send Successlly");
// rx_flag=2;
// }
// else
// {
// write("232Send Failed");
// }
}
on start
{
reg_value.DATA_06H=0x12; //写入单个保持寄存器数据
//以下为4个保持寄存器数据
reg_value.DATA_10H[0]=0x12;
reg_value.DATA_10H[1]=0x65;
reg_value.DATA_10H[2]=0x13;
reg_value.DATA_10H[3]=0x76;
if(rs232Open(ModBus232_PortNum)==1 && rs232Configure(ModBus232_PortNum,ModBus232_BaudRate,8,1,0)==1)
{
write("Modbus 端口打开成功");
if ( 1==RS232Receive(ModBus232_PortNum,MB_Recevive_Buffer,MB_Recevive_Size) )
{
write("Modbus 端口接受打开");
}
}
else
{
write("Modbus 端口打开失败");
}
}
on preStop
{
if (1==RS232Close(ModBus232_PortNum))
{
write("Modbus 端口关闭成功");
}
else
{
write("Modbus 端口关闭失败");
}
}
RS232OnReceive( dword port, byte buffer[], dword number )
{
//rx_flag 不同的接收,待扩展
switch(rx_flag)
{
case 1:
//write("myVar is 1");
break;
case 2://MB_ReadHoldingReg_03H
{
crc_check = ( (buffer[number-1]<<8) | buffer[number-2] );
/* CRC 校验正确 */
if(crc_check == MB_CRC16(buffer,number-2))
{
// write("myVar is %d",number);
for (i = 0; i < Read_Reg_Num; i++)
{
Receive_Data_Result[Read_Slave_ID][Read_Start_Reg_Num+i]=(buffer[3+2*i]<<8)+buffer[4+2*i];
}
for (i = 0; i < Read_Reg_Num; i++)
{
Write("读取的数据值 是 %d",Receive_Data_Result[Read_Slave_ID][Read_Start_Reg_Num+i]);
}
}
}
break;
case 3:
//write("myVar is 3");
break;
default: break;
//write("myVar is not 1, 2 or 3");
}
}
on key 'a'
{
reg_value.DATA_10H[0]=1;
reg_value.DATA_10H[1]=2;
reg_value.DATA_10H[2]=3;
reg_value.DATA_10H[3]=4;
MB_WriteNumHoldingReg_10H(MB_SLAVEADDR_0, HoldingReg,4,reg_value.DATA_10H);
}
on key 'q'
{
reg_value.DATA_10H[0]=11;
reg_value.DATA_10H[1]=12;
reg_value.DATA_10H[2]=13;
reg_value.DATA_10H[3]=14;
MB_WriteNumHoldingReg_10H(MB_SLAVEADDR_0, HoldingReg,4,reg_value.DATA_10H);
}
on key 'b'
{
MB_WriteHoldingReg_06H(MB_SLAVEADDR_0, HoldingReg, 1000);
}
on key 'c'
{
MB_WriteHoldingReg_06H(MB_SLAVEADDR_0, HoldingReg, 1);
}
on key 'd'
{
MB_WriteHoldingReg_06H(MB_SLAVEADDR_1, HoldingReg, 1000);
}
on key 'e'
{
MB_WriteHoldingReg_06H(MB_SLAVEADDR_1, HoldingReg, 1);
}
on key 't'
{
MB_ReadHoldingReg_03H(MB_SLAVEADDR_1, HoldingReg, 3);
}
on key 'g'
{
MB_ReadHoldingReg_03H(MB_SLAVEADDR_1, HoldingReg, 4);
}