Exemplo n.º 1
0
        public int Read(byte[] buffer, int offset, int count)
        {
            if (!isOpen)
            {
                throw new InvalidOperationException("Serial Port Read - port not open");
            }
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer", Resources.GetResourceString("ArgumentNull_Buffer"));
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset", Resources.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", Resources.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (buffer.Length - offset < count)
            {
                throw new ArgumentException(Resources.GetResourceString("Argument_InvalidOffLen"));
            }
            int beginReadPos = readPos;

            byte [] tempReturnBuffer = new byte[count];

            if (readLen - readPos >= 1)
            {
                int min = (readLen - readPos < count) ? readLen - readPos : count;
                Buffer.BlockCopy(inBuffer, readPos, buffer, 0, min);
                readPos += min;
                if (min == count)
                {
                    if (readPos == readLen)
                    {
                        readPos = readLen = 0;                                          // just a check to see if we can reset buffer
                    }
                    return(count);
                }

                if (InBufferBytes == 0)
                {
                    return(min);
                }
            }

            int bytesLeftToRead = count - (readPos - beginReadPos);

            if (bytesLeftToRead + readLen >= inBuffer.Length)
            {
                ResizeBuffer();
            }

            int returnCount = internalSerialStream.Read(inBuffer, readLen, bytesLeftToRead);

            Buffer.BlockCopy(inBuffer, beginReadPos, buffer, offset, returnCount + (readPos - beginReadPos));
            readLen = readPos = 0;
            return(returnCount + readPos - beginReadPos);            // return the number of bytes we threw into the buffer plus what we had.
        }
Exemplo n.º 2
0
        private static void HidSharpMethod()
        {
            Console.WriteLine("HIDSharp looking for a USB Serial devices...");

            List <SerialDevice> devices = DeviceList.Local.GetSerialDevices().ToList();

            if (!devices.Any())
            {
                Console.WriteLine("No Devices Found");
                return;
            }

            foreach (SerialDevice device in devices)
            {
                Console.WriteLine($"Found {device}");

                if (device.DevicePath.Contains("GBA ST2"))
                {
                    Console.WriteLine($"Found Match! { device.GetFriendlyName()}");
                    if (billAcceptor == null)
                    {
                        billAcceptor = device;
                        break;
                    }
                }
            }

            if (billAcceptor == null)
            {
                Console.WriteLine("Bill Acceptor not found!");
                return;
            }

            StringBuilder builder;
            SerialStream  stream = billAcceptor.Open();

            using (stream)
            {
                while (!CancelationToken)
                {
                    byte[] buffer = new byte[8];
                    int    count  = 0;

                    try
                    {
                        count = stream.Read(buffer, 0, buffer.Length);
                    }
                    catch (TimeoutException e)
                    {
                        Console.WriteLine("Timeout - new session started");
                        continue;
                    }

                    if (count > 0)
                    {
                        string converted = Convert.ToBase64String(buffer);
                        Console.WriteLine($"Evaluating: {converted}");

                        switch (converted)
                        {
                        case "B/lPAj0BAZA=":
                        case "B/lPAj0EA5U=":
                        case "B/lPAj0EBJY=":
                        case "B/lPAj0BAI8=":
                            Console.WriteLine("10 kn entered");
                            break;

                        default:
                            ReactToUnknownBill();
                            break;
                        }

                        count = 0;
                    }
                }
            }

            Console.WriteLine("Closing session");
        }