Esempio n. 1
0
 public void Init_SerialPort(
     ref System.IO.Ports.SerialPort serPort,
     System.IO.Ports.SerialDataReceivedEventHandler recFunction,
     int baudRate,
     string comName,
     System.IO.Ports.Parity parity,
     System.IO.Ports.StopBits stopBits,
     int dataBits)
 {
     serPort          = new System.IO.Ports.SerialPort();
     serPort.BaudRate = baudRate;
     serPort.PortName = comName;
     serPort.Parity   = parity;
     serPort.StopBits = stopBits;
     serPort.DataBits = dataBits;
     try
     {
         serPort.Open();
         serPort.DataReceived += recFunction;
         ErroMsg    = string.Empty;
         IsPortOpen = true;
     }
     catch
     {
         ErroMsg = "端口 " + serPort.PortName + " 不能打开!";
     }
 }
Esempio n. 2
0
        private void scan_com_port()
        {
            int baud = 19200;

            System.IO.Ports.Parity   parity   = System.IO.Ports.Parity.None;
            System.IO.Ports.StopBits stopbits = System.IO.Ports.StopBits.One;
            int databits = 8;

            //System.IO.Ports.Handshake handshake = System.IO.Ports.Handshake.None;

            for (int ii = 1; ii <= 256; ii++)
            {
                try
                {
                    sp = new System.IO.Ports.SerialPort("COM" + ii.ToString(), baud, parity, databits, stopbits);
                    sp.Open();
                    if (sp.IsOpen)
                    {
                        CanBusPortNumberCmbx.Items.Add(ii.ToString());
                    }
                }
                catch (Exception)  //開埠不成功就會到這來
                {
                    // 執行開埠不成功的處理
                }
                finally
                {
                    sp.Close();
                }
            }
            CanBusPortNumberCmbx.SelectedIndex = 0;
        }
