// Note: A constructor summary is auto-generated by the doc builder. /// <summary></summary> /// <param name="socketNumber">The socket that this module is plugged in to.</param> /// <param name="baud">The baud rate to communicate with the module with.</param> public Bluetooth(int socketNumber, long baud) { // This finds the Socket instance from the user-specified socket number. This will generate user-friendly error messages if the socket is invalid. If there is more than one socket on this // module, then instead of "null" for the last parameter, put text that identifies the socket to the user (e.g. "S" if there is a socket type S) Socket socket = Socket.GetSocket(socketNumber, true, this, null); this.reset = GTI.DigitalOutputFactory.Create(socket, Socket.Pin.Six, false, this); this.statusInt = GTI.InterruptInputFactory.Create(socket, Socket.Pin.Three, GTI.GlitchFilterMode.Off, GTI.ResistorMode.Disabled, GTI.InterruptMode.RisingAndFallingEdge, this); this.serialPort = GTI.SerialFactory.Create(socket, 38400, GTI.SerialParity.None, GTI.SerialStopBits.One, 8, GTI.HardwareFlowControl.NotRequired, this); //this.statusInt.Interrupt += GTI.InterruptInputFactory.Create.InterruptEventHandler(statusInt_Interrupt); this.serialPort.ReadTimeout = Timeout.Infinite; this.serialPort.Open(); Thread.Sleep(5); this.reset.Write(true); // Poundy added: Thread.Sleep(5); this.SetDeviceBaud(baud); this.serialPort.Flush(); this.serialPort.Close(); this.serialPort.BaudRate = (int)baud; this.serialPort.Open(); // Poundy readerThread = new Thread(new ThreadStart(runReaderThread)); readerThread.Start(); Thread.Sleep(500); }
void Port_LineReceived(GT.SocketInterfaces.Serial sender, string line) { try { Debug.Print(line); string[] data = line.Split('|'); switch (data[0]) { case "Relay1": { var state = data[1].ToLower() == "true" ? true : false; Debug.Print("relay 1:" + state); } break; case "Relay2": { var state = data[1].ToLower() == "true" ? true : false; Debug.Print("relay 2:" + state); } break; } } catch (Exception ex) { Debug.Print(ex.ToString()); //logs.WriteLogs("relay error :" + ex); } }
/// <summary>Initializes the serial port with the given parameters.</summary> /// <param name="baudRate">The baud rate to use.</param> /// <param name="parity">The parity to use.</param> /// <param name="stopBits">The stop bits to use.</param> /// <param name="dataBits">The number of data bits to use.</param> /// <param name="flowControl">The flow control to use.</param> public void Configure(int baudRate, GTI.SerialParity parity, GTI.SerialStopBits stopBits, int dataBits, GTI.HardwareFlowControl flowControl) { if (this.serialPort != null) { throw new InvalidOperationException("Configure can only be called once."); } this.serialPort = GTI.SerialFactory.Create(socket, baudRate, parity, stopBits, dataBits, flowControl, this); this.serialPort.Open(); }
private void OnSerialDataReceived(GTI.Serial sender) { var available = this.SerialPort.BytesToRead; var data = new byte[available]; var size = this.SerialPort.Read(data, 0, available); string line = new string(Encoder.UTF8.GetChars(data, 0, size)); if (line == null) { return; } this.serialBuffer += line; this.Log(line); if (this.serialBuffer.IndexOf("*OPEN*") >= 0) { this.serialBuffer = this.Replace("*OPEN*", "", this.serialBuffer); this.OnConnectionEstablished(this, null); } if (this.serialBuffer.IndexOf("*CLOS*") >= 0) { this.serialBuffer = this.Replace("*CLOS*", "", this.serialBuffer); this.OnConnectionClosed(this, null); } if (this.serialBuffer.IndexOf("\r\r") >= 0) { this.serialBuffer = this.Replace("\r\r", "\r", this.serialBuffer); } if (this.serialBuffer.IndexOf("\r\n") >= 0) { int index = -1; while ((index = this.serialBuffer.IndexOf("\r\n")) >= 0) { string newLine = this.serialBuffer.Substring(0, index) + "\r\n"; this.serialBuffer = this.serialBuffer.Substring(index + 2); this.OnSerialLineReceived(newLine); } } if (this.currentRequest != null && this.awaitingPostData && this.serialBuffer.Length >= this.currentRequest.PostLength) { this.OnSerialLineReceived(this.serialBuffer); } this.OnDataReceived(this, line); }
void ProgramStarted() { Debug.Print("Program Started"); GT.Socket socket = GT.Socket.GetSocket(11, true, null, null); serial = GT.SocketInterfaces.SerialFactory.Create(socket, 9600, SerialParity.None, SerialStopBits.One, 8, HardwareFlowControl.UseIfAvailable, extender); Debug.Print(serial.PortName); serial.DataReceived +=ser_DataReceived; serial.Open(); button.ButtonPressed += MonitorIntruders; monitor.Tick += monitor_Tick; }
/// <summary>Constructs a new instance.</summary> /// <param name="stream">The stream to use.</param> public HttpResponse(GTI.Serial stream) { if (stream == null) { throw new ArgumentNullException("stream"); } this.stream = stream; this.HeaderData = new StringHashtable(); }
/// <summary>Constructs a new instance.</summary> /// <param name="socketNumber">The socket that this module is plugged in to.</param> public PulseOximeter(int socketNumber) { this.IsProbeAttached = false; this.LastReading = null; Socket socket = Socket.GetSocket(socketNumber, true, this, null); this.serialPort = GTI.SerialFactory.Create(socket, 4800, GTI.SerialParity.Even, GTI.SerialStopBits.One, 8, GTI.HardwareFlowControl.NotRequired, this); this.serialPort.Open(); this.workerThread = new Thread(this.DoWork); this.workerThread.Start(); }
void Port_DataReceived(Gadgeteer.SocketInterfaces.Serial sender) { if (this.DataReceived == null) { return; } var buffer = new byte[sender.BytesToRead]; sender.Read(buffer, 0, buffer.Length); this.DataReceived(this, buffer); }
/// <summary>Constructs a new instance.</summary> /// <param name="request">The http request the session will represent.</param> /// <param name="stream">The stream of serial data.</param> public HttpSession(HttpRequest request, GTI.Serial stream) { if (request == null) { throw new ArgumentNullException("request"); } if (stream == null) { throw new ArgumentNullException("stream"); } this.request = request; this.response = new HttpResponse(stream); }
/// <summary>Constructs a new instance.</summary> /// <param name="socketNumber">The socket that this module is plugged in to.</param> public SerialCameraL2(int socketNumber) { Socket socket = Socket.GetSocket(socketNumber, true, this, null); socket.EnsureTypeIsSupported('U', this); this.port = GTI.SerialFactory.Create(socket, 115200, GTI.SerialParity.None, GTI.SerialStopBits.One, 8, GTI.HardwareFlowControl.NotRequired, this); this.port.ReadTimeout = 500; this.port.WriteTimeout = 500; this.port.Open(); this.ResetCamera(); this.newImageReady = false; this.updateStreaming = false; this.paused = false; }
/// <summary>Constructs a new instance.</summary> /// <param name="socketNumber">The socket that this module is plugged in to.</param> public GPS(int socketNumber) { Socket socket = Socket.GetSocket(socketNumber, true, this, null); socket.EnsureTypeIsSupported('U', this); this.lastPositionReceived = TimeSpan.MinValue; this.LastPosition = null; this.powerControl = GTI.DigitalOutputFactory.Create(socket, Socket.Pin.Three, true, this); this.gpsExtInt = GTI.DigitalOutputFactory.Create(socket, Socket.Pin.Six, true, this); this.serial = GTI.SerialFactory.Create(socket, 9600, GTI.SerialParity.None, GTI.SerialStopBits.One, 8, GTI.HardwareFlowControl.NotRequired, this); this.serial.NewLine = "\r\n"; this.serial.LineReceived += this.OnLineReceived; }
/// <summary>Constructs a new instance.</summary> /// <param name="socketNumber">The socket that this module is plugged in to.</param> public RFIDReader(int socketNumber) { Socket socket = Socket.GetSocket(socketNumber, true, this, null); socket.EnsureTypeIsSupported('U', this); this.buffer = new byte[RFIDReader.MESSAGE_LENGTH]; this.read = 0; this.checksum = 0; this.port = GTI.SerialFactory.Create(socket, 9600, GTI.SerialParity.None, GTI.SerialStopBits.Two, 8, GTI.HardwareFlowControl.NotRequired, this); this.port.ReadTimeout = 10; this.port.Open(); this.timer = new GT.Timer(100); this.timer.Tick += this.DoWork; this.timer.Start(); }
void ser_DataReceived(Serial sender) { Debug.Print(sender.PortName); }
/// <summary>Constructs a new instance.</summary> /// <param name="socketNumber">The socket that this module is plugged in to.</param> public WiFiRN171(int socketNumber) { Socket socket = Socket.GetSocket(socketNumber, true, this, null); var useFlowControl = socket.SupportsType('K'); this.LocalIP = "0.0.0.0"; this.LocalListenPort = "0"; this.DebugPort = null; this.Ready = false; this.commandModeResponse = ""; this.serialBuffer = ""; this.timeout = 10000; this.useDhcp = false; this.inCommandMode = false; this.updateComplete = new AutoResetEvent(false); this.commandResponseComplete = new AutoResetEvent(false); this.awaitingPostData = false; this.httpEnabled = false; this.httpBuffer = ""; this.currentRequest = null; this.onHttpRequestReceived = this.OnHttpRequestReceived; this.onDataReceived = this.OnDataReceived; this.onLineReceived = this.OnLineReceived; this.onConnectionEstablished = this.OnConnectionEstablished; this.onConnectionClosed = this.OnConnectionClosed; if (!useFlowControl) { socket.EnsureTypeIsSupported('U', this); } this.reset = GTI.DigitalOutputFactory.Create(socket, Socket.Pin.Three, true, this); this.rts = GTI.DigitalOutputFactory.Create(socket, Socket.Pin.Six, false, this); this.SerialPort = GTI.SerialFactory.Create(socket, 115200, GTI.SerialParity.None, GTI.SerialStopBits.One, 8, useFlowControl ? GTI.HardwareFlowControl.Required : GTI.HardwareFlowControl.NotRequired, this); this.SerialPort.DataReceived += this.OnSerialDataReceived; this.SerialPort.Open(); this.Reset(); var end = DateTime.Now.AddMilliseconds(this.timeout); while (!this.Ready) { if (end <= DateTime.Now && this.timeout > 0) { throw new TimeoutException(); } Thread.Sleep(1); } this.EnterCommandMode(); this.ExecuteCommand("set sys printlvl 0"); this.ExecuteCommand("set comm remote 0"); if (useFlowControl) { this.ExecuteCommand("set uart flow 1"); } this.ExecuteCommand("set uart tx 1"); this.ExitCommandMode(); }
/// <summary>Constructs a new instance.</summary> /// <param name="socketNumber">The socket that this module is plugged in to.</param> public USBSerial(int socketNumber) { this.socket = Socket.GetSocket(socketNumber, true, this, null); this.socket.EnsureTypeIsSupported('U', this); this.serialPort = null; }
/// <summary>Constructs a new instance.</summary> /// <param name="socketNumber">The socket that this module is plugged in to.</param> public RS232(int socketNumber) { this.socket = Socket.GetSocket(socketNumber, true, this, null); this.socket.EnsureTypeIsSupported(new char[] { 'U', 'K' }, this); this.serialPort = null; }
private void OnLineReceived(GTI.Serial sender, string line) { try { if (line != null && line.Length > 0) { this.OnNmeaSentenceReceived(this, line); } if (line.Substring(0, 7) != "$GPRMC,") { return; } string[] tokens = line.Split(','); if (tokens.Length != 13) { this.DebugPrint("RMC NMEA line does not have 13 tokens, ignoring"); return; } if (tokens[2] != "A") { this.OnInvalidPositionReceived(this, null); return; } double timeRawDouble = Double.Parse(tokens[1]); int timeRaw = (int)timeRawDouble; int hours = timeRaw / 10000; int minutes = (timeRaw / 100) % 100; int seconds = timeRaw % 100; int milliseconds = (int)((timeRawDouble - timeRaw) * 1000.0); int dateRaw = Int32.Parse(tokens[9]); int days = dateRaw / 10000; int months = (dateRaw / 100) % 100; int years = 2000 + (dateRaw % 100); Position position = new Position(); position.FixTimeUtc = new DateTime(years, months, days, hours, minutes, seconds, milliseconds); position.LatitudeString = tokens[3] + " " + tokens[4]; position.LongitudeString = tokens[5] + " " + tokens[6]; double latitudeRaw = double.Parse(tokens[3]); int latitudeDegreesRaw = ((int)latitudeRaw) / 100; double latitudeMinutesRaw = latitudeRaw - (latitudeDegreesRaw * 100); position.Latitude = latitudeDegreesRaw + (latitudeMinutesRaw / 60.0); if (tokens[4] == "S") { position.Latitude = -position.Latitude; } double longitudeRaw = double.Parse(tokens[5]); int longitudeDegreesRaw = ((int)longitudeRaw) / 100; double longitudeMinutesRaw = longitudeRaw - (longitudeDegreesRaw * 100); position.Longitude = longitudeDegreesRaw + (longitudeMinutesRaw / 60.0); if (tokens[6] == "W") { position.Longitude = -position.Longitude; } position.SpeedKnots = 0; if (tokens[7] != "") { position.SpeedKnots = Double.Parse(tokens[7]); } position.CourseDegrees = 0; if (tokens[8] != "") { position.CourseDegrees = Double.Parse(tokens[8]); } this.lastPositionReceived = GT.Timer.GetMachineTime(); this.LastPosition = position; this.OnPositionReceived(this, position); } catch { this.DebugPrint("Error parsing RMC NMEA message"); } }