public int Read(byte[] buf, int offset, int len) { if (!fd.HasValue || len > READING_BUFFER_SIZE) { throw new Exception(); } int res = ComWrapper.com_read(fd.Value, readingBuffer, len); if (res != -1) { Marshal.Copy(readingBuffer, buf, offset, res); } else { throw new Exception(); } return(res); }
private void StartReading() { if (!fd.HasValue) { throw new Exception(); } while (true) { CancellationToken.ThrowIfCancellationRequested(); int res = ComWrapper.com_read(fd.Value, readingBuffer, READING_BUFFER_SIZE); if (res != -1) { byte[] buf = new byte[res]; Marshal.Copy(readingBuffer, buf, 0, res); OnDataReceived(buf); } Thread.Sleep(50); } }