tx
2025-04-09 fa7510e1ed63df0366787fa4ed1b3db6426d2b46
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
 
//电气柜MODEBUS的寄存器(科迪)
namespace TProduct.Link.Debugger
{
    public partial class DebugMsg_KEDI_江大_Ctrl : UserControl, ILinkDebugger
    { 
        public DebugMsg_KEDI_江大_Ctrl()
        {
            InitializeComponent();
            spinEdit参数.Visible = label参数.Visible = false;
            button发送指令.Visible = false;
        }
 
        private void MsgBuild科迪测试台_Load(object sender, EventArgs e)
        {
            //设置当前电脑有哪个串口可用的Combox控件
            InitialDispContrl(comboBoxCOM);
        }
        public static bool InitialDispContrl(System.Windows.Forms.ComboBox ctrl)
        {
            ctrl.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
            if (ctrl.Items.Count == 0)
            {
                MessageBox.Show("没有COM端口");
                return false;
            }
            ctrl.SelectedIndex = 0;
            return true;
        }
        private SerialPort _seriesPort = null;
        public void DisposePort()
        {
            if (_seriesPort != null && _seriesPort.IsOpen)
            {
                _seriesPort.Close();
            }
        }
 
        //连接设备(下发命令) 
        private void btnConnPoll_Click(object sender, EventArgs e)
        {
            if (comboBoxType.SelectedIndex == 0)
            {// 
 
            }
            if (comboBoxType.SelectedIndex == 1)
            {//无纸数据记录仪
 
            }
            if (comboBoxType.SelectedIndex == 2)
            {//流量转速
                var commandSend =   TProduct.Link.Kedi.MsgItem_犇流_转速.BuildQueryMessage(1);
                textBoxSend.Text = BitConverter.ToString(commandSend, 0, commandSend.Length);
            }
            if (comboBoxType.SelectedIndex == 3)
            {//出口阀门
                var commandSend = TProduct.Link.Kedi.MsgItem_力夫_阀门.BuildMessage出口(
                    Convert.ToDouble(spinEdit参数.Value), 8);
                textBoxSend.Text = BitConverter.ToString(commandSend, 0, commandSend.Length);
            }
            if (comboBoxType.SelectedIndex == 4)
            {// 直流电源(开)
 
            }
            if (comboBoxType.SelectedIndex == 5)
            {// 直流电源(开)
 
            }
            if (comboBoxType.SelectedIndex == 6)
            {//进口阀门
                var commandSend = TProduct.Link.Kedi.MsgItem_力夫_阀门.BuildMessage进口(
                    Convert.ToDouble(spinEdit参数.Value), 8);
                textBoxSend.Text = BitConverter.ToString(commandSend, 0, commandSend.Length);
            }
            if (comboBoxType.SelectedIndex == 7)
            {//力夫用电参数仪
 
            }
            if (comboBoxType.SelectedIndex == 8)
            {//力夫温度控制器
 
            }
            if (comboBoxType.SelectedIndex == 9)
            {//力夫PLC控制启
 
            }
            if (comboBoxType.SelectedIndex == 10)
            {//力夫PLC控制停
 
            }
            if (comboBoxType.SelectedIndex == 11)
            {//江大扭矩仪
                var commandSend = TProduct.Link.Kedi.MsgItem_江大_扭矩仪.GetPollSendMessage(1);//1代表:本从机号X
                textBoxSend.Text = BitConverter.ToString(commandSend, 0, commandSend.Length);
            }
 
        }
 
