1, the code is useful?
2, in red word, receive or send, what's the meaning of this a few parameters, how to fill in? Please use vernacular explain
using System;
Using System. Collections. Generic;
Using System. The Text;
Using System. The Configuration;
Using System. Windows. Forms;
The namespace CSharpModBusExample
{
Public enum FunctionCode: byte
{
///& lt; Summary>
///Read Multiple Registers
///& lt;/summary>
Read=3,
///& lt; Summary>
///Write Multiple Registers
///& lt;/summary>
Write=16
}
Internal class ModBusTCPIPWrapper: ModBusWrapper, IDisposable
{
Private static short StartingAddress=short. Parse (ConfigurationManager. The AppSettings [" StartingAddress] ");
Public static ModBusTCPIPWrapper Instance=new ModBusTCPIPWrapper ();
Private SocketWrapper SocketWrapper=new SocketWrapper ();
Bool connected=false;
Public override void the Connect ()
{
if (! Connected)
{
This. SocketWrapper. Logger=this. Logger;
This. SocketWrapper. The Connect ();
This. Connected=true;
}
}
Public override byte [] the Receive ()
{
Enclosing the Connect ();
List
//[1]. The Send
sendData publishes the event. AddRange (ValueHelper. Instance. GetBytes (enclosing NextDataIndex ()));//1 ~ 2. (the Transaction Identifier)
SendData publishes the event. AddRange (new Byte [] {0, 0}).//3 ~ 4: Protocol Identifier, 0=MODBUS Protocol
SendData publishes the event. AddRange (ValueHelper. Instance. GetBytes (short) (6));//5 ~ 6: subsequent Byte number (for read requests, the subsequent six Byte)
SendData publishes the event. Add (0);//7: the Unit Identifier: This field is, informs the for intra - system routing purpose.
SendData publishes the event. The Add ((byte) FunctionCode. Read);//8. The Function Code: 3 (Read Multiple Register)
SendData publishes the event. AddRange (ValueHelper. Instance. GetBytes (StartingAddress));//9 ~ 10. The starting address
SendData publishes the event. AddRange (ValueHelper. Instance. GetBytes (30) (short)); 11 ~ 12.//the number of registers to read
This. SocketWrapper. Write (sendData publishes the event ToArray ());//send a read request
//[2]. To prevent the front desk by continuous, speaking, reading and writing the UI thread blocking
Application.doevents ();
//[3]. Read the Response headers: will return after 8 byte of the Response Header
Byte [] receiveData=https://bbs.csdn.net/topics/this.socketWrapper.Read (256);//the data in the buffer does not exceed a total of 256 byte read 256 byte at a time, to prevent the residual data affect the next read
Short identifier=(short) ((((short) receiveData [0]) & lt; <8) + receiveData [1]);
//[4]. Read the return data: according to ResponseHeader, read the subsequent data
If (identifier.=this. CurrentDataIndex)//request data identification and returns the identity of the inconsistencies, lost packets
{
Return new Byte [0];
}
Byte length=receiveData [8].//the last Byte, record the register of the data in the Byte count
Byte [] result=new byte (length),
Array. Copy (receiveData, 9, result, 0, length);
return result;
}
Public override void the Send (byte [] data)
{
//[0] : filling 0, clear out the rest of the register
If (data. The Length & lt; 60)
{
Var input=data;
Data=https://bbs.csdn.net/topics/new Byte [60];
Array. Copy (input, data, input Length);
}
Enclosing the Connect ();
List
//[1]. Write the Header: the MODBUS Application Protocol Header
Values. AddRange (ValueHelper. Instance. GetBytes (enclosing NextDataIndex ()));//1 ~ 2. (the Transaction Identifier)
Values. The AddRange (new Byte [] {0, 0}).//3 ~ 4: Protocol Identifier, 0=MODBUS Protocol
Values. AddRange (ValueHelper. Instance. GetBytes ((byte) (data. Length + 7))); The number of Byte//5 ~ 6: follow-up
Values. The Add (0);//7: the Unit Identifier: This field is, informs the for intra - system routing purpose.
Values. The Add ((byte) FunctionCode. Write);//8. The Function Code: 16 (Write Multiple Register)
Values. AddRange (ValueHelper. Instance. GetBytes (StartingAddress));//9 ~ 10. The starting address
Values. AddRange (ValueHelper. Instance. GetBytes ((short) (data. Length/2)));//11 ~ 12. Register number
Values. The Add ((byte) data. Length);//13. The number of Byte data
//[2]. Added data
Values. The AddRange (data);//14 ~ End: need to send data
//[3]. Writing data
This. SocketWrapper. Write (values. ToArray ());
//[4]. To prevent the front desk by continuous, speaking, reading and writing the UI thread blocking
Application.doevents ();
//[5]. Read the Response: after finish will return the result of 12 byte
Byte [] responseHeader=this. SocketWrapper. Read (12);
}
# region IDisposable members
Public override void the Dispose ()
{
SocketWrapper. The Dispose ();
}
# endregion
}
}
CodePudding user response:
Urgent urgent, important, say 3 times, if the scores, I add,,,