淺談C#使用TCP/IP與ModBus進(jìn)行通訊
Client與Server之間有兩種通訊方式:一種是TCP/IP,另一種是通過(guò)串口(Serial Port),本文重點(diǎn)介紹***種通訊方式。第二種方式留了接口,暫時(shí)還沒有實(shí)現(xiàn)。
2. 數(shù)據(jù)包格式及MBAP header (MODBUS Application Protocol header)
2.1 數(shù)據(jù)包格式
數(shù)據(jù)交換過(guò)程中,數(shù)據(jù)包的格式由三部分組成:協(xié)議頭 + 功能碼 + 數(shù)據(jù)(請(qǐng)求或接受的數(shù)據(jù))。
這里主要用到下列兩個(gè)功能碼(十進(jìn)制):
3: 讀取寄存器中的值(Read Multiple Register)
16: 往寄存器中寫值(Write Multiple Register)
2.2 MBAP header
協(xié)議頭具體包括下列4個(gè)字段:
(1) Transaction Identifier:事務(wù)ID標(biāo)識(shí),Client每發(fā)送一個(gè)Request數(shù)據(jù)包的時(shí)候,需要帶上該標(biāo)識(shí);當(dāng)Server響應(yīng)該請(qǐng)求的時(shí)候,會(huì)把該標(biāo)識(shí)復(fù)制到Response中;這樣客戶端就可以進(jìn)行容錯(cuò)判斷,防止數(shù)據(jù)包發(fā)串了。
(2) Protocal Identifier:協(xié)議標(biāo)識(shí),ModBus協(xié)議中,該值為0;
(3) Length:整個(gè)數(shù)據(jù)包中,從當(dāng)個(gè)前這個(gè)字節(jié)之后開始計(jì)算,后續(xù)數(shù)據(jù)量的大小(按byte計(jì)算)。
(4) Unit Identifier:
3. 大小端轉(zhuǎn)換
ModBus使用Big-Endian表示地址和數(shù)據(jù)項(xiàng)。因此在發(fā)送或者接受數(shù)據(jù)的過(guò)程中,需要對(duì)數(shù)據(jù)進(jìn)行轉(zhuǎn)換。
3.1 判斷大小端
對(duì)于整數(shù)1,在兩種機(jī)器上有兩種不同的標(biāo)示方式,如上圖所示;因此,我們可以用&操作符來(lái)取其地址,再轉(zhuǎn)換成指向byte的指針(byte*),***再取該指針的值;若得到的byte值為1,則為L(zhǎng)ittle-Endian,否則為Big-Endian。
- unsafe
- {
- inttester = 1;
- boollittleEndian = (*(byte*)(&tester)) == (byte)1;
- }
3.2 整數(shù)/浮點(diǎn)數(shù)轉(zhuǎn)換成Byte數(shù)組
.Net提供了現(xiàn)成的API,可以BitConverter.GetBytes(value)和BitConverter.ToXXOO(Byte[] data)來(lái)進(jìn)行轉(zhuǎn)換。下面的代碼對(duì)該轉(zhuǎn)換進(jìn)行了封裝,加入了Little-Endian轉(zhuǎn)Big-Endian的處理(以int為例):
- publicclassValueHelper //Big-Endian可以直接轉(zhuǎn)換
- {
- publicvirtualByte[] GetBytes(intvalue)
- {
- returnBitConverter.GetBytes(value);
- }
- publicvirtualintGetInt(byte[] data)
- {
- returnBitConverter.ToInt32(data, 0);
- }
- }
- internalclassLittleEndianValueHelper : ValueHelper //Little-Endian,轉(zhuǎn)換時(shí)需要做翻轉(zhuǎn)處理。
- {
- publicoverrideByte[] GetBytes(intvalue)
- {
- returnthis.Reverse(BitConverter.GetBytes(value));
- }
- publicvirtualintGetInt(byte[] data)
- {
- returnBitConverter.ToInt32(this.Reverse(data), 0);
- }
- privateByte[] Reverse(Byte[] data)
- {
- Array.Reverse(data);
- returndata;
- }
- }
4. 事務(wù)標(biāo)識(shí)和緩沖處理
4.1 Transaction Identifier
上面2.2節(jié)中提到,Client每發(fā)送一個(gè)Request數(shù)據(jù)包的時(shí)候,需要帶上一個(gè)標(biāo)識(shí);當(dāng)Server響應(yīng)該請(qǐng)求的時(shí)候,會(huì)把該標(biāo)識(shí)復(fù)制到Response中,返回給Client。這樣Client就可以用來(lái)判斷數(shù)據(jù)包有沒有發(fā)串。在程序中,可以可以用一個(gè)變量及記錄該標(biāo)識(shí):
- privatebytedataIndex = 0;
- protectedbyteCurrentDataIndex
- {
- get { returnthis.dataIndex; }
- }
- protectedbyteNextDataIndex()
- {
- return++this.dataIndex;
- }
每次Client發(fā)送數(shù)據(jù)的時(shí)候,調(diào)用NextDataIndex()來(lái)取得事務(wù)標(biāo)識(shí);接著當(dāng)Client讀取Server的返回值的時(shí)候,需要判斷數(shù)據(jù)包中的數(shù)據(jù)標(biāo)識(shí)是否與發(fā)送時(shí)的標(biāo)志一致;如果一致,則認(rèn)為數(shù)據(jù)包有效;否則丟掉無(wú)效的數(shù)據(jù)包。
4.2 緩沖處理
上節(jié)中提到,如果Client接收到的響應(yīng)數(shù)據(jù)包中的標(biāo)識(shí),與發(fā)送給Server的數(shù)據(jù)標(biāo)識(shí)不一致,則認(rèn)為Server返回的數(shù)據(jù)包無(wú)效,并丟棄該數(shù)據(jù)包。
如果只考慮正常情況,即數(shù)據(jù)木有差錯(cuò),Client每次發(fā)送請(qǐng)求后,其請(qǐng)求包里面包含需要讀取的寄存器數(shù)量,能算出從Server返回的數(shù)據(jù)兩大小,這樣就能確定讀完Server返回的所有緩沖區(qū)中的數(shù)據(jù);每次交互后,Socket緩沖區(qū)中都為空,則整個(gè)過(guò)程沒有問(wèn)題。但是問(wèn)題是:如果Server端出錯(cuò),或者數(shù)據(jù)串包等異常情況下,Client不能確定Server返回的數(shù)據(jù)包(占用的緩沖區(qū))有多大;如果緩沖區(qū)中的數(shù)據(jù)沒有讀完,下次再?gòu)木彌_區(qū)中接著讀的時(shí)候,數(shù)據(jù)包必然是不正確的,而且會(huì)錯(cuò)誤會(huì)一直延續(xù)到后續(xù)的讀取操作中。
因此,每次讀取數(shù)據(jù)時(shí),要么全部讀完緩沖區(qū)中的數(shù)據(jù),要么讀到錯(cuò)誤的時(shí)候,就必須清楚緩沖區(qū)中剩余的數(shù)據(jù)。網(wǎng)上搜了半天,木有找到Windows下如何清理Socket緩沖區(qū)的。有篇文章倒是提到一個(gè)狠招,每次讀完數(shù)據(jù)后,直接把Socket給咔嚓掉;然后下次需要讀取或發(fā)送數(shù)據(jù)的時(shí)候,再重新建立Socket連接。
回過(guò)頭再來(lái)看,其實(shí),在Client與Server進(jìn)行交互的過(guò)程中,Server每次返回的數(shù)據(jù)量都不大,也就一個(gè)MBAP Header + 幾十個(gè)寄存器的值。因此,另一個(gè)處理方式,就是每次讀取盡可能多的數(shù)據(jù)(多過(guò)緩沖區(qū)中的數(shù)據(jù)量),多讀的內(nèi)容,再忽略掉。暫時(shí)這么處理,期待有更好的解決方法。
5. 源代碼
5.1 類圖結(jié)構(gòu):
5.2 使用示例
(1) 寫入數(shù)據(jù):
- this.Wrapper.Send(Encoding.ASCII.GetBytes(this.tbxSendText.Text.Trim()));
- publicoverridevoidSend(byte[] data)
- {
- //[0]:填充0,清掉剩余的寄存器
- if(data.Length <60)
- {
- var input = data;
- data = newByte[60];
- Array.Copy(input, data, input.Length);
- }
- this.Connect();
- List<byte>values = newList<byte>(255);
- //[1].Write Header:MODBUS Application Protocol header
- values.AddRange(ValueHelper.Instance.GetBytes(this.NextDataIndex()));//1~2.(Transaction Identifier)
- values.AddRange(newByte[] { 0, 0 });//
- Protocol Identifier,0 = MODBUS protocol
- values.AddRange(ValueHelper.Instance.GetBytes((byte)(data.Length + 7)));//
- 后續(xù)的Byte數(shù)量
- values.Add(0);//
- Unit Identifier:This field is used for intra-system routing purpose.
- values.Add((byte)FunctionCode.Write);//
- Function Code : 16 (Write Multiple Register)
- values.AddRange(ValueHelper.Instance.GetBytes(StartingAddress));//9~10.起始地址
- values.AddRange(ValueHelper.Instance.GetBytes((short)(data.Length / 2)));//11~12.寄存器數(shù)量
- values.Add((byte)data.Length);//13.數(shù)據(jù)的Byte數(shù)量
- //[2].增加數(shù)據(jù)
- values.AddRange(data);//14~End:需要發(fā)送的數(shù)據(jù)
- //[3].寫數(shù)據(jù)
- this.socketWrapper.Write(values.ToArray());
- //[4].防止連續(xù)讀寫引起前臺(tái)UI線程阻塞
- Application.DoEvents();
- //[5].讀取Response: 寫完后會(huì)返回12個(gè)byte的結(jié)果
- byte[] responseHeader = this.socketWrapper.Read(12);
- }
(2) 讀取數(shù)據(jù):
- this.tbxReceiveText.Text = Encoding.ASCII.GetString(this.Wrapper.Receive());
- publicoverridebyte[] Receive()
- {
- this.Connect();
- List<byte>sendData = newList<byte>(255);
- //[1].Send
- sendData.AddRange(ValueHelper.Instance.GetBytes(this.NextDataIndex()));//1~2.(Transaction Identifier)
- sendData.AddRange(newByte[] { 0, 0 });//3~4:Protocol Identifier,0 = MODBUS protocol
- sendData.AddRange(ValueHelper.Instance.GetBytes((short)6));//5~6:后續(xù)的Byte數(shù)量(針對(duì)讀請(qǐng)求,后續(xù)為6個(gè)byte)
- sendData.Add(0);//
- Unit Identifier:This field is used for intra-system routing purpose.
- sendData.Add((byte)FunctionCode.Read);//8.Function Code : 3 (Read Multiple Register)
- sendData.AddRange(ValueHelper.Instance.GetBytes(StartingAddress));//9~10.起始地址
- sendData.AddRange(ValueHelper.Instance.GetBytes((short)30));//11~12.需要讀取的寄存器數(shù)量
- this.socketWrapper.Write(sendData.ToArray()); //發(fā)送讀請(qǐng)求
- //[2].防止連續(xù)讀寫引起前臺(tái)UI線程阻塞
- Application.DoEvents();
- //[3].讀取Response Header : 完后會(huì)返回8個(gè)byte的Response Header22:byte[] receiveData = this.socketWrapper.Read(256);//緩沖區(qū)中的數(shù)據(jù)總量不超過(guò)256byte,一次讀256byte,防止殘余數(shù)據(jù)影響下次讀取
- shortidentifier = (short)((((short)receiveData[0]) <<8) + receiveData[1]);
- //[4].讀取返回?cái)?shù)據(jù):根據(jù)ResponseHeader,讀取后續(xù)的數(shù)據(jù)
- if(identifier != this.CurrentDataIndex) //請(qǐng)求的數(shù)據(jù)標(biāo)識(shí)與返回的標(biāo)識(shí)不一致,則丟掉數(shù)據(jù)包
- {
- returnnewByte[0];
- }
- bytelength = receiveData[8];//***一個(gè)字節(jié),記錄寄存器中數(shù)據(jù)的Byte數(shù)
- byte[] result = newbyte[length];
- Array.Copy(receiveData, 9, result, 0, length);
- returnresult;
- }
(3) 測(cè)試發(fā)送和讀取:
5.3 代碼下載
原文鏈接:http://www.cnblogs.com/happyhippy/archive/2011/07/17/2108976.html