modbus代碼如下,可能有很多人已經看過了,想問的問題是:
1、代碼是否有用?
2、紅色字里,接識訓發送時,這幾個引數是什么意思,該怎么填寫? 請用白話解釋下
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Windows.Forms;
namespace CSharpModBusExample
{
public enum FunctionCode : byte
{
/// <summary>
/// Read Multiple Registers
/// </summary>
Read = 3,
/// <summary>
/// Write Multiple Registers
/// </summary>
Write = 16
}
internal class ModBusTCPIPWrapper : ModBusWrapper, IDisposable
{
private static short StartingAddress = short.Parse(ConfigurationManager.AppSettings["StartingAddress"]);
public static ModBusTCPIPWrapper Instance = new ModBusTCPIPWrapper();
private SocketWrapper socketWrapper = new SocketWrapper();
bool connected = false;
public override void Connect()
{
if (!connected)
{
this.socketWrapper.Logger = this.Logger;
this.socketWrapper.Connect();
this.connected = true;
}
}
public override byte[] Receive()
{
this.Connect();
List<byte> sendData = new List<byte>(255);
//[1].Send
sendData.AddRange(ValueHelper.Instance.GetBytes(this.NextDataIndex()));//1~2.(Transaction Identifier)
sendData.AddRange(new Byte[] { 0, 0 });//3~4:Protocol Identifier,0 = MODBUS protocol
sendData.AddRange(ValueHelper.Instance.GetBytes((short)6));//5~6:后續的Byte數量(針對讀請求,后續為6個byte)
sendData.Add(0);//7: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.需要讀取的暫存器數量
this.socketWrapper.Write(sendData.ToArray()); //發送讀請求
//[2].防止連續讀寫引起前臺UI執行緒阻塞
Application.DoEvents();
//[3].讀取Response Header : 完后會回傳8個byte的Response Header
byte[] receiveData = this.socketWrapper.Read(256);//緩沖區中的資料總量不超過256byte,一次讀256byte,防止殘余資料影響下次讀取
short identifier = (short)((((short)receiveData[0]) << 8) + receiveData[1]);
//[4].讀取回傳資料:根據ResponseHeader,讀取后續的資料
if (identifier != this.CurrentDataIndex) //請求的資料標識與回傳的標識不一致,則丟掉資料包
{
return new Byte[0];
}
byte length = receiveData[8];//最后一個位元組,記錄暫存器中資料的Byte數
byte[] result = new byte[length];
Array.Copy(receiveData, 9, result, 0, length);
return result;
}
public override void Send(byte[] data)
{
//[0]:填充0,清掉剩余的暫存器
if (data.Length < 60)
{
var input = data;
data = new Byte[60];
Array.Copy(input, data, input.Length);
}
this.Connect();
List<byte> values = new List<byte>(255);
//[1].Write Header:MODBUS Application Protocol header
values.AddRange(ValueHelper.Instance.GetBytes(this.NextDataIndex()));//1~2.(Transaction Identifier)
values.AddRange(new Byte[] { 0, 0 });//3~4:Protocol Identifier,0 = MODBUS protocol
values.AddRange(ValueHelper.Instance.GetBytes((byte)(data.Length + 7)));//5~6:后續的Byte數量
values.Add(0);//7:Unit Identifier:This field is used for intra-system routing purpose.
values.Add((byte)FunctionCode.Write);//8.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.暫存器數量
values.Add((byte)data.Length);//13.資料的Byte數量
//[2].增加資料
values.AddRange(data);//14~End:需要發送的資料
//[3].寫資料
this.socketWrapper.Write(values.ToArray());
//[4].防止連續讀寫引起前臺UI執行緒阻塞
Application.DoEvents();
//[5].讀取Response: 寫完后會回傳12個byte的結果
byte[] responseHeader = this.socketWrapper.Read(12);
}
#region IDisposable 成員
public override void Dispose()
{
socketWrapper.Dispose();
}
#endregion
}
}
uj5u.com熱心網友回復:
急急急,重要的事,說3遍, 如果分數,我再加。。。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/116938.html
標籤:網絡協議與配置
上一篇:求助,局域網內網延遲不穩定的問題
下一篇:C#網路傳輸
