?? modbus.c
字號(hào):
temp2 &= temp1;
temp1 = temp5 + 2;
Asc1_Tx_BUF[temp1] = temp2;
Deal_all_OK = 1;
//讀取離散輸出是否正常
if (Deal_all_OK == 0)
{
Asc1_Tx_BUF[1] = 0x81;
Asc1_Tx_BUF[2] = 0x04;
Asc1_Tx_lengh = 3;
}
}
}
}
if (Deal_all_OK == 1)
{
Deal_all_OK = 0;
//計(jì)算發(fā)送緩沖區(qū)
Asc1_Tx_lengh = 3 + temp5;
crc_cal( Asc1_Tx_BUF, Asc1_Tx_lengh );
Asc1_Tx_lengh += 2;
//發(fā)送緩沖區(qū)數(shù)據(jù)
Send_Asc0_Tx_buf();
}
else
{
//計(jì)算發(fā)送緩沖區(qū)
crc_cal( Asc1_Tx_BUF, Asc1_Tx_lengh );
Asc1_Tx_lengh += 2;
//發(fā)送緩沖區(qū)數(shù)據(jù)
Send_Asc0_Tx_buf();
}
}
//****************************************************************************
// @Function void Deal_WRITE_One_Coilr()
// @Description 寫單個(gè)線圈
//****************************************************************************
__inline void Deal_WRITE_One_Coilr()
{
bit Deal_all_OK = 0;
ubyte temp1, temp2, temp5;
uword temp3, temp4;
//判定功能碼是否合法(05,但是幀長超出8個(gè)字節(jié))
if (Asc1_Rx_lengh != 8)
{
Asc1_Tx_BUF[1] = 0x85;
Asc1_Tx_BUF[2] = 0x01;
Asc1_Tx_lengh = 3;
}
else
{
//判定寄存器值是否合法(0 ---0xFFFF)
temp3 = ( uword ) Asc1_Rx_BUF[4] * 256 + Asc1_Rx_BUF[5];
if (temp3 != 0xFF00 && temp3 != 0x0000)
{
Asc1_Tx_BUF[1] = 0x85;
Asc1_Tx_BUF[2] = 0x03;
Asc1_Tx_lengh = 3;
}
else
{
//判定寄存器地址是否合法
temp4 = ( uword ) Asc1_Rx_BUF[2] * 256 + Asc1_Rx_BUF[3];
if (temp4 >= ModBus_PLC_Bit_max) //---------------------------------
{
Asc1_Tx_BUF[1] = 0x85;
Asc1_Tx_BUF[2] = 0x02;
Asc1_Tx_lengh = 3;
}
else
{
//處理寫請(qǐng)求
temp2 = Asc1_Rx_BUF[3] / 8; //數(shù)組的第幾個(gè)字節(jié)
temp1 = ( ubyte ) ( Asc1_Rx_BUF[3] % 8 ); //對(duì)應(yīng)字節(jié)的第幾位
temp5 = ( 0x01 ) << ( temp1 );
if (temp3 == 0xFF00)
{
PLC_Bit_Buffer_all.PLC_Bit_Buffer[temp2] += temp5;
}
else
{
temp5 = ~temp5;
PLC_Bit_Buffer_all.PLC_Bit_Buffer[temp2] &= temp5;
}
Deal_all_OK = 1;
//寫取離散輸出是否正常
if (Deal_all_OK == 0)
{
Asc1_Tx_BUF[1] = 0x85;
Asc1_Tx_BUF[2] = 0x04;
Asc1_Tx_lengh = 3;
}
}
}
}
if (Deal_all_OK == 1)
{
Deal_all_OK = 0;
Send_Asc0_Rx_buf_Back();
}
else
{
//計(jì)算發(fā)送緩沖區(qū)
crc_cal( Asc1_Tx_BUF, Asc1_Tx_lengh );
Asc1_Tx_lengh += 2;
//發(fā)送緩沖區(qū)數(shù)據(jù)
Send_Asc0_Tx_buf();
}
}
//****************************************************************************
// @Function void Deal_WRITE_One_Register()
// @Description 寫一個(gè)保持寄存器
//****************************************************************************
__inline void Deal_WRITE_One_Register()
{
bit Deal_all_OK = 0;
//ubyte temp1,temp2;
uword temp3, temp4;
//判定功能碼是否合法(06,但是幀長超出8個(gè)字節(jié))
//測試數(shù)組01 06 00 01 00 50 00 36 5A
if (Asc1_Rx_lengh != 8)
{
Asc1_Tx_BUF[1] = 0x86;
Asc1_Tx_BUF[2] = 0x01;
Asc1_Tx_lengh = 3;
}
else
{
//判定寄存器值是否合法(0 ---0xFFFF)
//測試數(shù)組
temp3 = ( uword ) Asc1_Rx_BUF[2] * 256 + Asc1_Rx_BUF[3];
if (temp3 > ModBus_PLC_Word_max)
{
Asc1_Tx_BUF[1] = 0x86;
Asc1_Tx_BUF[2] = 0x03;
Asc1_Tx_lengh = 3;
}
else
{
//判定寄存器地址是否合法
//測試數(shù)組01 06 01 01 00 50 D9 CA
temp4 = temp3;
if (temp4 > ModBus_PLC_Word_max) //-------
{
Asc1_Tx_BUF[1] = 0x86;
Asc1_Tx_BUF[2] = 0x02;
Asc1_Tx_lengh = 3;
}
else
{
//處理寫請(qǐng)求
temp3 = temp3 * 2;
PLC_Word_Buffer.U_W_B_A.word_int[temp3] = Asc1_Rx_BUF[5];
temp3++;
PLC_Word_Buffer.U_W_B_A.word_int[temp3] = Asc1_Rx_BUF[4];
Deal_all_OK = 1;
}
}
}
if (Deal_all_OK == 1)
{
Deal_all_OK = 0;
Send_Asc0_Rx_buf_Back();
}
else
{
//計(jì)算發(fā)送緩沖區(qū)
crc_cal( Asc1_Tx_BUF, Asc1_Tx_lengh );
Asc1_Tx_lengh += 2;
//發(fā)送緩沖區(qū)數(shù)據(jù)
Send_Asc0_Tx_buf();
}
}
//****************************************************************************
// @Function void Deal_READ_Hold_Registers()
// @Description 讀保持寄存器
// 01 03 00 0A 00 01 A4 08
// 01 03 00 0E 00 04 25 CA
//****************************************************************************
__inline void Deal_READ_Hold_Registers()
{
bit Deal_all_OK = 0;
ubyte temp1, temp2;
uword temp3, temp4;
//PLC_Word_Buffer.U_W_B_A.uword_value[13]++;
//PLC_Word_Buffer.U_W_B_A.uword_value[14]++;
//PLC_Word_Buffer.U_W_B_A.uword_value[15]++;
//判定功能碼是否合法(03,但是幀長超出8個(gè)字節(jié))
if (Asc1_Rx_lengh != 8)
{
Asc1_Tx_BUF[1] = 0x83;
Asc1_Tx_BUF[2] = 0x01;
Asc1_Tx_lengh = 3;
}
else
{
//判定讀取首地址是否合法
if (Asc1_Rx_BUF[2] > 0 || Asc1_Rx_BUF[3] > ModBus_PLC_Word_max)
{
Asc1_Tx_BUF[1] = 0x83;
Asc1_Tx_BUF[2] = 0x03;
Asc1_Tx_lengh = 3;
}
else
{
//判定讀取末地址是否合法
temp3 = ( uword ) Asc1_Rx_BUF[4] * 256 + Asc1_Rx_BUF[5];
temp4 = temp3 + Asc1_Rx_BUF[3];
if (temp4 > ModBus_PLC_Word_max || Asc1_Rx_BUF[4] > 0)
{
Asc1_Tx_BUF[1] = 0x83;
Asc1_Tx_BUF[2] = 0x02;
Asc1_Tx_lengh = 3;
}
else
{
//處理讀請(qǐng)求
Asc1_Tx_BUF[0] = 0x01;
Asc1_Tx_BUF[1] = 0x03;
temp1 = ( ubyte ) ( Asc1_Rx_BUF[5] * 2 );
Asc1_Tx_BUF[2] = temp1;
temp3 = Asc1_Rx_BUF[3] * 2;
//temp4 = temp1+3;
Asc1_Tx_lengh = temp1 + 3;
Asc1_Tx_BUF_point = &( Asc1_Tx_BUF[3] );
for (temp2 = temp1 / 2; temp2 > 0; temp2--)
{
Asc1_Tx_BUF_point++;
*Asc1_Tx_BUF_point = PLC_Word_Buffer.U_W_B_A.word_int[temp3++];
Asc1_Tx_BUF_point--;
*Asc1_Tx_BUF_point = PLC_Word_Buffer.U_W_B_A.word_int[temp3--];
temp3 += 2;
Asc1_Tx_BUF_point += 2;
}
Deal_all_OK = 1;
}
}
}
if (Deal_all_OK == 1)
{
Deal_all_OK = 0;
//計(jì)算發(fā)送緩沖區(qū)
//Asc1_Tx_lengh = (ubyte)(temp4);
crc_cal( Asc1_Tx_BUF, Asc1_Tx_lengh );
Asc1_Tx_lengh += 2;
//發(fā)送緩沖區(qū)數(shù)據(jù)
Send_Asc0_Tx_buf();
}
else
{
Deal_all_OK = 0;
//計(jì)算發(fā)送緩沖區(qū)
crc_cal( Asc1_Tx_BUF, Asc1_Tx_lengh );
Asc1_Tx_lengh += 2;
//發(fā)送緩沖區(qū)數(shù)據(jù)
Send_Asc0_Tx_buf();
}
}
//****************************************************************************
// @Function void Deal_WRITE_Some_Coil()
// @Description 強(qiáng)制多個(gè)線圈
//
//
//****************************************************************************
__inline void Deal_WRITE_Some_Coil()
{
}
//****************************************************************************
// @Function void Deal_WRITE_Some_Register()
// @Description 預(yù)置多寄存器
//****************************************************************************
__inline void Deal_WRITE_Some_Register()
{
bit Deal_all_OK = 0;
ubyte temp1, temp2;//,temp5;
uword temp3, temp4;
//判定功能碼是否合法(16,但是幀長超出8個(gè)字節(jié))
//包長= Node + FunCode + StarAdd + RegNum + Hbyte + Infmation + LCR
// = 1 + 1 + 2 + 2 + 1 + N + 2
// = N+9
temp1 = Asc1_Rx_BUF[6] + 9;
if (Asc1_Rx_lengh != temp1)
{
Asc1_Tx_BUF[1] = 0x90;
Asc1_Tx_BUF[2] = 0x01;
Asc1_Tx_lengh = 3;
}
else
{
//判定寄存器起始地址是否正確
temp3 = ( uword ) Asc1_Rx_BUF[2] * 256 + Asc1_Rx_BUF[3];
if (temp3 > ModBus_PLC_Word_max)
{
Asc1_Tx_BUF[1] = 0x90;
Asc1_Tx_BUF[2] = 0x03;
Asc1_Tx_lengh = 3;
}
else
{
//判定最后一個(gè)寄存器地址是否正確
temp4 = ( uword ) Asc1_Rx_BUF[4] * 256 + Asc1_Rx_BUF[5];
//temp5 = (ubyte)(temp4 - temp3)+1;
//temp5 *= 2;
temp4 = Asc1_Rx_BUF[3] + Asc1_Rx_BUF[5] - 1;
if (temp4 > ModBus_PLC_Word_max)
{
Asc1_Tx_BUF[1] = 0x90;
Asc1_Tx_BUF[2] = 0x02;
Asc1_Tx_lengh = 3;
}
else
{
//處理寫請(qǐng)求
temp3 = Asc1_Rx_BUF[3] * 2; //確定開始字節(jié)數(shù)組號(hào)
temp2 = 8; //接受信息字節(jié)號(hào)
for (; temp4 > 0; temp4--)
{
//寫字的低字節(jié)
PLC_Word_Buffer.U_W_B_A.word_int[temp3] = Asc1_Rx_BUF[temp2];
temp3++;
temp2--;
//寫字的高字節(jié)
PLC_Word_Buffer.U_W_B_A.word_int[temp3] = Asc1_Rx_BUF[temp2];
temp2 += 3;
temp3++;
}
Deal_all_OK = 1;
}
}
}
if (Deal_all_OK == 1)
{
Deal_all_OK = 0;
for (temp1 = 0; temp1 < 6; temp1++)
{
Asc1_Tx_BUF[temp1] = Asc1_Rx_BUF[temp1];
}
Asc1_Tx_lengh = 6;
crc_cal( Asc1_Tx_BUF, Asc1_Tx_lengh );
Asc1_Tx_lengh += 2;
//發(fā)送緩沖區(qū)數(shù)據(jù)
Send_Asc0_Tx_buf();
}
else
{
//計(jì)算發(fā)送緩沖區(qū)
crc_cal( Asc1_Tx_BUF, Asc1_Tx_lengh );
Asc1_Tx_lengh += 2;
//發(fā)送緩沖區(qū)數(shù)據(jù)
Send_Asc0_Tx_buf();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -