public void DiscardOutBuffer() { if (!fd.HasValue) { throw new Exception(); } ComWrapper.com_tcoflush(fd.Value); }
public void Close() { if (!fd.HasValue) { throw new Exception(); } cts.Cancel(); ComWrapper.com_close(fd.Value); Marshal.FreeHGlobal(readingBuffer); }
public void Write(byte[] buf, int offset, int len) { if (!fd.HasValue) { throw new Exception(); } IntPtr ptr = Marshal.AllocHGlobal(len); Marshal.Copy(buf, offset, ptr, len); ComWrapper.com_write(fd.Value, ptr, len); Marshal.FreeHGlobal(ptr); }
public void Open() { // open serial port int fd = ComWrapper.com_open(portName); if (fd == -1) { throw new Exception($"failed to open port ({portName})"); } ComWrapper.com_set_interface_attribs(fd, baudRate, dataBits, (int)parity, (int)stopBits); // start reading //Task.Run((Action)StartReading, CancellationToken); this.fd = fd; }
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); } }