Exemplo n.º 1
0
 //连接设备
 private void button1_Click(object sender, EventArgs e)
 {
     //bool bOK = false;
     if (button1.Text == "连接设备")//启动串口扫描,连接设备
     {
         Rx1616 = new CP1616Packet(1, 1);
         string[] ports = SerialPort.GetPortNames();
         PortName           = "";
         progressBar1.Value = 0;
         int perCount, per;
         perCount = ports.Length * 3;
         per      = 100 / perCount;
         foreach (string com in ports)
         {
             for (int i = 0; i < 3; i++)
             {
                 progressBar1.Value += per;
                 try
                 {
                     COMPort1.Close();
                     COMPort1.PortName = com;
                     COMPort1.Open();
                     byte[] tx = CP1616Packet.MakeCP1616Packet(1, 1, 1);
                     try
                     {
                         COMPort1.Write(tx, 0, tx.Length);
                     }
                     catch { continue; }
                     while (true)
                     {
                         try
                         {
                             if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                             {
                                 DeviceID           = BytesOP.MakeShort(Rx1616.Data[0], Rx1616.Data[1]);
                                 label3.Text        = "设备ID:" + DeviceID.ToString();
                                 label2.Text        = com;//.Show(com);
                                 PortName           = com;
                                 progressBar1.Value = 100;
                                 button1.Text       = "断开连接";
                                 enBT(true);
                                 //bOK = true;
                                 break;
                             }
                         }
                         catch
                         { break; }
                     }
                     if (PortName != "")
                     {
                         break;
                     }
                     COMPort1.Close();
                     Thread.Sleep(100);
                 }
                 catch
                 {
                     break;
                 }
             }
             if (PortName != "")
             {
                 break;
             }
         }
         if (PortName == "")
         {
             progressBar1.Value = 100;
             MessageBox.Show("未找到设备");
         }
     }
     else//已经连接,则断开
     {
         Rx1616 = new CP1616Packet(2, 1);
         byte[] tx = CP1616Packet.MakeCP1616Packet(2, 1, 1);
         try
         {
             COMPort1.Write(tx, 0, tx.Length);
         }
         catch
         {
             PortName           = "";
             progressBar1.Value = 100;
             button1.Text       = "连接设备";
             label3.Text        = "";
             enBT(false);
             button1.Enabled = true;
             COMPort1.Close();
         }
         while (true)
         {
             try
             {
                 if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                 {
                     PortName           = "";
                     progressBar1.Value = 100;
                     button1.Text       = "连接设备";
                     enBT(false);
                     button1.Enabled = true;
                     label3.Text     = "";
                     break;
                 }
             }
             catch
             { break; }
         }
         COMPort1.Close();
     }
 }
Exemplo n.º 2
0
        private void button1_Click(object sender, EventArgs e)
        {
            Rx1616 = new CP1616Packet(100, 1);
            string[] ports = SerialPort.GetPortNames();
            PortName           = "";
            progressBar1.Value = 0;
            int perCount, per;

            perCount = ports.Length * 3;
            per      = 100 / perCount;
            bool bOK = false;

            foreach (string com in ports)
            {
                COMPort1.PortName = com;
                for (int i = 0; i < 3; i++)
                {
                    progressBar1.Value += per;
                    try
                    {
                        COMPort1.Open();
                        byte[] deviceID = new byte[2];
                        deviceID[0] = BytesOP.GetHighByte((ushort)numericUpDown1.Value);
                        deviceID[1] = BytesOP.GetLowByte((ushort)numericUpDown1.Value);
                        byte[] tx = CP1616Packet.MakeCP1616Packet(100, 1, deviceID);
                        try
                        {
                            COMPort1.Write(tx, 0, tx.Length);
                        }
                        catch { continue; }
                        while (true)
                        {
                            try
                            {
                                if (Rx1616.DataPacketed((byte)COMPort1.ReadByte()))
                                {
                                    MessageBox.Show("设定成功");
                                    bOK = true;
                                    break;
                                }
                            }
                            catch
                            { break; }
                        }
                        if (bOK)
                        {
                            break;
                        }
                        COMPort1.Close();
                        Thread.Sleep(100);
                    }
                    catch
                    {
                        break;
                    }
                }
                if (bOK)
                {
                    break;
                }
            }
            if (!bOK)
            {
                progressBar1.Value = 100;
                MessageBox.Show("未找到设备");
            }
            COMPort1.Close();
        }