当前位置: 首页>>代码示例>>C#>>正文


C# SerialPort.Open方法代码示例

本文整理汇总了C#中System.IO.Ports.SerialPort.Open方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.Open方法的具体用法?C# SerialPort.Open怎么用?C# SerialPort.Open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.Ports.SerialPort的用法示例。


在下文中一共展示了SerialPort.Open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SerialDevice

        public SerialDevice(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits, string returnToken = "\n")
        {
            _dataReading = string.Empty;
            _dataQueue = new ConcurrentQueue<string>();

            _timeCounter = new Stopwatch();
            _timeCounter.Start();

            _COMPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);

            _returnToken = returnToken;

            _COMPort.NewLine = _returnToken;
            _COMPort.RtsEnable = true;
            _COMPort.DtrEnable = true;

            _COMPort.ReadTimeout = SerialPort.InfiniteTimeout;
            _COMPort.WriteTimeout = SerialPort.InfiniteTimeout;

            _COMPort.Open();

            if (!_COMPort.IsOpen)
                _COMPort.Open();
            if (!_COMPort.IsOpen)
                throw new Exception("Can't connect to the COM port!");

            _serialThread = new Thread(new ThreadStart(GetSerialDataContinious));
            _serialThread.Priority = ThreadPriority.Normal;
            _serialThread.Name = string.Format("SerialHandle{0}", _serialThread.ManagedThreadId);

            _communicatyionIsActive = true;
            _serialThread.Start();
        }
开发者ID:GandziukVI,项目名称:SDA_ExtensionBoxController,代码行数:33,代码来源:SerialDevice.cs

示例2: GetTemperature

 public XmlElement GetTemperature(string port, string baudRate)
 {
     try
     {
         _serialPort = new SerialPort(port);
         _serialPort.BaudRate = Convert.ToInt32(baudRate);
         _serialPort.Parity = Parity.None;
         _serialPort.StopBits = StopBits.One;
         _serialPort.DataBits = 8;
         _serialPort.Handshake = Handshake.None;
         _serialPort.RtsEnable = true;
         _serialPort.ReadTimeout = 1000;
         if (_serialPort.IsOpen)
         {
             _serialPort.Close();
             _serialPort.Open();
         }
         else
         {
             _serialPort.Open();
         }
         Thread.Sleep(1000);
         count = _serialPort.BytesToRead;
         if (count < 1)
         {
             throw new Exception("No Data to Read..."+count);
         }
         else
         {
             while (count > 0)
             {
                 int byteData = _serialPort.ReadByte();
                 data = data + Convert.ToChar(byteData);
                 count--;
             }
         }
         _serialPort.DiscardOutBuffer();
         _serialPort.Close();
         _result = GetXML(data.Trim());
     }
     catch (Exception ex)
     {
         if (_serialPort.IsOpen)
             _serialPort.Close();
         _result = GetExceptionXML(ex.ToString());
     }
     return _result;
 }
开发者ID:stellant,项目名称:LaserpointFinal,代码行数:48,代码来源:Temperature.svc.cs

示例3: ModbusSerialAsciiMasterReadRegisters

        /// <summary>
        ///     Simple Modbus serial ASCII master read holding registers example.
        /// </summary>
        public static void ModbusSerialAsciiMasterReadRegisters()
        {
            using (SerialPort port = new SerialPort("COM1"))
            {
                // configure serial port
                port.BaudRate = 9600;
                port.DataBits = 8;
                port.Parity = Parity.None;
                port.StopBits = StopBits.One;
                port.Open();

                // create modbus master
                IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(port);

                byte slaveId = 1;
                ushort startAddress = 1;
                ushort numRegisters = 5;

                // read five registers		
                ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, numRegisters);

                for (int i = 0; i < numRegisters; i++)
                    Console.WriteLine("Register {0}={1}", startAddress + i, registers[i]);
            }

            // output: 
            // Register 1=0
            // Register 2=0
            // Register 3=0
            // Register 4=0
            // Register 5=0
        }
开发者ID:baminmru,项目名称:vodopad-ip-server,代码行数:35,代码来源:Driver.cs

示例4: Open

 public void Open()
 {
     serial = new SerialPort(portName, boudRate);
     serial.DataReceived += new SerialDataReceivedEventHandler(Serial_DataReceived);
     serial.Open();
     Write("Hello there.");
 }
开发者ID:marciotoshio,项目名称:TwitterToSerial,代码行数:7,代码来源:Serial.cs