Esempio n. 3
0
        public static string  GetConvertParity(System.IO.Ports.Parity mParity)
        {
            try
            {
                string strParity = "";

                switch (mParity)
                {
                case System.IO.Ports.Parity.Even:
                    strParity = "Even";
                    break;

                case System.IO.Ports.Parity.Odd:
                    strParity = "Odd";
                    break;

                case System.IO.Ports.Parity.Mark:
                    strParity = "Mark";
                    break;

                case System.IO.Ports.Parity.Space:
                    strParity = "Space";
                    break;
                }

                return(strParity);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 4
0
        public static System.IO.Ports.Parity GetConvertParity(string parity)
        {
            try
            {
                System.IO.Ports.Parity mParity = System.IO.Ports.Parity.None;

                switch (parity)
                {
                case "Even":
                    mParity = System.IO.Ports.Parity.Even;
                    break;

                case "Odd":
                    mParity = System.IO.Ports.Parity.Odd;
                    break;

                case "Mark":
                    mParity = System.IO.Ports.Parity.Mark;
                    break;

                case "Space":
                    mParity = System.IO.Ports.Parity.Space;
                    break;
                }

                return(mParity);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 5
0
        private void LoadDefaultData()
        {
            //讀取BaudRate;//
            int BaudRate = Properties.Settings.Default.BaudRate;

            cbBaudRate.SetSelectItem(BaudRate);

            //讀取DataBit;//
            int DataBit = Properties.Settings.Default.DataBits;

            cbDataBits.SetSelectItem(DataBit);

            //讀取Parity;//
            System.IO.Ports.Parity parity = Properties.Settings.Default.Parity;
            cbParity.SelectedIndex = (int)parity;

            //讀取StopBit;//
            System.IO.Ports.StopBits stopBit = Properties.Settings.Default.StopBits;
            cbStopBit.SelectedIndex = (int)stopBit;

            string[] comPortList = Uart.Instance.GetPortNames();
            for (int i = 0; i < comPortList.Count(); i++)
            {
                if (comPortList[i].Equals(Properties.Settings.Default.Com))
                {
                    cbSerial.SelectedIndex = i;
                    break;
                }
            }
        }
Esempio n. 6
0
        //-------------------------------------------------------------------------------
        /// <summary>
        /// Обработчик события выбора паритета
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxParity_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cb = sender as ComboBox;

            _Parity = (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity),
                                                         (String)cb.SelectedItem);
            return;
        }
Esempio n. 7
0
 public void Init_BalancePort(
     int baudRate,
     string comName,
     System.IO.Ports.Parity parity,     //指定奇偶校验位
     System.IO.Ports.StopBits stopBits, //停止位
     int dataBits)                      //数据位数
 {
     Init_SerialPort(ref _Balance_Port, Balance_DataReceived, baudRate, comName, parity, stopBits, dataBits);
 }
Esempio n. 8
0
 public Profile(string name, int baudRate, System.IO.Ports.Parity parity, int dataBits, System.IO.Ports.StopBits stopBits, string defaultPath, string defaultPort)
 {
     this.name        = name;
     this.baudRate    = baudRate;
     this.parity      = parity;
     this.dataBits    = dataBits;
     this.stopBits    = stopBits;
     this.defaultPath = defaultPath;
     this.defaultPort = defaultPort;
 }
Esempio n. 9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="_serialportname"></param>
        /// <param name="_baudrate"></param>
        /// <param name="_databits"></param>
        /// <param name="_parity"></param>
        /// <param name="_stopbits"></param>
        public SmartHomeDevice(string _serialportname, string _baudrate, string _databits, string _parity, string _stopbits)
        {
            this.serialportname = _serialportname;
            this.baudrate       = _baudrate;
            this.databits       = _databits;
            this.parity         = myConverter.FromStringToSerialParity(_parity);
            this.stopbits       = myConverter.FromStringToSerialStopBits(_stopbits);

            IsConnected = false;
        }
Esempio n. 10
0
        /// <summary>
        /// Overload Constructor
        /// </summary>
        /// <param name="SerialPortNumber"></param>
        /// <param name="SerialBaudRate"></param>
        /// <param name="SerialDataBits"></param>
        /// <param name="SerialParity"></param>
        /// <param name="SerialStopBits"></param>
        public clsSLM(int SerialPortNumber, long SerialBaudRate, System.IO.Ports.Parity SerialParity, int SerialDataBits, System.IO.Ports.StopBits SerialStopBits)
        {
            m_iCommPortNumber = SerialPortNumber;
            m_lCommBaudRate   = SerialBaudRate;
            m_pCommParity     = SerialParity;
            m_iCommDataBits   = SerialDataBits;
            m_sCommStopBits   = SerialStopBits;

            m_serport = new System.IO.Ports.SerialPort();
        }
Esempio n. 11
0
 public Modbus(string PName, int PBaud, System.IO.Ports.Parity parity, int timeout)
 {
     _PortName = PName;
     _PortBaud = PBaud;
     _Timeout = timeout;
     _parity = parity;
     SP.PortName = _PortName;
     SP.PortBaud = _PortBaud;
     SP.Timeout = timeout;
     SP.Parity = parity;
 }
Esempio n. 12
0
        /// <summary>
        /// Overload Constructor
        /// </summary>
        /// <param name="SerialPortNumber"></param>
        /// <param name="SerialBaudRate"></param>
        /// <param name="SerialDataBits"></param>
        /// <param name="SerialParity"></param>
        /// <param name="SerialStopBits"></param>
        public clsDiagnostics(int SerialPortNumber, long SerialBaudRate, System.IO.Ports.Parity SerialParity, int SerialDataBits, System.IO.Ports.StopBits SerialStopBits)
        {
            m_iCommPortNumber = SerialPortNumber;
            m_lCommBaudRate   = SerialBaudRate;
            m_pCommParity     = SerialParity;
            m_iCommDataBits   = SerialDataBits;
            m_sCommStopBits   = SerialStopBits;

            m_serport = new System.IO.Ports.SerialPort();
            // m_tTerminalWindow = new System.Windows.Forms.RichTextBox();
        }
Esempio n. 13
0
        private void sendSerialData(string p_strCOMPort, int p_intBaudRate, System.IO.Ports.Parity p_objParitySetting, int p_intDataBits, System.IO.Ports.StopBits p_objStopBits, string p_strDataToSend)
        {
            System.IO.Ports.SerialPort l_objLiftSerialPort = new System.IO.Ports.SerialPort(p_strCOMPort, p_intBaudRate, p_objParitySetting, p_intDataBits, p_objStopBits);

            try
            {
                l_objLiftSerialPort.Open();
                l_objLiftSerialPort.WriteLine(p_strDataToSend);
            }
            catch (Exception e) { throw e; }
            finally { l_objLiftSerialPort.Close(); }
        }
Esempio n. 14
0
        private void sendSerialData(string p_strCOMPort, int p_intBaudRate, System.IO.Ports.Parity p_objParitySetting, int p_intDataBits, System.IO.Ports.StopBits p_objStopBits, byte[] p_bteDataToSend)
        {
            System.IO.Ports.SerialPort l_objSwitcherSerialPort = new System.IO.Ports.SerialPort(p_strCOMPort, p_intBaudRate, p_objParitySetting, p_intDataBits, p_objStopBits);

            try
            {
                //Since the manual had a hex message, I tried to follow an example found here: https://web.archive.org/web/20130709121945/http://msmvps.com/blogs/coad/archive/2005/03/23/SerialPort-_2800_RS_2D00_232-Serial-COM-Port_2900_-in-C_2300_-.NET.aspx
                //If this does not work, try sending the text values instead (like the projector lift)
                l_objSwitcherSerialPort.Open();
                l_objSwitcherSerialPort.Write(p_bteDataToSend, 0, 10);
            }
            catch (Exception e) { throw e; }
            finally { l_objSwitcherSerialPort.Close(); }
        }
        public SerialPort(string ConfigString, string FriendlyName) : base(ConfigString, FriendlyName)
        {
            string Port     = Config["Port"];
            int    BaudRate = int.Parse(Config["BaudRate"]);

            System.IO.Ports.Parity Parity = (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity), Config["Parity"]);
            int DataBits = int.Parse(Config["DataBits"]);

            System.IO.Ports.StopBits StopBits = (System.IO.Ports.StopBits)Enum.Parse(typeof(System.IO.Ports.StopBits), Config["StopBits"]);

            if (FriendlyName == null || FriendlyName.Equals(string.Empty))
            {
                friendly_name = Port;
            }
            port = new System.IO.Ports.SerialPort(Port, BaudRate, Parity, DataBits, StopBits);
        }
Esempio n. 16
0
        /// <summary>
        /// Load all profiles from xml file.
        /// https://stackoverflow.com/questions/4835891/extract-value-of-attribute-node-via-xpath
        /// </summary>
        /// <param name="xmlSource">xml source</param>
        private void Load(ref XmlDocument xmlSource)
        {
            XmlNodeList profileNodes = xmlSource.SelectNodes("*/profile");

            foreach (XmlNode profile in profileNodes)
            {
                string name = profile.Attributes[0].Value;
                int    baud = Convert.ToInt32(profile.Attributes[1].Value);
                System.IO.Ports.Parity parity = ParityStringToEnum(profile.Attributes[2].Value);
                int dBits = Convert.ToInt32(profile.Attributes[3].Value);
                System.IO.Ports.StopBits sBits = StopBitsStringToEnum(profile.Attributes[4].Value);
                string path = profile.Attributes[5].Value;
                string port = profile.Attributes[6].Value;

                Add(new Profile(name, baud, parity, dBits, sBits, path, port));
            }
        }
Esempio n. 17
0
 public void Connect(string portName, int baudrate, int databit, System.IO.Ports.Parity parity, System.IO.Ports.StopBits stopbits)
 {
     try
     {
         if (serial == null)
         {
             serial = new ComConnect(portName, baudrate, databit, parity, stopbits, "CRLF");
             serial.OpenSerialPort();
             serial.PortDateReceiveEvent -= serial_PortDateReceiveEvent;
             serial.PortDateReceiveEvent += serial_PortDateReceiveEvent;
             ReadIoHandle.Start();
         }
     }
     catch (Exception ex)
     {
         throw new Exception("打开PLC串口异常:" + ex.Message);
     }
 }
Esempio n. 18
0
        /// <summary>
        /// 取得校验描述
        /// </summary>
        /// <param name="parity"></param>
        /// <returns></returns>
        protected string GetParityDesc(System.IO.Ports.Parity parity)
        {
            string strDesc = "未知校验";

            switch (parity)
            {
            case System.IO.Ports.Parity.None:
                strDesc = "校验无";
                break;

            case System.IO.Ports.Parity.Even:
                strDesc = "偶校验";
                break;

            case System.IO.Ports.Parity.Odd:
                strDesc = "奇校验";
                break;
            }
            return(strDesc);
        }
Esempio n. 19
0
 /// <summary>
 /// SerialPortHelper Initialization
 /// </summary>
 /// <param name="com"></param>
 /// <param name="BaudRate"></param>
 /// <param name="DataBits"></param>
 /// <param name="StopBIts"></param>
 /// <param name="Parity"></param>
 public bool Load(string com, int BaudRate          = 9600, int DataBits = 8,
                  System.IO.Ports.StopBits StopBIts = System.IO.Ports.StopBits.One,
                  System.IO.Ports.Parity Parity     = System.IO.Ports.Parity.None)
 {
     SerialPort.PortName = com;
     SerialPort.BaudRate = BaudRate;
     SerialPort.DataBits = DataBits;
     SerialPort.StopBits = StopBIts;
     SerialPort.Parity   = Parity;
     Timer.Start();
     try {
         SerialPort.Open();
         return(true);
     } catch {
         _SerialPort.DataReceived -= _SerialPort_DataReceived;
         _Timer.Elapsed           -= _Timer_Elapsed;
         SerialPort.Dispose();
         Timer.Dispose();
         return(false);
     }
 }
Esempio n. 20
0
        public static System.IO.Ports.Parity GetParity(this string args)
        {
            System.IO.Ports.Parity r = System.IO.Ports.Parity.None;

            switch (args.ToUpper())
            {
            case "NONE": r = System.IO.Ports.Parity.None; break;

            case "ODD": r = System.IO.Ports.Parity.Odd; break;

            case "EVEN": r = System.IO.Ports.Parity.Even; break;

            case "MARK": r = System.IO.Ports.Parity.Mark; break;

            case "SPACE": r = System.IO.Ports.Parity.Space; break;

            default:
                throw new Exception("值不在 Parity 枚举内。(args = {0})".FormatWith(args));
            }

            return(r);
        }
Esempio n. 21
0
        public void open()
        {
            string com = con.ck_com;
            int    btl = int.Parse(con.tx_Baud);
            int    sjc = int.Parse(con.tx_sjc);

            System.IO.Ports.Parity pr = new System.IO.Ports.Parity();
            if (con.tx_jiaoyanwei == "无")
            {
                pr = System.IO.Ports.Parity.None;
            }
            else if (con.tx_jiaoyanwei == "奇")
            {
                pr = System.IO.Ports.Parity.Odd;
            }
            else if (con.tx_jiaoyanwei == "偶")
            {
                pr = System.IO.Ports.Parity.Even;
            }
            System.IO.Ports.StopBits jyw = new System.IO.Ports.StopBits();
            if (con.tx_stop == "1")
            {
                jyw = System.IO.Ports.StopBits.One;
            }
            else if (con.tx_stop == "2")
            {
                jyw = System.IO.Ports.StopBits.Two;
            }
            else
            {
                jyw = System.IO.Ports.StopBits.None;
            }
            int outitme = int.Parse(con.tx_outtime);

            modbus.openport_RUN(com, btl, sjc, pr, jyw, outitme, out cg);

            modbus2.openport_Ascii("COM3", 19200, 7, System.IO.Ports.Parity.Even, System.IO.Ports.StopBits.One, out cg);
        }
Esempio n. 22
0
 private void SelectParity(System.IO.Ports.Parity parity)
 {
     if (parity == System.IO.Ports.Parity.Even)
     {
         lvParity.SelectedItem = lvParity.GetItems()[0];
     }
     else if (parity == System.IO.Ports.Parity.Mark)
     {
         lvParity.SelectedItem = lvParity.GetItems()[1];
     }
     else if (parity == System.IO.Ports.Parity.None)
     {
         lvParity.SelectedItem = lvParity.GetItems()[2];
     }
     else if (parity == System.IO.Ports.Parity.Odd)
     {
         lvParity.SelectedItem = lvParity.GetItems()[3];
     }
     else if (parity == System.IO.Ports.Parity.Space)
     {
         lvParity.SelectedItem = lvParity.GetItems()[4];
     }
 }
Esempio n. 23
0
        public bool Open(string SerialName, int BaudRate, int dataBis, System.IO.Ports.Parity Pority, System.IO.Ports.StopBits StopBits)
        {
            try
            {
                mySerialPort.PortName = SerialName;
                mySerialPort.BaudRate = BaudRate;
                mySerialPort.DataBits = dataBis;
                mySerialPort.Parity   = Pority;
                mySerialPort.StopBits = StopBits;

                //mySerialPort.PortName = SerialName.Split('(')[1].Split(')')[0];
                if (!mySerialPort.IsOpen)
                {
                    mySerialPort.Open();
                }

                return(true);
            }
            catch (Exception ex)
            {
                //mySerialPort.Close();
                return(false);
            }
        }
Esempio n. 24
0
 public SerialPort(string portName, int baudRate, System.IO.Ports.Parity parity, int dataBits, System.IO.Ports.StopBits stopBits)
 {
     this.baudRate = 0x2580;
     this.dataBits = 8;
     this.stopBits = System.IO.Ports.StopBits.One;
     this.portName = "COM1";
     this.encoding = System.Text.Encoding.ASCII;
     this.decoder  = System.Text.Encoding.ASCII.GetDecoder();
     this.maxByteCountForSingleChar = System.Text.Encoding.ASCII.GetMaxByteCount(1);
     this.readTimeout            = -1;
     this.writeTimeout           = -1;
     this.receivedBytesThreshold = 1;
     this.parityReplace          = 0x3f;
     this.newLine         = "\n";
     this.readBufferSize  = 0x1000;
     this.writeBufferSize = 0x800;
     this.inBuffer        = new byte[0x400];
     this.oneChar         = new char[1];
     this.PortName        = portName;
     this.BaudRate        = baudRate;
     this.Parity          = parity;
     this.DataBits        = dataBits;
     this.StopBits        = stopBits;
 }
Esempio n. 25
0
        public bool Init(string portname, int rate, int databits, System.IO.Ports.StopBits stopbits, System.IO.Ports.Parity parity)
        {
            if (sp == null)
            {
                return(false);
            }
            bool success = false;

            try
            {
                sp.PortName  = portname;
                sp.BaudRate  = rate;
                sp.DataBits  = databits;
                sp.StopBits  = stopbits;
                sp.Parity    = parity;
                sp.Handshake = System.IO.Ports.Handshake.None;
                sp.Encoding  = Encoding.UTF8;
                success      = true;
            }
            catch (Exception ex)
            {
            }
            return(success);
        }
Esempio n. 26
0
 //-------------------------------------------------------------------------------
 /// <summary>
 /// Обработчик события выбора паритета
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void comboBoxParity_SelectedIndexChanged(object sender, EventArgs e)
 {
     ComboBox cb = sender as ComboBox;
     _Parity = (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity), 
         (String)cb.SelectedItem);
     return;
 }
