예제 #1
0
        private void ComComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            var comboBox = (ComboBox)sender;

            // 需要关闭端口后设置
            serialPort = SerialPortHelper.GetSerialPort(comboBox.Text);

            // 数据接收回调
            SerialPortHelper.ParseReData = (byte[] data) =>
            {
                Console.WriteLine("解析数据!");
                controllerToRead.ParseDataToControllerItem(data);

                // 解析完成后,读取下一个控制器数据
                // ReadControllerData(serialPort, ++controllerToReadIndex);
            };

            // 同步时间
            byte[] txData = { 0x00,                                       0x00,                         0x10,                    0x01,                      0x07, (byte)(DateTime.Now.Year - 2000),
                              (byte)DateTime.Now.Month, (byte)DateTime.Now.Day, (byte)DateTime.Now.DayOfWeek, (byte)DateTime.Now.Hour, (byte)DateTime.Now.Minute,
                              (byte)DateTime.Now.Second };

            SerialPortHelper.SendData(serialPort, txData);

            Thread.Sleep(100);

            // 读取第一个控制器信息
            ReadControllerData(serialPort, 0);
        }