Ejemplo n.º 1
0
 public void DiscardOutBuffer()
 {
     if (!fd.HasValue)
     {
         throw new Exception();
     }
     ComWrapper.com_tcoflush(fd.Value);
 }
Ejemplo n.º 2
0
 public void Close()
 {
     if (!fd.HasValue)
     {
         throw new Exception();
     }
     cts.Cancel();
     ComWrapper.com_close(fd.Value);
     Marshal.FreeHGlobal(readingBuffer);
 }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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;
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        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);
            }
        }