static void Main(String[] args) { using (var serialPort = new SerialPortStream("COM1")) { serialPort.OpenDirect(); while (serialPort.IsOpen) { var ch = (Char)serialPort.ReadChar(); Console.Write(ch); } } }
public async Task Initialize() { _logger.LogInformation("Initializing air quality provider."); var i = 0; while (true) { if (i == 5) { var ports = SerialPortStream.GetPortNames(); _logger.LogWarning( $"Failed to initialize sensor connection. Change port. Available ports {string.Join(',', ports)}"); return; } try { _logger.LogInformation($"Connecting to: {_settings.PortName}"); _serial = new SerialPortStream(_settings.PortName, 9600, 8, Parity.None, StopBits.One); _serial.DataReceived += SerialOnDataReceived; _serial.ErrorReceived += OnErrorReceived; _serial.OpenDirect(); if (!_serial.IsOpen) { throw new InvalidOperationException(); } _serial.Handshake = Handshake.None; _serial.ReadTimeout = 1000; _serial.NewLine = "\r\n"; _lastUpdate = DateTime.Now; _logger.LogInformation("Provider initialized."); return; } catch (Exception e) { i++; _logger.LogWarning($"Failed to initialize. Re-trying {i} time."); } await Task.Delay(1000); } }
private bool _readInProgress = false; //TODO: this maybe better as a lock object //private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) //{ // Task.Run(async () => // { // if (!_readInProgress) // { // _readInProgress = true; // do // { // await PacketHandler.ReadPacketAsync(IoPort); // } while (IoPort.BytesToRead > 0); // //this is necessary as some packets are sent so close together that the dataReceived event is not fired. // _readInProgress = false; // } // }); //} public async Task Connect() { IoPort.PortName = Port; InitialiseConnection(); if (IoPort.IsOpen) { await Disconnect(); } State = ConnectionState.Connecting; Console.Out.WriteLine("Connecting using Port: {0}", IoPort.PortName); await Task.Run(() => IoPort.OpenDirect()); var msg = new CommandPacket(CommandPacket.Command.TestConnection); char resp; var initialisationDateTime = DateTime.Now; var timeout = false; await msg.SendAsync(IoPort); do { resp = (char)IoPort.ReadByte(); if (DateTime.Now > initialisationDateTime.AddMilliseconds(1000)) { timeout = true; } } while (resp != 'k' && timeout == false); if (timeout == true) { State = ConnectionState.Error; throw new Exception("Connection didn't initiate properly (failed to get software version)"); } State = ConnectionState.Connected; }
static void Main(string[] args) { Boolean bPortSet = false; Boolean bCommandSet = false; String comTarget = ""; String comCommand = ""; Console.WriteLine(""); Console.WriteLine("----------------------------------------------"); Console.WriteLine(" Welcome to the CraftComputing Epson HMD tool"); Console.WriteLine("----------------------------------------------"); Console.WriteLine(""); if (args.Length != 2) { } else { comTarget = args[0]; bPortSet = int.TryParse(comTarget, out _); if (args[1].ToLower().Equals("3don")) { comCommand = "set2d3d 1"; bCommandSet = true; } if (args[1].ToLower().Equals("3doff")) { comCommand = "set2d3d 0"; bCommandSet = true; } } if (bCommandSet && bPortSet) { var serialPort = new SerialPortStream("COM" + comTarget); try { Console.WriteLine("Opening COM" + comTarget); serialPort.OpenDirect(); Console.WriteLine("Sending " + comCommand); serialPort.WriteLine(comCommand); }catch (Exception e) { Console.WriteLine("There was an error"); Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); Console.WriteLine(e.Source); Console.WriteLine(e.InnerException); } serialPort.Close(); } else { Console.WriteLine("You did not provide the correct arguments."); Console.WriteLine(""); Console.WriteLine("The syntax is: Epson3DSwitcher <port> <command>"); Console.WriteLine("Where <port> is the number of the COM port the headset is attached to"); Console.WriteLine("and <command> is the command which can be 3DOn or 3DOff"); } }