示例5: BalanceReader

        /*
         *         public string ComPort = String.Empty;
        public int BaudRate = 110;
        public StopBits StopBits = StopBits.None;
        public int DataBits = 7;
        public Parity Parity = Parity.None;
         * */
        public BalanceReader(string comPort = "COM1", int baudRate = 9600, StopBits stopBits = StopBits.One,
            int dataBits = 7, Parity parity = Parity.Even, string sicsCommand = "SI", bool rts = false)
        {
            _sicsCommand = sicsCommand;

            if (comPort == String.Empty) comPort = "COM1";
            _port = new SerialPort
            {
                PortName = comPort,
                BaudRate = baudRate,
                StopBits = stopBits,
                DataBits = 7,
                Parity = Parity.Even,
                RtsEnable = rts
            };

            // from Page 39 of NewClassic Balances METTLER TOLEDO manual for MS-S / MS-L Models
            //_port.Handshake = Handshake.XOnXOff;

            if (_port.IsOpen == false)
            {
                try
                {
                    _port.Open();
                }
                catch (Exception)
                {
                    // port will not be open, therefore will become null
                }
            }

            _port.DataReceived += _port_DataReceived;
        }
开发者ID:ajarrott,项目名称:NewBalance,代码行数:40,代码来源:BalanceReader.cs

示例6: Main

        static void Main(string[] args)
        {
            string result;
            int end = 0;
            SerialPort sp = new SerialPort("COM2");
            sp.BaudRate = 115200; // 56700;
            sp.NewLine = "\r\n";
            sp.Open();
            sendCommand(sp, "AT+RST", eWaitFor.eOK);
            Thread.Sleep(1000);
            sendCommand(sp, "AT+CWJAP=\"\",\"\"", eWaitFor.eOK | eWaitFor.eFAIL);
            Thread.Sleep(1000);
            sendCommand(sp, "AT+CWMODE=1", eWaitFor.eOK | eWaitFor.eNoChange);
            Thread.Sleep(1000);
            //sendCommand(sp, "AT+CWJAP=\"CBA_F1\",\"abc\"", eWaitFor.eOK);
            //sendCommand(sp, "AT+CWJAP=\"LumiaTk5276\",\"abc\"", eWaitFor.eOK);
            sendCommand(sp, "AT+CWJAP=\"edup-rt\",\"abc\"", eWaitFor.eOK);

            Thread.Sleep(1000);
            Console.WriteLine("IP: " + getIP(sp));
            sendCommand(sp, "AT+CIPMUX=1", eWaitFor.eOK);
            sendCommand(sp, "AT+CIPSTART=4,\"TCP\",\"google.com\",80", eWaitFor.eOK);
            sendCommand(sp, "AT+CIPSEND=4,18", eWaitFor.eGT);
            sp.Write("GET / HTTP/1.0\r\n\r\n");
            Thread.Sleep(5000); //Czekamy tu na internet
            result = sp.ReadExisting();
            Console.WriteLine(result);

            //sp.WriteLine("AT+CIFSR");
            //result = sp.ReadTo("OK\r");

            sp.Close();
        }
开发者ID:tkopacz,项目名称:iot-kabelki-azure-iot-hub,代码行数:33,代码来源:Program.cs

示例7: Main

        static void Main(string[] args)
        {
            int i = 0;
            SerialPort SP = null;
            Console.WriteLine("Specify which comport you want to connect to:");
            
            foreach (string s in SerialPort.GetPortNames())
            {
                Console.WriteLine($"{i++,2} {s}");
            }
            int PortNo = int.Parse(Console.ReadLine());
            try
            {
                SP = new SerialPort(SerialPort.GetPortNames()[PortNo]);
                SP.Open();
                while(true)
                {
                    Console.Write(SP.ReadExisting());
                }
            }
            finally
            {
                if (SP.IsOpen)
                {
                    SP.Close();
                }
            }

            Console.Read();
        }
开发者ID:svutborg,项目名称:Programming-II,代码行数:30,代码来源:Program.cs

示例8: LCD43

 public LCD43()
 {
     comLcdPri = new SerialPort(SerialPorts.COM1, 115200, Parity.None, 8, StopBits.One);
     comLcdPri.DataReceived += new SerialDataReceivedEventHandler(Lcd_DataReceived);
     comLcdPri.ReadTimeout = 100;
     comLcdPri.Open();
 }
开发者ID:TomasFilingr,项目名称:WD-offline,代码行数:7,代码来源:LCD43.cs

