Example #1
0
        /// <summary>
        /// Connect to a given reader or port with a specified timeout on the ping response
        /// </summary>
        /// <param name="deviceName">Name of Device</param>
        /// <param name="timeout">Timeout in ms</param>
        /// <returns></returns>
        public bool ConnectWithTimeout(string deviceName, int timeout)
        {
            Command        cmd          = new Ping();
            AutoResetEvent receivedResp = new AutoResetEvent(false);
            bool           success      = false;

            if (!conn.Connect(deviceName))
            {
                return(false);
            }

            Callback resp = (ResponseFrame frame, Exception e) =>
            {
                if (TcmpFrame.IsValidFrame(frame))
                {
                    success = true;
                }
                receivedResp.Set();
            };

            SendCommand(cmd, resp);
            receivedResp.WaitOne(timeout);

            if (success)
            {
                DeviceName = deviceName;
            }

            return(success);
        }
Example #2
0
        private void DataReceivedHandler(object sender, EventArgs e)
        {
            Debug.WriteLine("Data is being recieve");
            if (!conn.IsOpen() && responseCallback != null)
            {
                responseCallback(null, new HardwareException("Connection to device is not open"));
            }

            Debug.WriteLine($"     Before: {BitConverter.ToString(buffer.ToArray())}");

            if (conn.Read(this.buffer) == 0)
            {
                return;
            }

            Debug.WriteLine($"      After: {BitConverter.ToString(buffer.ToArray())}");

            ResponseFrame resp;

            buffer = TcmpFrame.RemoveEscapeCharacters(buffer.ToArray());

            for (int i = 0; i < buffer.Count; i++)
            {
                resp = null;
                try
                {
                    if (buffer[i] == 0x7E)
                    {
                        resp = ExtractFrame(i);

                        if (resp != null)
                        {
                            buffer.RemoveRange(0, resp.FrameLength + i);

                            Debug.WriteLine(string.Format("Command family: {0:X}, {1:X}\nResponse Code: {2:X}", resp.CommandFamily[0], resp.CommandFamily[1], resp.ResponseCode));
                            responseCallback?.Invoke(resp, null);
                            i -= 1;
                        }
                    }
                }
                catch (LackOfDataException exc)
                {
                    if (buffer.Count > 1 && buffer[buffer.Count - 1] == 0x7E)
                    {
                        responseCallback?.Invoke(null, exc);
                        FlushBuffer();
                    }
                }
                catch (LcsException exc)
                {
                    responseCallback?.Invoke(null, new LcsException(this.buffer.ToArray(), "There is an error in the length bytes since Len1+Len0+Lcs != 0"));
                }
                catch (HardwareException exc)
                {
                    responseCallback(null, new HardwareException("Connection to device is not open"));
                }
            }
        }
Example #3
0
        /// <summary>
        /// Checks if the frame given has a CRC error
        /// </summary>
        /// <param name="frame">Frame to be checked</param>
        /// <param name="crc">CRC to be compare to</param>
        /// <returns>True if there is a CRC error, false otherwise</returns>
        protected static bool HasCrcError(TcmpFrame frame, byte[] crc)
        {
            List <byte> data = new List <byte>(frame.ToArray());

            byte[] temp = CalculateCrc(data.GetRange(1, frame.FrameLength - 4));

            if (temp[0] != frame.Crc[0] || temp[1] != frame.Crc[1])
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #4
0
        /// <summary>
        /// Checks if the frame given has a LCS errors, CRC errors, or other errors
        /// </summary>
        /// <param name="frame"></param>
        /// <returns>True is frame has no errors, false otherwise</returns>
        public static bool IsValidFrame(TcmpFrame frame)
        {
            if (frame == null)
            {
                return(false);
            }

            byte[] contents = frame.ToArray();

            if (contents.Length < 10)
            {
                return(false);
            }

            if (contents[0] != 0x7E)
            {
                return(false);
            }

            if (HasLcsError(frame.Len1, frame.Len0, frame.Lcs))
            {
                return(false);
            }

            if (frame.Length + 5 != contents.Length)
            {
                return(false);
            }

            if (HasCrcError(frame, frame.Crc))
            {
                return(false);
            }

            return(true);
        }