wenxiangchao
4 天以前 559890c482f5973f06aafdfec9550fee5ba3c7f2
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
using HMI.UI.Entity;
using System.IO.Ports;
using System.Windows.Controls;
 
namespace Plugin.StandardTest
{
    public class ScannerManager
    {
        public static Dictionary<string, SerialPort> map = new Dictionary<string, SerialPort>();
        public static Dictionary<SerialPort, Queue<TextBox>> controlMap = new Dictionary<SerialPort, Queue<TextBox>>();
 
        public static Dictionary<SerialPort, TextBox> focusMap = new Dictionary<SerialPort, TextBox>();
        public static Dictionary<TextBox, SerialPort> reverseMap = new Dictionary<TextBox, SerialPort>();
 
 
        public static void RegisterCom(ScannerConfig setting, TextBox tb)
        {
            if (!map.ContainsKey(setting.PortName))
            {
                // 配置串口
                var serialPort = new SerialPort
                {
                    PortName = setting.PortName,
                    BaudRate = setting.BaudRate,
                    Parity = Parity.None,
                    DataBits = setting.DataBits,
                    StopBits = setting.StopBits,
                    Handshake = Handshake.None,
                    DtrEnable = setting.DtrEnable,
                    RtsEnable = setting.RtsEnable
                    //ReadTimeout = 500,
                    //WriteTimeout = 500,
                    //ReciveBufferSize=4096
                };
 
                try
                {
                    // 打开串口
                    serialPort.Open();
                    map[setting.PortName] = serialPort;
                    controlMap[serialPort] = new Queue<TextBox>();
                    reverseMap[tb] = serialPort;
                    tb.GotFocus += Tb_GotFocus;
                    controlMap[serialPort].Enqueue(tb);
 
                    // 注册接收数据事件
                    serialPort.DataReceived += SerialPort_DataReceived; ;
                }
                catch (Exception ex)
                {
 
                }
            }
            else { 
                
            }
        }
 
        private static 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 static void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            var sp = sender as SerialPort;
            string barcode = ReadDataFrom(sp);
            TextBox tb;
            if (focusMap.ContainsKey(sp))
            {
                tb = focusMap[sp];
            }
            else
            {
                tb = controlMap[sp].Dequeue();
            }
            //string barcode = 
            tb.Dispatcher.Invoke(() =>
                {
                    tb.Text = barcode;
                });
        }
 
        private static void Tb_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            var tb = sender as TextBox;
            SerialPort sp = reverseMap[tb];
            focusMap[sp] = tb;
        }
    }
}