alielie
2025-10-10 15bbd62b0b24b72c717ae91ab45348a2083ad04e
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
using HMI.Core.Util;
using HMI.UI.Entity;
using HMI.UI.Util;
using HMI.Util;
using Panuon.WPF.UI;
using System;
using System.ComponentModel;
using System.IO.Ports;
using System.Windows;
 
namespace Plugin.TsExecution
{
    public class BarcodeEventArg
    {
        public string name { get; private set; }
        public string positionCode { get; private set; }
        public string snrCode { get; private set; }
 
        public BarcodeEventArg(string name, string positionCode, string snrCode)
        {
            this.name = name;
            this.positionCode = positionCode;
            this.snrCode = snrCode;
        }
    }
 
    /// <summary>
    /// Scanner.xaml 的交互逻辑
    /// </summary>
    public partial class Scanner : WindowX
    {
        private string _title;
 
        public string title
        {
            get { return _title; }
            set => Set(ref _title, value);
        }
 
        public event EventHandler<BarcodeEventArg> FinishBarcode;
        SerialPort serialPort;
        public Scanner(string title)
        {
            this.title = title;
            this.DataContext = this;
            InitializeComponent();
            Loaded += Scanner_Loaded;
        }
 
        private void Scanner_Loaded(object sender, RoutedEventArgs e)
        {
            InitScanner();
            Loaded -= Scanner_Loaded;
        }
 
        /// <summary>
        /// 设置扫码枪
        /// </summary>
        private void InitScanner()
        {
            string comStr = "";
            if (serialPort != null) return;
 
            string section = this.Tag.ToString();
            ScannerConfig common = ScannerConfig.ReadFromIni("", "Config");
 
            string portName = IniFileHelper.ReadIniData(section, "PortName", "","");
            if (string.IsNullOrEmpty(portName))
            {
                if (section.StartsWith("A"))
                {
                    portName = "COM1";
                }
                else
                {
                    portName = "COM2";
                }
            }
            common.PortName = portName;
 
 
            // 配置串口
            serialPort = new SerialPort
            {
                PortName = common.PortName,
                BaudRate = common.BaudRate,
                Parity = Parity.None,
                DataBits = common.DataBits,
                StopBits = common.StopBits,
                Handshake = Handshake.None,
                DtrEnable = common.DtrEnable,
                RtsEnable = common.RtsEnable
                //ReadTimeout = 500,
                //WriteTimeout = 500,
                //ReciveBufferSize=4096
            };
 
            try
            {
                // 打开串口
                serialPort.Open();
                // 注册接收数据事件
                serialPort.DataReceived += SerialPort_DataReceived;
            }
            catch (Exception ex)
            {
                MsgBoxHelper.Error(this, $"{this.lang("Tr.InitGunFailed")}: {ex.StackTrace}");
            }
        }
 
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                SerialPort sp = (SerialPort)sender;
                string barcode = ReadDataFrom(sp);
 
                //string barcode = 
                Dispatcher.Invoke(() =>
                {
                    if (snrText.IsFocused)
                    {
                        snrText.Text = barcode;
                        Con_Click(null, null);
                    }
                    else
                    {
                        positionText.Text = barcode;
                        //positionText.Focus();
                        //snrText.Text = "";
                        snrText.Focus();
                    }
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show($"{this.lang("Tr.GetGunData")}: {ex.StackTrace}");
            }
        }
 
        private void Con_Click(object sender, RoutedEventArgs e)
        {
            Dispatcher.Invoke(() =>
            {
                string position = positionText.Text, snr = snrText.Text;
                if (string.IsNullOrEmpty(position) || string.IsNullOrEmpty(snr))
                {
                    ToastHelper.Warning(this, this.lang("Tr.PleaseInputSnrs"));
                    return;
                }
                try
                {
                    FinishBarcode.Invoke(this, new BarcodeEventArg(Tag.ToString(), position, snr));
                    //positionText.Text = "";
                    //snrText.Text = "";
                    positionText.Focus();
                }
                catch (Exception ex)
                {
                    ToastHelper.Warning(this, ex.Message);
                }
            });
        }
 
        private void Ok_Click(object sender, RoutedEventArgs e)
        {
            string position = positionText.Text, snr = snrText.Text;
            if (string.IsNullOrEmpty(position) || string.IsNullOrEmpty(snr))
            {
                ToastHelper.Warning(this, this.lang("Tr.PleaseInputSnrs"));
                return;
            }
            try
            {
                FinishBarcode.Invoke(this, new BarcodeEventArg(Tag.ToString(), position, snr));
                this.Close();
            }
            catch (Exception ex)
            {
                ToastHelper.Warning(this, ex.Message);
            }
        }
 
        private string ReadDataFrom(SerialPort sp)
        {
            string barcode = "";
            while (sp.BytesToRead > 0)
            {
                byte[] buffer = new byte[sp.BytesToRead];
                sp.Read(buffer, 0, buffer.Length);
                barcode += System.Text.Encoding.UTF8.GetString(buffer);
                System.Threading.Thread.Sleep(50);
            }
            return barcode;
        }
 
        private void Window_Closing(object sender, CancelEventArgs e)
        {
            try
            {
                serialPort.Close();
                serialPort = null;
            }
            catch (Exception ex)
            {
            }
        }
    }
}