Modbus板卡通讯
客户端动态检测DO
public class TcpClient
{
public Form1 Client_DO;
public TcpClient(Form1 parent)
{
Client_DO = parent;
}
IPAddress ip1 = IPAddress.Parse("192.168.1.75");
int connPort1 = 502;
System.Timers.Timer Draw_Timer;
public Socket ClientServer;
public Thread Thread_DO;
public IPAddress IP1
{
get {
return ip1; }
set {
ip1 = value; }
}
public int ConnPort1
{
get {
return connPort1; }
set {
connPort1 = value; }
}
public void ClientServerinit()
{
Thread_DO = new Thread(Refresh_State_DO);
Thread_DO.IsBackground = true;
Thread_DO.Start();
Draw_Timer = new System.Timers.Timer(900);
Draw_Timer.Elapsed += new System.Timers.ElapsedEventHandler(clear);
Draw_Timer.AutoReset = true;
Draw_Timer.Enabled = true;
Thread.Sleep(500);
IPAddress ip1 = IPAddress.Parse("192.168.1.75");
int connPort1 = 502;
ClientServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ClientServer.Connect(new IPEndPoint(ip1, connPort1));
Console.WriteLine("连接服务器成功");
}
public string ToHexString(int length, byte[] bytes)
{
string hexString = string.Empty;
if (bytes != null)
{
StringBuilder strB = new StringBuilder();
for (int i = 0; i < length; i++)
{
for (int j = 0; j < 1; j++)
{
strB.Append(bytes[i].ToString("X2") + " ");
}
}
hexString = strB.ToString();
}
return hexString;
}
private int[] DO_State_Box = new int[16];
byte[] CheckData_DO = new byte[12] {
0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x01, 0x00, 0x00, 0x00, 0x10 };
public void clear(object source, System.Timers.ElapsedEventArgs e)
{
Client_DO.richTextBox1.Invoke(new Action(()