コード例 #1
0
ファイル: Program.cs プロジェクト: CKernal/ComDeviceControl
        static void Main(string[] args)
        {
            IComDevice comDevice = new ComDevice();

            comDevice.SerialDevice = new SerialPort
            {
                PortName = "COM1",
                BaudRate = 9600,
                Parity   = Parity.None,
                DataBits = 8,
                StopBits = StopBits.One,
            };

            comDevice.Protocol = new ComDeviceProtocol
            {
                StartCommand    = new byte[] { 0x02, 0x55, 0x0d, 0x0a },
                StopCommand     = new byte[] { 0x02, 0x54, 0x0d, 0x0a },
                ErrorString     = "error",
                ReceiveDataSize = 20,
                StartString     = "S",
                EndString       = "E",
                DataStartIndex  = 1,
                DataLength      = 10,
                TimeOut         = 1000 * 10,
            };


            if (comDevice.Open())
            {
                Console.WriteLine("Open sucessful.");
                while (true)
                {
                    Console.WriteLine(DateTime.Now.ToString() + " => Start...");
                    var ret = comDevice.GetData();

                    if (ret.Item1)
                    {
                        Console.WriteLine("Recived OK :{0}", ret.Item2);
                    }
                    else
                    {
                        Console.WriteLine("Recived NG :{0}", ret.Item2);
                    }

                    System.Threading.Thread.Sleep(1);
                }
            }
            else
            {
                Console.WriteLine("Open failed.");
            }


            Console.ReadKey();
        }
