/// <summary> /// Constructor for SerialPortController that allows data to be read from and /// written to the serial port. /// </summary> /// <param name="isThreaded">Determines if the object uses multiple threads (with no blocking) or a single thread (with possible blocking)</param> /// <param name="isPrintWarnings">Print warnings to Console when buffers overflow is set to true</param> /// <param name="maxBufferSize">The maximum size of the bytes buffer (2048 a good value in practice)</param> public SerialPortController(bool isThreaded, bool isPrintWarnings, int maxBufferSize) { this.isThreaded= isThreaded; this.maxBufferSize = maxBufferSize; bytesBuffer = new byte[maxBufferSize]; //if (SAVE_TIMING) // bytesBufferTiming = new long[maxBufferSize]; // create the port settings portSettings = new HandshakeNone(); // create a default port on COM2 with no handshaking port = new Port("COM1:", portSettings); // these are fairly inefficient settings // for a terminal we want to send/receive all data as it is queued // so 1 byte at a time is the best way to go port.RThreshold = 1; // get an event for every 1 byte received port.InputLen = 1; // calling Input will read 1 byte port.SThreshold = 1; // send 1 byte at a time // define an event handler if using threaded option if (isThreaded) port.DataReceived +=new Port.CommEvent(port_DataReceived); this.isPrintWarnings = isPrintWarnings; }
public void SerialOpen(String portName, BaudRates baud) { BasicPortSettings portSettings = new BasicPortSettings(); portSettings.BaudRate = baud; mPort = new Port(portName, portSettings); mPort.RThreshold = 1; mPort.SThreshold = 1; // send 1 byte at a time mPort.InputLen = 0; mPort.Open(); mPort.DataReceived +=new Port.CommEvent(mPort_DataReceived); }
public OpenNETCFSerialPort(string portName, int baudRate) { serialPort = new Port(portName, new HandshakeNone(), portBufSize, portBufSize); serialPort.Settings.BaudRate = (BaudRates)baudRate; serialPort.Settings.ByteSize = 8; serialPort.Settings.Parity = Parity.none; serialPort.Settings.StopBits = StopBits.one; serialPort.InputLen = 0; serialPort.RThreshold = 1; serialPort.SThreshold = 1; serialPort.DataReceived += new Port.CommEvent(DataReceived); serialPort.OnError += new Port.CommErrorEvent(CommError); }
private void OpenCOM() { string szPort=""; try { szPort = GetGPSPort(); if (szPort == "") szPort = "GPD1:"; serialPort = new Port(szPort, new HandshakeNone()); AddRawText("Trying to open " + szPort + ":..."); serialPort.Settings.BaudRate = BaudRates.CBR_57600; serialPort.RThreshold = 1; serialPort.InputLen = 1; serialPort.DataReceived += new OpenNETCF.IO.Serial.Port.CommEvent(serialPort_DataReceived); serialPort.Open(); AddRawText("port opened..."); } catch(OpenNETCF.IO.Serial.CommPortException sx) { AddRawText("Exception in OpenCOM " + szPort); AddRawText(sx.Message); mnuRAWStart.Enabled = true; mnuRAWStop.Enabled = false; } }
private void run() { setstate=States.Opening; this.reset_gps_vars(); DataReceived+= new DataReceivedEventHandler(GPS_DataReceived); if (!demomodeon) { if (this.autodiscovery) { if (!autodiscover()) { setstate=States.Stopped; return; } } } if (!demomodeon) { if (!isconnecteddevice()) { OnError(null,"Com Port "+comport+" Is Not On Device",""); setstate=States.Stopped; return; } if (state!=States.AutoDiscovery) { DetailedPortSettings portSettings = new HandshakeNone(); cp = new Port(comport,portSettings); cp.Settings.BaudRate=this.baudrate; cp.RThreshold=64; cp.InputLen=1; try { cp.Open(); } catch (Exception e) { OnError(e,"Could Not Open Com Port "+comport,""); cp.Dispose(); setstate=States.Stopped; return; } cp.OnError+=new Port.CommErrorEvent(cp_OnError); cp.DataReceived+=new Port.CommEvent(cp_DataReceived); } } if (state==States.Opening || state==States.AutoDiscovery) setstate=States.Running; if (demomodeon) { load_demo_data(); } else { while (state==States.Running) { Thread.Sleep(10); } if (cp.IsOpen) cp.Close(); cp.Dispose(); cp=null; } setstate=States.Stopped; }
/// <summary> /// Auto discover our GPS /// </summary> /// <returns>True if we have found a GPS</returns> private bool autodiscover() { Type searchbauds = typeof(OpenNETCF.IO.Serial.BaudRates); // get a list of devices foreach (string port in this.devices()) { foreach (OpenNETCF.IO.Serial.BaudRates bauds in OpenNETCF.EnumEx.GetValues(searchbauds)) { if ((bauds >= minbaudrate) && (bauds <= maxbaudrate)) { // initialize the port like in run proc DetailedPortSettings portSettings = new HandshakeNone(); cp = new Port(port,portSettings); cp.Settings.BaudRate = bauds; cp.RThreshold=64; cp.InputLen=1; OnAutoDiscovery( OpenNETCF.IO.Serial.GPS.AutoDiscoverStates.Testing,port,cp.Settings.BaudRate); try { cp.Open(); } catch { OnAutoDiscovery(OpenNETCF.IO.Serial.GPS.AutoDiscoverStates.FailedToOpen,port,cp.Settings.BaudRate); } if (cp.IsOpen) { // if port open, invoke cp_DataReceived if bytes occur cp.DataReceived+=new Port.CommEvent(cp_DataReceived); if (state==States.Opening) setstate=States.Running; int cpt=0; while (state==States.Running && cpt <= 100) { // wait 1 seconds for the sentences Thread.Sleep(10); cpt++; } if (state==States.AutoDiscovery) { OnAutoDiscovery(OpenNETCF.IO.Serial.GPS.AutoDiscoverStates.Opened,port,cp.Settings.BaudRate); comport = port; baudrate = cp.Settings.BaudRate; autodiscovery=false; return true; } else { cp.Close(); setstate=States.Opening; } OnAutoDiscovery(OpenNETCF.IO.Serial.GPS.AutoDiscoverStates.Failed,port,cp.Settings.BaudRate); } cp.Dispose(); cp=null; } } } // default values OnAutoDiscovery(OpenNETCF.IO.Serial.GPS.AutoDiscoverStates.NoGPSDetected,comport,baudrate); autodiscovery=false; return false; }
public CommWrapper() { this.port = new Port("COM1", this.portSettings); this.port.InputLen = 0; }
protected virtual void Dispose(bool disposing) { if (!this.isDisposed && disposing) { this.port.Dispose(); this.port = null; } this.isDisposed = true; }