Esempio n. 27
0
 public SerialPort(string portName, int baudRate, System.IO.Ports.Parity parity, int dataBits, System.IO.Ports.StopBits stopBits)
 {
 }
Esempio n. 28
0
 public SerialPort(string portName, int baudRate, System.IO.Ports.Parity parity)
 {
 }
Esempio n. 29
0
 public SerialProtocol(String portName, int baudRate, System.IO.Ports.Parity parity, int dataBits, System.IO.Ports.StopBits stopBits)
 {
     sPort = new System.IO.Ports.SerialPort(portName, baudRate, parity, dataBits, stopBits);
     sPort.Open();
 }
Esempio n. 30
0
 public SimpleSerial(string portName, int baudRate, System.IO.Ports.Parity parity)
     : base(portName, baudRate, parity)
 {
 }
Esempio n. 31
0
 // CONSTRUCTORS -- Pass the Buck
 public SimpleSerial(string portName, int baudRate, System.IO.Ports.Parity parity, int dataBits, System.IO.Ports.StopBits stopBits)
     : base(portName, baudRate, parity, dataBits, stopBits)
 {
 }
 public SerialPort(string portName, int baudRate, System.IO.Ports.Parity parity, int dataBits)
 {
     Port = new System.IO.Ports.SerialPort(portName, baudRate, parity, dataBits);
     Port.Open();
 }