コード例 #2
0
ファイル: MyActiveXControl.cs プロジェクト: rench/WebCom
        public String ComOpen(bool b)
        {
            String str = b?"打开串口成功":"关闭串口成功";

            try
            {
                if (b)
                {
                    timerAutoClose.Enabled = true;
                    timerCount             = 4;
                    if (!ComDevice.IsOpen)
                    {
                        ComDevice.PortName = cbbComList.Text.ToString();
                        ComDevice.BaudRate = Convert.ToInt32(cbbBaudRate.Text.ToString());
                        ComDevice.Parity   = (Parity)Convert.ToInt32(cbbParity.SelectedIndex.ToString());
                        ComDevice.DataBits = Convert.ToInt32(cbbDataBits.Text.ToString());
                        ComDevice.StopBits = (StopBits)Convert.ToInt32(cbbStopBits.Text.ToString());
                        ComDevice.Open();
                    }
                }
                else
                {
                    if (ComDevice.IsOpen)
                    {
                        ComDevice.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //AddContent("串口打开错误:" + ex.Message + "\r\n");
                str = "错误:" + ex.Message + "\r\n";
            }
            finally
            {
                if (ComDevice.IsOpen == false)
                {
                    btnSend.Enabled = false;
                    btnOpen.Text    = "打开串口";
                    btnOpen.Image   = ZWebCom.Properties.Resources.close;
                    Log.Text        = "串口已关闭";
                }
                else
                {
                    btnSend.Enabled = true;
                    btnOpen.Text    = "关闭串口";
                    btnOpen.Image   = ZWebCom.Properties.Resources.open;
                    // 串口号,波特率,数据位,停止位.校验位
                    Log.Text = "串口已开启:" + cbbComList.Text + "," + cbbBaudRate.Text + "," + cbbDataBits.Text + "," + cbbStopBits.Text + "," + cbbParity.Text;
                }
            }
            return(str);
        }
コード例 #3
0
ファイル: ZUART.cs プロジェクト: xjq0928/ZUART
        private void btnOpen_Click(object sender, EventArgs e)
        {
            if (cbbComList.Items.Count <= 0)
            {
                //MessageBox.Show("没有发现串口,请检查线路!");
                Log.Text = "没有发现串口,请检查线路!";
                return;
            }

            try
            {
                //if (ComDevice.IsOpen == false)
                if (btnOpen.Text == "打开串口")
                {
                    ComDevice.PortName = cbbComList.Text.ToString();
                    ComDevice.BaudRate = Convert.ToInt32(cbbBaudRate.Text.ToString());
                    ComDevice.Parity   = (Parity)Convert.ToInt32(cbbParity.SelectedIndex.ToString());
                    ComDevice.DataBits = Convert.ToInt32(cbbDataBits.Text.ToString());
                    ComDevice.StopBits = (StopBits)Convert.ToInt32(cbbStopBits.Text.ToString());

                    ComDevice.Open();
                }
                else
                {
                    ComDevice.Close();
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                AddContent("串口打开错误:" + ex.Message + "\r\n");
            }
            finally
            {
                if (ComDevice.IsOpen == false)
                {
                    btnSend.Enabled   = false;
                    btnOpen.Text      = "打开串口";
                    btnOpen.Image     = Properties.Resources.close;
                    Log.Text          = "串口已关闭";
                    timerIcon.Enabled = false;
                    this.Icon         = Properties.Resources.zuart;
                }
                else
                {
                    btnSend.Enabled = true;
                    btnOpen.Text    = "关闭串口";
                    btnOpen.Image   = Properties.Resources.open;
                    // 串口号,波特率,数据位,停止位.校验位
                    Log.Text          = "串口已开启:" + cbbComList.Text + "," + cbbBaudRate.Text + "," + cbbDataBits.Text + "," + cbbStopBits.Text + "," + cbbParity.Text;
                    timerIcon.Enabled = true;
                }
            }
        }
コード例 #4
0
 /// <summary>
 /// 打开串口
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void Btn_Open_Click(object sender, EventArgs e)
 {
     if (ComDevice.IsOpen == false)
     {
         try
         {
             string[] portList = SerialPort.GetPortNames();
             if (portList.Length > 0)
             {
                 ComDevice.PortName = portList[0];
             }
             //打开串口
             ComDevice.Open();
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
             return;
         }
         Btn_Open.Text      = "关闭串口";
         Lab_Flag.BackColor = Color.Green;   //标志显示绿色,表示串口已成功打开
     }
     else
     {
         try
         {
             ComDevice.Close();
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
         Btn_Open.Text      = "打开串口";
         Lab_Flag.BackColor = Color.Red;     //标识显示红色,表示串口未打开
     }
 }
コード例 #5
0
        private void btnOpen_Click_1(object sender, EventArgs e)
        {
            if (ComDevice.IsOpen == false)
            {
                ComDevice.PortName = cbbComList.Text;// cbbComList.SelectedItem.ToString();
                ComDevice.BaudRate = Convert.ToInt32(cbbbaut.Text);

                if (ccbEO.Text == "无")
                {
                    ComDevice.Parity = Parity.None; //(Parity)Convert.ToInt32(cbbParity.SelectedIndex.ToString());
                }
                else if (ccbEO.Text == "偶")
                {
                    ComDevice.Parity = Parity.Even;
                }
                else if (ccbEO.Text == "奇")
                {
                    ComDevice.Parity = Parity.Odd;
                }
                else if (ccbEO.Text == "0")
                {
                    ComDevice.Parity = Parity.Space;
                }
                else
                {
                    ComDevice.Parity = Parity.Mark;
                }


                ComDevice.DataBits = Convert.ToInt16(ccbDb.Text);//Convert.ToInt32(cbbDataBits.SelectedItem.ToString());

                if (cbbStp.Text == "1")
                {
                    ComDevice.StopBits = StopBits.One;// 1;//(StopBits)Convert.ToInt32(cbbStopBits.SelectedItem.ToString());
                }
                else if (cbbStp.Text == "1.5")
                {
                    ComDevice.StopBits = StopBits.OnePointFive;
                }
                else if (cbbStp.Text == "2")
                {
                    ComDevice.StopBits = StopBits.Two;
                }
                else
                {
                    ComDevice.StopBits = StopBits.None;
                }

                try
                {
                    ComDevice.Open();

                    btnSnd.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                btnOpen.Text = "关闭串口";
                // pictureBox1.BackgroundImage = Properties.Resources.green;
            }
            else
            {
                try
                {
                    ComDevice.Close();
                    btnSnd.Enabled = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                btnOpen.Text = "打开串口";
                //pictureBox1.BackgroundImage = Properties.Resources.red;
            }
        }