        private void comboBoxType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxType.SelectedIndex == 0)
            {// 
 
            }
            else if (comboBoxType.SelectedIndex == 1)
            {//无纸数据记录仪
                spinEdit参数.Text = "8";
                label参数.Text = "端口总数";
                label参数.Location = new System.Drawing.Point(256, 12);
                spinEdit参数.Visible = label参数.Visible = true;
            }
            else if (comboBoxType.SelectedIndex == 2)
            {
                //spinEdit参数.Text = "1";
                //label参数.Text = "地址"; label参数.Location = new System.Drawing.Point(268, 12);
                //spinEdit参数.Visible = label参数.Visible = true;
            }
            else if (comboBoxType.SelectedIndex == 3)
            {//出口阀门
                label参数.Text = "开度"; 
                spinEdit参数.Visible = label参数.Visible = true;
            }
            else if (comboBoxType.SelectedIndex == 6)
            {//进口阀门
                label参数.Text = "开度";  
                spinEdit参数.Visible = label参数.Visible = true;
            }
            else if (comboBoxType.SelectedIndex == 4)
            {// 直流电源(开)
                label参数.Text = "电压";
                spinEdit参数.Visible = label参数.Visible = true;
            }
            else
            {
                spinEdit参数.Visible = label参数.Visible = false;
            }
        }
 
        private void btnClearText_Click(object sender, EventArgs e)
        {
            this.textBoxReceive.Text = "";
        }
 
 
        private void buttonAna_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.textBoxReceive.Text))
                return;
 
            System.Text.Encoding chs = System.Text.Encoding.GetEncoding("gb2312");
            string error_info = null;
            var byteMessage = StringToHexValuve(textBoxReceive.Text, out error_info);
 
            if (comboBoxType.SelectedIndex == 0)
            {// 
 
            }
            if (comboBoxType.SelectedIndex == 1)
            {// 无纸数据记录仪
  
                    double inelt_press_ma; double outlet_press_ma;
                    var error = new TProduct.Link.Kedi.SiMsg压力().AbstactByteValue(byteMessage.ToArray(),
                        out inelt_press_ma, out outlet_press_ma);
                    if (!string.IsNullOrEmpty(error))
                    {
                        MessageBox.Show(error);
                        return;
                    }
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("进口压力:{0} ma", inelt_press_ma); sb.AppendLine();
                    sb.AppendFormat("出口压力:{0} ma", outlet_press_ma); sb.AppendLine();
 
                    textBoxResult.Text = sb.ToString();
                 
            }
            if (comboBoxType.SelectedIndex == 2)
            {//转速              
                var info = TProduct.Link.Kedi.MsgItem_犇流_转速.GetReceiveMessageValues(byteMessage.ToArray() );
                textBoxResult.Text = info;
            }
        }
 
 
 
        /// <summary>
        /// 字符串转16进制字符2
        /// </summary>
        /// <param name="content">字符串</param>
        /// <param name="encode">编码格式</param>
        /// <returns></returns>
        private List<byte> StringToHexValuve(string content, out string error_info)
        {
            //去掉空格
            string[] arr = content.Split(' ');
            if (arr.Length < 3)
            {
                arr = content.Split('-');
                if (arr.Length < 0)
                {
                    error_info = "字符无法解析";
                    return null;
                }
            }
            List<byte> result = new List<byte>();
            for (int i = 0; i < arr.Length; i++)
            {
                if (string.IsNullOrWhiteSpace(arr[i]))
                    continue;
 
                var dddd = Convert.ToByte(arr[i], 16);
                result.Add(dddd);
            }
            error_info = null;
            return result;
        }
 
        private void btnOpenCom_Click(object sender, EventArgs e)
        {
            try
            {
                if (_seriesPort != null && _seriesPort.IsOpen)
                {
                    MessageBox.Show("串口已打开,请关闭后再打开");
                    return;
                }
                //
                //com.BaudRate = 115200;
                if (_seriesPort == null)
                {
                    _seriesPort = new SerialPort();
                    _seriesPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.OnReceiveMessage);
 
                }
 
                _seriesPort.BaudRate = 9600;// Convert.ToInt32(comboBoxbtl.Text);
                _seriesPort.PortName = comboBoxCOM.Text;
                _seriesPort.DataBits = 8;
                _seriesPort.Parity = Parity.None;
                _seriesPort.StopBits = StopBits.One;
                _seriesPort.ReceivedBytesThreshold = 1;
                if (comboBoxType.SelectedIndex == 9)
                {//力夫PLC控制启
                    _seriesPort.DataBits = 7;
                    _seriesPort.Parity = Parity.Even;
                }
                if (comboBoxType.SelectedIndex == 10)
                {//力夫PLC控制停
                    _seriesPort.DataBits = 7;
                    _seriesPort.Parity = Parity.Even;
                }
                //打开COM接口
                _seriesPort.Open();
 
                //
                Properties.Settings.Default.PortName = comboBoxCOM.Text;
 
                //!设置按钮
                button发送指令.Visible = true;
 
 
 
            }
            catch (Exception ex)
            {
                MessageBox.Show("串口信息提示:" + ex.Message, "信息提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
 
        private void button发送指令_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxSend.Text) || textBoxSend.Text.Length < 1)
                return;
            textBoxResult.Text = "";
            System.Text.Encoding chs = System.Text.Encoding.GetEncoding("gb2312");
            string error_info = null;
            var byteMessage = StringToHexValuve(textBoxSend.Text, out error_info);
            if (byteMessage == null)
                return;
 
            if (_seriesPort == null)
                return;
            try
            {
                //清除
                _seriesPort.DiscardOutBuffer();
                _seriesPort.DiscardInBuffer();
 
                //发送查询
                _seriesPort.Write(byteMessage.ToArray(), 0, byteMessage.Count);
            }
            catch (Exception err)
            {
                MessageBox.Show("Error in write event: " + err.Message);
                return;
            } 
        }
 
        bool isDisp0X = true;
        //接收消息(事件)
        private void OnReceiveMessage(object sender, SerialDataReceivedEventArgs e)
        {
            if (_seriesPort == null || !_seriesPort.IsOpen)
                return;
 
            //SerialPort sp = sender as SerialPort;
            Thread.Sleep(100);//暂停100毫秒等一条完整的命令进入缓冲区 
 
            //读取串口中的数据
            int count = _seriesPort.BytesToRead;
            if (count < 1)
            {
                string strError = string.Format("收到信号的数据量过少,字节长度只有{0}位\n ", count);
                this.textBoxReceive.Invoke(new EventHandler(delegate { textBoxReceive.AppendText(strError); }));
                //this.rtfTerminal1.AppendText(strError);
                return;
            }
 
            try
            {
                //读取信号
                byte[] byteMessage = new byte[count];
                _seriesPort.Read(byteMessage, 0, byteMessage.Length);
 
                string strMessage;
                if (isDisp0X)
                    strMessage = BitConverter.ToString(byteMessage, 0, byteMessage.Length);// byte[] -> 16进制字符串
                else
                    strMessage = Encoding.ASCII.GetString(byteMessage.ToArray());
 
                this.textBoxReceive.Invoke(new MethodInvoker(
                 delegate
                 {
                     this.textBoxReceive.AppendText(strMessage + "\n");
                 }));
            }
            catch (Exception err)
            {
                string strError = "Error in read event: " + err.Message + "\n";
                this.textBoxReceive.Invoke(new EventHandler(delegate { textBoxReceive.AppendText(strError); }));
                return;
            }
        }
 
 
    }
}