找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 6354|回復(fù): 0
收起左側(cè)

modbus rtu原理及通訊工具編寫代碼

[復(fù)制鏈接]
ID:200143 發(fā)表于 2017-5-12 23:32 | 顯示全部樓層 |閱讀模式
Modbus 是一個工業(yè)上常用的通訊協(xié)議、一種通訊約定。
ModBus 協(xié)議是應(yīng)用層報文傳輸協(xié)議(OSI 模型第7層),它定義了一個與通信層無關(guān)的協(xié)議數(shù)據(jù)單元(PDU),即PDU=功能碼+數(shù)據(jù)域。
ModBus 協(xié)議能夠應(yīng)用在不同類型的總線或網(wǎng)絡(luò)。對應(yīng)不同的總線或網(wǎng)絡(luò),Modbus 協(xié)議引入一些附加域映射成應(yīng)用數(shù)據(jù)單元(ADU),即ADU=附加域+PDU。目前,Modbus 有下列三種通信方式:
1.    以太網(wǎng),對應(yīng)的通信模式是Modbus TCP。
2.    異步串行傳輸(各種介質(zhì)如有線RS-232-/422/485/;光纖、無線等),對應(yīng)的通信模式是 Modbus RTU 或 Modbus ASCII。
       Modbus 的ASCII、RTU 協(xié)議規(guī)定了消息、數(shù)據(jù)的結(jié)構(gòu)、命令和應(yīng)答的方式,數(shù)據(jù)通訊采用Maser/Slave方式。
3.    高速令牌傳遞網(wǎng)絡(luò),對應(yīng)的通信模式是Modbus PLUS。

Modbus 需要對數(shù)據(jù)進(jìn)行校驗,串行協(xié)議中除有奇偶校驗外,ASCII 模式采用LRC 校驗;RTU模式采用16位CRC 校驗;TCP 模式?jīng)]有額外規(guī)定校驗,因為TCP 是一個面向連接的可靠協(xié)議。

Modbus 協(xié)議的應(yīng)用中,最常用的是Modbus RTU 傳輸模式。

RTU 傳輸模式
當(dāng)設(shè)備使用RTU (Remote Terminal Unit) 模式在 Modbus  串行鏈路通信, 報文中每個8位字節(jié)含有兩個4位十六進(jìn)制字符。這種模式的主要優(yōu)點是較高的數(shù)據(jù)密度,在相同的波特率下比ASCII 模式有更高的吞吐率。每個報文必須以連續(xù)的字符流傳送。

RTU 模式每個字節(jié) ( 11 位 ) 的格式為:
      編碼系統(tǒng):  8位二進(jìn)制。 報文中每個8位的字節(jié)含有兩個4位十六進(jìn)制字符(0–9, A–F)
Bits per Byte:  1 起始位
                          8 數(shù)據(jù)位, 首先發(fā)送最低有效位
                          1 位作為奇偶校驗
                          1 停止位
偶校驗是要求的,其它模式 ( 奇校驗, 無校驗 ) 也可以使用。為了保證與其它產(chǎn)品的最大兼容性,同時支持無校驗?zāi)J绞墙ㄗh的。默認(rèn)校驗?zāi)J侥J?必須為偶校驗。注:使用無校驗要求2 個停止位。

字符的串行傳送方式:
每個字符或字節(jié)均由此順序發(fā)送(從左到右):最低有效位 (LSB) . . . 最高有效位 (MSB)
file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image002.jpg
圖1:RTU 模式位序列

設(shè)備配置為奇校驗、偶校驗或無校驗都可以接受。如果無奇偶校驗,將傳送一個附加的停止位以填充字符幀:
file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image004.jpg
圖2:RTU 模式位序列 (無校驗的特殊情況)

幀檢驗域:循環(huán)冗余校驗 (CRC)
在RTU 模式包含一個對全部報文內(nèi)容執(zhí)行的,基于循環(huán)冗余校驗 (CRC - Cyclical Redundancy Checking) 算法的錯誤檢驗域。
CRC 域檢驗整個報文的內(nèi)容。不管報文有無奇偶校驗,均執(zhí)行此檢驗。
CRC 包含由兩個8位字節(jié)組成的一個16位值。  
CRC 域作為報文的最后的域附加在報文之后。計算后,首先附加低字節(jié),然后是高字節(jié)。CRC 高字節(jié)為報文發(fā)送的最后一個子節(jié)。
附加在報文后面的CRC 的值由發(fā)送設(shè)備計算。接收設(shè)備在接收報文時重新計算 CRC 的值,并將計算結(jié)果于實際接收到的CRC 值相比較。如果兩個值不相等,則為錯誤。
CRC 的計算,開始對一個16位寄存器預(yù)裝全1。 然后將報文中的連續(xù)的8位子節(jié)對其進(jìn)行后續(xù)的計算。只有字符中的8個數(shù)據(jù)位參與生成CRC 的運算,起始位,停止位和校驗位不參與 CRC 計算。
CRC 的生成過程中, 每個 8–位字符與寄存器中的值異或。然后結(jié)果向最低有效位(LSB)方向移動(Shift) 1位,而最高有效位(MSB)位置充零。 然后提取并檢查 LSB:如果LSB 為1, 則寄存器中的值與一個固定的預(yù)置值異或;如果LSB 為 0, 則不進(jìn)行異或操作。
這個過程將重復(fù)直到執(zhí)行完8次移位。完成最后一次(第8次)移位及相關(guān)操作后,下一個8位字節(jié)與寄存器的當(dāng)前值異或,然后又同上面描述過的一樣重復(fù)8次。當(dāng)所有報文中子節(jié)都運算之后得到的寄存器中的最終值,就是CRC。
當(dāng)CRC 附加在報文之后時,首先附加低字節(jié),然后是高字節(jié)。
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字節(jié)。

下面是我為公司設(shè)計的一個 ModbusRTU 通信測試小工具,界面截圖如下:
file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image008.jpg
圖4:Modbus RTU 通信工具

我的通用Modbus RTU 動態(tài)庫,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 獲得接收數(shù)據(jù)
        public bool GetModbusData(refbyte[] values)
        {
            //Ensureport is open:
            if (sp.IsOpen)
            {
                // 等待線程進(jìn)入
                //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))
                        {
                            //顯示輸入數(shù)據(jù)
                            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 打包發(fā)送數(shù)據(jù)
        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 數(shù)據(jù)包:
                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 數(shù)據(jù)包
                //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;
            }
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表