protected virtual void FixComunicationProblem() { while (_port.BytesToRead > 0) { int temp; temp = _port.ReadByte(); } }
private byte[] ReadResponse() { byte[] response = new byte[12]; byte value; int index = 0; do { if (serialPort.BytesToRead == 0) { Console.WriteLine("No data available"); Thread.Sleep(50); break; } value = (byte)serialPort.ReadByte(); if (value == 0x7E) //new response { index = 0; } response[index++] = value; }while (value != 0xEF); return(response); }
private byte[] ReceiveSysEx(int timeoutSeconds) { var storedInputData = new List<byte>(); bool parsingSysex = false; var startTime = DateTime.UtcNow.Ticks; _sp.ReadTimeout = timeoutSeconds * 1000; while (new TimeSpan(DateTime.UtcNow.Ticks - startTime).Seconds < timeoutSeconds) { lock (this) { int inputData = _sp.ReadByte(); if (parsingSysex) if (inputData == SysExMessage.END_SYSEX) return storedInputData.ToArray(); else storedInputData.Add((byte)inputData); else if (inputData == SysExMessage.START_SYSEX) parsingSysex = true; } } throw new TimeoutException(); }
byte ReadResponse(byte numbytes, byte timeout = 200) { byte counter = 0; bufferLen = 0; int avail; //100 != 0 && 200 != 0 while ((bufferLen != numbytes) && (timeout != counter)) { avail = serialPort.BytesToRead; if (avail <= 0) { Thread.Sleep(1); counter++; continue; } counter = 0; // there's a byte! camerabuff[bufferLen++] = (byte)serialPort.ReadByte(); // hwSerial->read(); //Console.WriteLine($"{avail}: A byte! {camerabuff[bufferLen - 1]}"); } return(bufferLen); }
public int ReadByte() { var b = _logged.ReadByte(); if (_log.IsEnabled(LogLevel.Trace)) { _log.LogTrace("Received : {received}", _byteFormatter.FormatReceivedSingleByte(b)); } return(b); }
static void ReadBytes(ISerialPort port, int count, ref byte[] buffer) { int bytesRead = 0; while (isConnected && (bytesRead < count)) { try { buffer[bytesRead] = port.ReadByte(); bytesRead++; } catch (TimeoutException) { // Continue trying to read while connected } } }
/// <remarks>The port must be open before the call</remarks> public async Task <byte[]> ReadAsync(CancellationToken cancellationToken) { var received = new List <byte>(); var captureBegan = false; var captureEnd = false; try { // ReSharper disable once LoopVariableIsNeverChangedInsideLoop while (!captureEnd) { await Task.Run(() => { var nextByte = _serialPort.ReadByte(); if (nextByte == -1) { captureEnd = true; return; } if (nextByte == _packetBeginning) { captureBegan = true; } if (nextByte == _packetEnd && captureBegan) { captureEnd = true; } received.Add(Convert.ToByte(nextByte)); }, cancellationToken); } return(received.ToArray()); } catch (Exception e) { throw new ReceptionException(e, received.ToArray()); } finally { _serialPort.DiscardInBuffer(); _serialPort.DiscardOutBuffer(); } }
public override async Task <List <Device> > Scan() { // TODO: Handle mid-scan disconnects var output = new List <Device>(); //if (timestampSize > 0b11) { // throw new ArgumentException($"timestampSize cannot be higher than {0b11}."); //} //if (timestampResolution > 0b1111) { // throw new ArgumentException($"timestampResolution cannot be higher than {0b1111}."); //} Console.Write(" Scan init"); try { port.Flush(); // Flush previous output port.WriteByte((byte)((byte)Command.Scan | timestampSize)); port.WriteByte((byte)(timestampResolution & 0xF)); //await Task.Delay(200); uint index_counter = 0; byte moreDevs; do { Console.Write(" 1111"); byte devClass = moreDevs = await port.ReadByte(); devClass &= 1; if ((moreDevs & 0x80) != 0) { var productID = await port.Read(4); var uniqueID = await port.Read(4); Console.WriteLine(BitConverter.ToUInt32(productID, 0)); if (devClass == (byte)DeviceClass.Buffer) { var shlAmount = await port.ReadByte(); var bufferSize = await port.ReadByte() | (await port.ReadByte() << 8); output.Add(new BufferDevice( this, BitConverter.ToUInt32(productID, 0), BitConverter.ToUInt32(uniqueID, 0), index_counter++, (ulong)bufferSize << shlAmount )); throw new NotImplementedException("Buffer devices are not implemented yet"); } else { Console.Write(" 2222"); var fieldCount = await port.ReadByte(); //Console.WriteLine($"> There are {fieldCount} fields"); var fields = new List <FieldType>(); for (int i = 0; i < fieldCount; i += 4) { var currentFieldTypes = await port.ReadByte(); for (int j = i; j < Math.Min(i + 4, fieldCount); j++) { //Console.WriteLine($"> New field {currentFieldTypes & 0b11}"); fields.Add((FieldType)(currentFieldTypes & 0b11)); currentFieldTypes >>= 2; } } Console.Write(" 3333"); output.Add(new PeripheralDevice( this, BitConverter.ToUInt32(productID, 0), BitConverter.ToUInt32(uniqueID, 0), index_counter++, fields )); } } } while (moreDevs != 0); } catch (InvalidOperationException) { throw new PortClosedException(); }; return(output); }
protected void ReadBytes(ISerialPort port, int count, ref byte[] buffer) { int bytesRead = 0; while (isConnected && (bytesRead < count)) { try { buffer[bytesRead] = port.ReadByte(); bytesRead++; } catch(TimeoutException) { // Continue trying to read while connected } } }