標題: modbus rtu原理及通訊工具編寫代碼 [打印本頁]
作者: andy9900010 時間: 2017-5-12 23:32
標題: modbus rtu原理及通訊工具編寫代碼
Modbus 是一個工業上常用的通訊協議、一種通訊約定。
ModBus 協議是應用層報文傳輸協議(OSI 模型第7層),它定義了一個與通信層無關的協議數據單元(PDU),即PDU=功能碼+數據域。
ModBus 協議能夠應用在不同類型的總線或網絡。對應不同的總線或網絡,Modbus 協議引入一些附加域映射成應用數據單元(ADU),即ADU=附加域+PDU。目前,Modbus 有下列三種通信方式:
1. 以太網,對應的通信模式是Modbus TCP。
2. 異步串行傳輸(各種介質如有線RS-232-/422/485/;光纖、無線等),對應的通信模式是 Modbus RTU 或 Modbus ASCII。
Modbus 的ASCII、RTU 協議規定了消息、數據的結構、命令和應答的方式,數據通訊采用Maser/Slave方式。
3. 高速令牌傳遞網絡,對應的通信模式是Modbus PLUS。
Modbus 需要對數據進行校驗,串行協議中除有奇偶校驗外,ASCII 模式采用LRC 校驗;RTU模式采用16位CRC 校驗;TCP 模式沒有額外規定校驗,因為TCP 是一個面向連接的可靠協議。
Modbus 協議的應用中,最常用的是Modbus RTU 傳輸模式。
RTU 傳輸模式
當設備使用RTU (Remote Terminal Unit) 模式在 Modbus 串行鏈路通信, 報文中每個8位字節含有兩個4位十六進制字符。這種模式的主要優點是較高的數據密度,在相同的波特率下比ASCII 模式有更高的吞吐率。每個報文必須以連續的字符流傳送。
RTU 模式每個字節 ( 11 位 ) 的格式為:
編碼系統: 8位二進制。 報文中每個8位的字節含有兩個4位十六進制字符(0–9, A–F)
Bits per Byte: 1 起始位
8 數據位, 首先發送最低有效位
1 位作為奇偶校驗
1 停止位
偶校驗是要求的,其它模式 ( 奇校驗, 無校驗 ) 也可以使用。為了保證與其它產品的最大兼容性,同時支持無校驗模式是建議的。默認校驗模式模式 必須為偶校驗。注:使用無校驗要求2 個停止位。
字符的串行傳送方式:
每個字符或字節均由此順序發送(從左到右):最低有效位 (LSB) . . . 最高有效位 (MSB)
file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image002.jpg
圖1:RTU 模式位序列
設備配置為奇校驗、偶校驗或無校驗都可以接受。如果無奇偶校驗,將傳送一個附加的停止位以填充字符幀:
file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image004.jpg
圖2:RTU 模式位序列 (無校驗的特殊情況)
幀檢驗域:循環冗余校驗 (CRC)
在RTU 模式包含一個對全部報文內容執行的,基于循環冗余校驗 (CRC - Cyclical Redundancy Checking) 算法的錯誤檢驗域。
CRC 域檢驗整個報文的內容。不管報文有無奇偶校驗,均執行此檢驗。
CRC 包含由兩個8位字節組成的一個16位值。
CRC 域作為報文的最后的域附加在報文之后。計算后,首先附加低字節,然后是高字節。CRC 高字節為報文發送的最后一個子節。
附加在報文后面的CRC 的值由發送設備計算。接收設備在接收報文時重新計算 CRC 的值,并將計算結果于實際接收到的CRC 值相比較。如果兩個值不相等,則為錯誤。
CRC 的計算,開始對一個16位寄存器預裝全1。 然后將報文中的連續的8位子節對其進行后續的計算。只有字符中的8個數據位參與生成CRC 的運算,起始位,停止位和校驗位不參與 CRC 計算。
CRC 的生成過程中, 每個 8–位字符與寄存器中的值異或。然后結果向最低有效位(LSB)方向移動(Shift) 1位,而最高有效位(MSB)位置充零。 然后提取并檢查 LSB:如果LSB 為1, 則寄存器中的值與一個固定的預置值異或;如果LSB 為 0, 則不進行異或操作。
這個過程將重復直到執行完8次移位。完成最后一次(第8次)移位及相關操作后,下一個8位字節與寄存器的當前值異或,然后又同上面描述過的一樣重復8次。當所有報文中子節都運算之后得到的寄存器中的最終值,就是CRC。
當CRC 附加在報文之后時,首先附加低字節,然后是高字節。
CRC 算法如下:
private bool CheckResponse(byte[] response)
{
//Perform a basicCRC check:
byte[] CRC= new byte[2];
GetCRC(response, ref CRC);
if(CRC[0] == response[response.Length - 2] && CRC[1]== response[response.Length - 1])
return true;
else
return false;
}
private void GetCRC(byte[]message, ref byte[] CRC)
{
//Function expects amodbus message of any length as well as a 2 byte CRC array in which to
//return the CRC values:
ushortCRCFull = 0xFFFF;
byteCRCHigh = 0xFF, CRCLow = 0xFF;
char CRCLSB;
for (int i = 0; i < (message.Length)- 2; i++)
{
CRCFull = (ushort)(CRCFull ^message);
for (int j = 0; j < 8; j++)
{
CRCLSB = (char)(CRCFull & 0x0001);
CRCFull = (ushort)((CRCFull >> 1)& 0x7FFF);
if(CRCLSB == 1)
CRCFull = (ushort)(CRCFull ^ 0xA001);
}
}
CRC[1] =CRCHigh = (byte)((CRCFull >> 8) & 0xFF);
CRC[0] =CRCLow = (byte)(CRCFull & 0xFF);
}
幀描述 (如下圖所示) :
file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image006.jpg
圖3:RTU 報文幀
注意:Modbus RTU 幀最大為256字節。
下面是我為公司設計的一個 ModbusRTU 通信測試小工具,界面截圖如下:
file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image008.jpg
圖4:Modbus RTU 通信工具
我的通用Modbus RTU 動態庫,modbus.cs 如下:
file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image009.gifmodbus.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.Threading;
namespace SerialPort_Lib
{
public class modbus
{
privateSerialPort sp = newSerialPort();
public string modbusStatus;
#regionConstructor / Deconstructor
public modbus()
{
}
~modbus()
{
}
#endregion
#regionOpen / Close Procedures
public bool Open(stringportName, int baudRate, int databits, Parity parity, StopBitsstopBits)
{
//Ensureport isn't already opened:
if(!sp.IsOpen)
{
//Assigndesired settings to the serial port:
sp.PortName = portName;
sp.BaudRate = baudRate;
sp.DataBits = databits;
sp.Parity = parity;
sp.StopBits = stopBits;
//Thesetimeouts are default and cannot be editted through the class at this point:
sp.ReadTimeout = -1;
sp.WriteTimeout = 10000;
try
{
sp.Open();
}
catch (Exception err)
{
modbusStatus = "Error opening " + portName + ": " +err.Message;
return false;
}
modbusStatus =portName + " opened successfully";
return true;
}
else
{
modbusStatus =portName + " already opened";
return false;
}
}
public bool Close()
{
//Ensureport is opened before attempting to close:
if (sp.IsOpen)
{
try
{
sp.Close();
}
catch (Exception err)
{
modbusStatus = "Error closing " + sp.PortName + ": " +err.Message;
return false;
}
modbusStatus =sp.PortName + " closed successfully";
return true;
}
else
{
modbusStatus =sp.PortName + " is not open";
return false;
}
}
#endregion
#regionCRC Computation
privatevoid GetCRC(byte[]message, ref byte[] CRC)
{
//Functionexpects a modbus message of any length as well as a 2 byte CRC array in whichto
//return the CRC values:
ushortCRCFull = 0xFFFF;
byteCRCHigh = 0xFF, CRCLow = 0xFF;
char CRCLSB;
for (int i = 0; i <(message.Length) - 2; i++)
{
CRCFull = (ushort)(CRCFull ^message);
for (int j = 0; j < 8; j++)
{
CRCLSB = (char)(CRCFull & 0x0001);
CRCFull = (ushort)((CRCFull >> 1)& 0x7FFF);
if(CRCLSB == 1)
CRCFull = (ushort)(CRCFull ^ 0xA001);
}
}
CRC[1] =CRCHigh = (byte)((CRCFull >> 8) & 0xFF);
CRC[0] =CRCLow = (byte)(CRCFull & 0xFF);
}
#endregion
#regionBuild Message
privatevoid BuildMessage(byteaddress, byte type, ushortstart, ushort registers, ref byte[] message)
{
//Arrayto receive CRC bytes:
byte[]CRC = new byte[2];
message[0] = address;
message[1] = type;
message[2] =(byte)(start >> 8);
message[3] =(byte)start;
message[4] =(byte)(registers >> 8);
message[5] =(byte)registers;
GetCRC(message, ref CRC);
message[message.Length - 2] = CRC[0];
message[message.Length - 1] = CRC[1];
}
#endregion
#regionCheck Response
privatebool CheckResponse(byte[] response)
{
//Performa basic CRC check:
byte[]CRC = new byte[2];
GetCRC(response, ref CRC);
if(CRC[0] == response[response.Length - 2] && CRC[1]== response[response.Length - 1])
return true;
else
returnfalse;
}
#endregion
#regionGet Response
privatevoid GetResponse(refbyte[] response)
{
//Thereis a bug in .Net 2.0 DataReceived Event that prevents people from using this
//event as an interrupt to handle data(it doesn't fire all of the time). Therefore
//we have to use the ReadBytecommand for a fixed length as it's been shown to be reliable.
for(int i = 0; i< response.Length; i++)
{
response = (byte)(sp.ReadByte());
}
}
#endregion
#regionGetModbusData 獲得接收數據
public bool GetModbusData(refbyte[] values)
{
//Ensureport is open:
if (sp.IsOpen)
{
// 等待線程進入
//Monitor.Enter(sp);
//Clear in/out buffers:
//sp.DiscardOutBuffer();
//sp.DiscardInBuffer();
//Message is 1 addr + 1 type +N Data + 2 CRC
try
{
//GetResponse(refreadBuffer);
//string str =readBuffer.ToString();
intcount = sp.BytesToRead;
if(count > 0)
{
byte[] readBuffer = newbyte[count];
GetResponse(ref readBuffer);
// readData = newbyte[29];
// Array.Copy(readBuffer, readData,readData.Length);
// CRC 驗證
if (CheckResponse(readBuffer))
{
//顯示輸入數據
values = readBuffer;
modbusStatus = "Write successful";
sp.DiscardInBuffer();
//values = System.Text.Encoding.ASCII.GetString(readData);
return true;
}
else
{
modbusStatus = "CRC error";
sp.DiscardInBuffer();
return false;
}
}
else return false;
}
catch (Exception err)
{
modbusStatus = "Error in write event: " + err.Message;
sp.DiscardInBuffer();
return false;
}
//finally
//{
// 通知其它對象
//Monitor.Pulse(sp);
// 釋放對象鎖
//Monitor.Exit(sp);
//}
}
else
{
modbusStatus = "Serial port not open";
return false;
}
}
#endregion
#regionSendModbusData 打包發送數據
public bool SendModbusData(refbyte[] values)
{
//Ensureport is open:
if (sp.IsOpen)
{
//Clearin/out buffers:
sp.DiscardOutBuffer();
sp.DiscardInBuffer();
//Function3 response buffer:
byte[]response = new byte[values.Length+ 2];
Array.Copy(values, response,values.Length);
//BuildMessage(address,(byte)3, start, registers, ref message);
//打包帶有 CRC 驗證的modbus 數據包:
byte[]CRC = new byte[2];
GetCRC(response, ref CRC);
response[response.Length - 2] = CRC[0];
response[response.Length - 1] = CRC[1];
values = response; //返回帶有 CRC 驗證的modbus 數據包
//Send modbus message to SerialPort:
try
{
sp.Write(response, 0, response.Length);
//GetResponse(refresponse);
returntrue;
}
catch (Exception err)
{
modbusStatus = "Error in read event: " + err.Message;
return false;
}
//Evaluatemessage:
//if (CheckResponse(response))
//{
// //Returnrequested register values:
// for (int i = 0; i < (response.Length -5) / 2; i++)
// {
// values = response[2 * i + 3];
// values <<= 8;
// values += response[2 * i + 4];
// }
// modbusStatus = "Read successful";
// return true;
//}
//else
//{
// modbusStatus = "CRC error";
// return false;
//}
}
else
{
modbusStatus = "Serial port not open";
return false;
}
歡迎光臨 (http://www.raoushi.com/bbs/) |
Powered by Discuz! X3.1 |