コード例 #1
0
        /// <summary>
        /// 开启串口
        /// </summary>
        /// <param name="comSetUP">串口参数</param>
        /// <returns></returns>
        public bool InitPort(Com_Struct comSetUP)
        {
            try
            {
                if (com.IsOpen)
                {
                    com.Close();
                }

                com.PortName = comSetUP.PortName;
                com.BaudRate = comSetUP.Baudrate;
                com.DataBits = comSetUP.ByteSize;
                com.Parity   = SsComm.StringToParity(comSetUP.Parity);
                com.StopBits = SsComm.StingToStopBits(comSetUP.StopBit);
                com.Open();
                if (com.IsOpen)
                {
                    com.DataReceived += new SerialDataReceivedEventHandler(DataReceived); //订阅委托
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #2
0
        private void InitSerialCom()
        {
            this.toolStripCmbPort.Items.AddRange(SsComm.AllCom());

            if (this.toolStripCmbPort.Items.Count > 0)
            {
                if (string.IsNullOrEmpty(configInfo.Port))
                {
                    this.toolStripCmbPort.SelectedIndex = 0;
                }
                else
                {
                    int index = this.toolStripCmbPort.FindString(configInfo.Port);
                    if (index != -1)
                    {
                        this.toolStripCmbPort.SelectedIndex = index;
                    }
                    else
                    {
                        this.toolStripCmbPort.SelectedIndex = 0;
                    }
                }
            }
            if (this.toolStripCmbBaud.Items.Count > 0)
            {
                if (string.IsNullOrEmpty(configInfo.Baud))
                {
                    this.toolStripCmbBaud.SelectedIndex = this.toolStripCmbBaud.FindString("115200");
                }
                else
                {
                    int index = this.toolStripCmbBaud.FindString(configInfo.Baud);
                    if (index != -1)
                    {
                        this.toolStripCmbBaud.SelectedIndex = index;
                    }
                    else
                    {
                        this.toolStripCmbBaud.SelectedIndex = 0;
                    }
                }
            }
        }
コード例 #3
0
 public SsCommApp()
 {
     com = new SsComm();
 }