alielie
2026-06-15 f08871fe7d148d667f2f11f80f64de71a0d9504c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using HMI.Util.Base;
using ModBusTcp;
using System;
using System.Text;
 
namespace ConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string msg;
            ModBusTcpController controller = new ModBusTcpController("127.0.0.1", 502);
            if (controller.Start(out msg))
            {
                bool res;
                int? target;
 
                controller.WriteBool("1", true, out msg);
                controller.ReadDOBool("1", out res, out msg);
                Console.WriteLine(res);
                controller.ReadDOInt("1", out target, out msg);
                Console.WriteLine(target);
                controller.WriteInt("1", 1, out msg);
                controller.ReadDOInt("1", out target, out msg);
                Console.WriteLine(target);
            }
            controller.Close();
 
            //AlarmUtil.GetProcessIdByName("TestPlatform");
 
            /// ModbusTcp
            //IDIOController controller = DIOControllerCreator
            //    .CreateController("C:\\Workspace\\VisualStudio\\WPFProject\\SimpleUtil\\DIOController\\bin\\Release\\modbus.ini");
            //bool s;
            //controller.Start(out string msg);
            //controller.ReadDIBool("0", out bool res, out msg);
            //controller.ReadDOBool("0", out res, out msg);
            //controller.WriteBool("0", true, out msg);
 
            //s = controller.WriteBool("100", new bool[] { false, false }, out msg);
            //s = controller.ReadDOBool("100", 2, out bool[] ress, out msg);
 
            //controller.Dispose();
 
            ///// 7432
            //controller = controller = DIOControllerCreator
            //    .CreateController("C:\\Workspace\\VisualStudio\\WPFProject\\SimpleUtil\\DIOController\\bin\\Release\\7432.ini");
            //controller.Start(out msg);
            //controller.ReadDIBool("0", out res, out msg);
            //controller.ReadDOBool("0", out res, out msg);
            //controller.WriteBool("0", true, out msg);
            //controller.Dispose();
            //string recvstr = "62 FD 82 39 39 39 37 32 32 36 36 30 38 34 33 39 35 33 38 53 38 44 41 30 30 32 32";
            //string recvData;
            //if (recvstr.Contains("FF"))
            //{
            //    recvData = recvstr.Replace(" ", "").Substring(6);
            //}
            //else
            //{
            //    byte[] bus = HexToByteArray(recvstr.Replace(" ", "").Substring(6));
            //    recvData = Encoding.Default.GetString(bus);
            //}
        }
 
        public static byte[] HexToByteArray(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            byte[] buffer = new byte[hexString.Length / 2];
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            }
            return buffer;
        }
    }
}