示例9: Start

        public void Start(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
        {
            if (!GetAvailablePorts().Contains(portName))
            {
                throw new Exception(string.Format("Unknown serial port: {0}", portName));
            }

            // Start the timer to empty the receive buffer (in case data smaller than MAX_RECEIVE_BUFFER is received)
            _bufferTimer = new Timer();
            _bufferTimer.Interval = BUFFER_TIMER_INTERVAL;
            _bufferTimer.Elapsed += _bufferTimer_Elapsed;
            _bufferTimer.Start();

            // Instantiate new serial port communication
            _serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);

            // Open serial port communication
            _serialPort.Open();

            // Check that it is actually open
            if (!_serialPort.IsOpen)
            {
                throw new Exception(string.Format("Could not open serial port: {0}", portName));
            }

            _serialPort.ReadTimeout = 100; // Milliseconds

            _readThread = new System.Threading.Thread(ReadThread);
            _readThreadRunning = true;
            _readThread.Start();
        }
开发者ID:whitestone-no,项目名称:open-serial-port-monitor,代码行数:31,代码来源:SerialReader.cs

示例10: PortModel

 public PortModel(string portName)
 {
     _serialPort = new SerialPort {PortName = portName};
     _serialPort.Open();
     _serialPort.ReadTimeout = _readTimeout;
     _serialPort.WriteTimeout = _writeTimeout;
 }
开发者ID:kbp,项目名称:alfa-server,代码行数:7,代码来源:PortModel.cs

示例11: start

 // Start Comms
 private void start() {
     clearcontainers();
     arduino = new SerialPort(port, 9600);
     arduino.DataReceived += new SerialDataReceivedEventHandler(receivedTag);
     arduino.Open();
     STATUS = DEVICE_STATE_LISTENING;
 }
开发者ID:Globlock,项目名称:Version0,代码行数:8,代码来源:BrokerReader.cs

示例12: Connect

        public void Connect(string portName, int baudRate = 115200)
        {
            try
            {
                serialPort = new SerialPort(portName);

                serialPort.BaudRate = baudRate;
                serialPort.Parity = Parity.None;
                serialPort.StopBits = StopBits.One;
                serialPort.DataBits = 8;
                serialPort.DataReceived += serialPort_DataReceived;

                serialPort.Open();

                isConnected = true;

                DebugPortState(String.Format("Connected to port {0}.", portName));

                if (OnConnectedEvent != null)
                    OnConnectedEvent(this,null);
            }
            catch (Exception ex)
            {
                DebugPortState(String.Format("Can`t connect to port {0}.", portName));

                if (OnConnectingError != null)
                    OnConnectingError(ex);
            }
        }
开发者ID:sureshmali,项目名称:MyNetSensors,代码行数:29,代码来源:ComPort.cs

示例13: SerialConnect

        // Connect to transmitter via serial port
        public void SerialConnect()
        {
            if (BaudRate != 0 && PortName != "None")
            {
                port = new SerialPort(PortName, BaudRate);
                port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                try
                {
                    port.Open();
                }
                catch
                { //todo 
                }

                if (port.IsOpen)
                {
                    Console.WriteLine(PortName + " is open");
                }
                else
                {
                    Console.WriteLine("Unable to open " + PortName);
                }
            }
            else
            {
                Console.WriteLine("Port name or baud rate not set prior to opening connection");
            }
        }
开发者ID:esayers,项目名称:iSOBOT,代码行数:29,代码来源:Isobot.cs

示例14: MidiDriver

        public MidiDriver(Cpu.Pin resetPin, string serialPortName = Serial.COM2)
        {
            _resetPinPort = new OutputPort(resetPin, false);

            _serialPort = new SerialPort(serialPortName, 31250, Parity.None, 8, StopBits.One);
            _serialPort.Open();
        }
开发者ID:cjdaly,项目名称:napkin,代码行数:7,代码来源:MidiDriver.cs

示例15: Serial2Matlab

 //        public Serial2Matlab(string port, int baudRate = 115200, Parity parity = Parity.None, int dataBits = 8, StopBits stopBits = StopBits.One)
 public Serial2Matlab()
 {
     serialPort = new SerialPort("COM43", 115200, Parity.None, 8, StopBits.One);
     serialPort.ReadTimeout = 2;
     serialPort.Open();
     serialPort.DiscardInBuffer();
 }
开发者ID:RiekertLeibbrandt,项目名称:marencoLibraries,代码行数:8,代码来源:Class1.cs


注:本文中的System.IO.Ports.SerialPort.Open方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。