Exemplo n.º 1
0
            /// <summary>
            /// Write data to GPIOBoard
            /// </summary>
            /// <param name="SlaveID">GPIOBoard Station ID</param>
            /// <param name="FrameType">Frame type</param>
            /// <param name="Data">Transmitted data</param>
            /// <returns></returns>
            public async Task <byte[]> GPIOWriteAsync(byte SlaveID, byte FrameType, byte[] Data)
            {
                try
                {
                    await Writelock.WaitAsync();

                    if (IsOpen())
                    {
                        int         Retry = MAXRETRY;
                        List <byte> Frame = new List <byte> {
                            SlaveID, FrameType
                        };
                        if (Data != null)
                        {
                            Frame.AddRange(Data);
                        }
                        Frame.AddRange(SerialHelper.CalcCRC(Frame.ToArray()));
                        Frame.Insert(0, HEADER[0]);
                        Frame.Insert(1, HEADER[1]);
                        Frame.AddRange(TAIL);
                        while (Retry > 0)
                        {
                            await WriteAsync(Frame.ToArray());

                            byte[] ReceivedBytes;
                            ReceivedBytes = await ReadAsync();

                            if (ReceivedBytes != null)
                            {
                                if (ParseResponde(SlaveID, FrameType, ReceivedBytes))
                                {
                                    return(ReceivedBytes);
                                }
                            }
                            //await Serial.WriteAsync(Frame.ToArray());
                            await Task.Delay(20);

                            DiscardInBuffer();
                            Retry--;
                        }
                        return(null);
                    }
                    throw new Exception("COM Port has been closed");
                }
                catch (Exception ex)
                {
                    string s = ex.Message;
                    return(null);
                }
                finally
                {
                    Writelock.Release();
                }
            }
Exemplo n.º 2
0
 private bool CRC16ErrorCheck(byte[] Data)
 {
     if (Data.Count() < 2)
     {
         return(false);
     }
     try
     {
         int    Count   = Data.Length;
         byte   HighCRC = Data[Count - 1];
         byte   LowCRC  = Data[Count - 2];
         byte[] data    = new byte[Count - 2];
         Array.Copy(Data, 0, data, 0, Count - 2);
         var CalculatedCRC = SerialHelper.CalcCRC(data);
         return(HighCRC == CalculatedCRC[1] && LowCRC == CalculatedCRC[0]);
     }
     catch (Exception)
     {
         throw;
     }
 }