Exemplo n.º 1
0
        public int Start()
        {
            if (xSerialPort != null && xSerialPort.IsOpen == true)
            {
                xSerialPort.Close();
            }
            if (xSerialPort == null)
            {
//                xSerialPort = new SerialPort(PortName, BaudRate, Parity, DataBits, StopBits);
                xSerialPort = new WinSerialPort(PortName, BaudRate, Parity, DataBits, StopBits);
            }
            try
            {
                /*  //typical settings
                 * xSerialPort.BaudRate = 9600;
                 * xSerialPort.DataBits = 8;
                 * xSerialPort.DiscardNull = false;
                 * xSerialPort.DtrEnable = false;
                 * xSerialPort.Encoding = Encoding.UTF8;
                 * xSerialPort.Handshake = Handshake.None;
                 * xSerialPort.NewLine = Environment.NewLine;
                 * xSerialPort.Parity = Parity.None;
                 * xSerialPort.ParityReplace = 63;
                 * xSerialPort.PortName = "COM3";
                 * xSerialPort.ReadBufferSize = 4096;
                 * xSerialPort.ReadTimeout = -1;
                 * xSerialPort.ReceivedBytesThreshold = 1;
                 * xSerialPort.RtsEnable = false;
                 * xSerialPort.StopBits = StopBits.One;
                 * xSerialPort.WriteBufferSize = 2048;
                 * xSerialPort.WriteTimeout = -1;
                 */
                xSerialPort.portName = PortName;
                xSerialPort.Open();
                receiveThread = new Thread(SerialPortProcessor.ReceiveWork);
                receiveThread.Start(this);
                return(0);
            }
            catch (IOException ex)
            {
                MessageBox.Show("ポートのオープン中、I/O 例外が発生しました。" + ex.ToString(), "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(1);
            }
        }