Example #1
0
        public async Task <BaosFrame> GetDatapointDescriptions()
        {
            byte[] data  = { 0xF0, 0x03, 0x00, 0x00, 0x03, 0xE8 };
            var    frame = BaosFrame.Create(data);

            return(await SendFrame(frame));
        }
Example #2
0
        private async Task <BaosFrame> SendFrame(BaosFrame frame)
        {
            _serial.DisableEventHandler();
            _logger.LogDebug("Start sending frame...");
            try
            {
                await _serial.WriteFrame(frame);          //write frame

                var ackFrame = await _serial.ReadFrame(); // wait for ack

                if (ackFrame == null || ackFrame.GetType() != typeof(AckFrame))
                {
                    _logger.LogError($"Could not get ack frame");
                    return(null);
                }
                var response = await _serial.ReadFrame(); //read the response frame

                if (response == null)
                {
                    _logger.LogDebug("Could not get response...");
                    return(null);
                }

                await _serial.SendAck();

                _logger.LogDebug("End sending frame...");

                return(response);
            }
            finally
            {
                _serial.EnableEventHandler();
            }
        }
Example #3
0
        internal async Task WriteFrame(BaosFrame frame)
        {
            await _waitSemaphore.WaitAsync();

            try
            {
                _stream.DiscardOutBuffer();
                var buffer = frame.ToByteFrame();
                Logger.LogHexOut(buffer);
                await _stream.WriteAsync(buffer);

                if (frame is AckFrame)
                {
                    return;
                }

                ControlValueIndicator = !ControlValueIndicator;
            }
            catch (Exception e)
            {
                Logger.LogError($"Could not write frame {e}", e);
            }
            finally
            {
                _waitSemaphore.Release();
            }
        }
Example #4
0
        public async Task <BaosFrame> SetDatapointValue(ushort datapointId, ReadOnlyMemory <byte> value)
        {
            var dp = BitConverter.GetBytes(datapointId).Reverse().ToArray();

            byte[] data = { 0xF0, 0x06, dp[0], dp[1], 0x00, 0x01, dp[0], dp[1], 0x03, (byte)value.Length };
            byte[] dg   = new byte[data.Length + value.Length];

            Array.Copy(data, 0, dg, 0, data.Length);
            Array.Copy(value.ToArray(), 0, dg, data.Length, value.Length);

            var frame = BaosFrame.Create(dg);

            return(await SendFrame(frame));
        }
Example #5
0
        public async Task <IReadOnlyCollection <DatapointValue> > GetDatapointValue(short startDatapointId, short numberOfDatapoints)
        {
            var dp      = BitConverter.GetBytes(startDatapointId).Reverse().ToArray();
            var dpCount = BitConverter.GetBytes(numberOfDatapoints).Reverse().ToArray();

            byte[] data     = { 0xF0, 0x05, dp[0], dp[1], dpCount[0], dpCount[1], 0x00 };
            var    frame    = BaosFrame.Create(data);
            var    response = await SendFrame(frame);

            if (response != null)
            {
                return(ParseDatapointValues((LongFrame)response));
            }
            return(null);
        }
Example #6
0
        internal async Task <BaosFrame> ReadFrame()
        {
            await _waitSemaphore.WaitAsync();

            try
            {
                int firstChar = _stream.ReadByte();

                // Logger.LogDebug($"First char is {firstChar}");
                if (firstChar == BaosFrame.ShortFrameStart)
                {
                    // Logger.LogDebug("Start parsing short frame...");
                    Thread.Sleep(100);
                    byte[] shortFrame = new byte[5];
                    shortFrame[0] = (byte)firstChar;
                    var bytesRead = await _stream.ReadAsync(shortFrame, 1, 4);

                    if (bytesRead != 4)
                    {
                        Logger.LogDebug($"Could not parse short frame (read bytes {bytesRead}/4)");
                        return(null);
                    }
                    Logger.LogHexIn(shortFrame);

                    var frame = BaosFrame.FromByteArray(BaosFrameType.ShortFrame, Logger, shortFrame);

                    return(frame);
                }
                if (firstChar == BaosFrame.SingleCharFrame)
                {
                    Logger.LogHexIn(new byte[] { (byte)firstChar });
                    return(new AckFrame());
                }

                if (firstChar == BaosFrame.ControlFrameLongFrameStart)
                {
                    //    Logger.LogDebug("Start parsing long frame...");
                    Thread.Sleep(200);

                    byte[] headerBuffer = new byte[4];
                    headerBuffer[0] = (byte)firstChar;
                    var bytesRead = await _stream.ReadAsync(headerBuffer, 1, 3);

                    Logger.LogHexIn(headerBuffer);

                    if (bytesRead != 3)
                    {
                        Logger.LogDebug($"Could not read header...read {bytesRead}/3");
                        return(null);
                    }

                    if (headerBuffer[0] == 0x68 && headerBuffer[0] == headerBuffer[3] &&
                        headerBuffer[1] == headerBuffer[2])
                    {
                        int    lengthToRead = headerBuffer[1] + 2; //read with checksum and stop byte
                        byte[] data         = new byte[lengthToRead];
                        var    dataMemory   = new Memory <byte>(data);

                        bytesRead = await _stream.ReadAsync(dataMemory);

                        Logger.LogHexIn(dataMemory);

                        if (bytesRead != lengthToRead)
                        {
                            Logger.LogDebug($"Invalid length of read data...({bytesRead}/{lengthToRead})");
                            return(null); //invalid length of data read
                        }

                        if (data[data.Length - 1] != 0x16)
                        {
                            Logger.LogDebug("Invalid stop byte...");
                            return(null); //invalid stop byte
                        }

                        return(BaosFrame.FromSpan(BaosFrameType.LongFrame, Logger, headerBuffer.AsSpan(), dataMemory.Span));
                    }
                }
            }
            catch (IOException ioe)
            {
                Logger.LogError($"Could not read frame {ioe}", ioe);
                Close();
                Thread.Sleep(100);
                Open();
            }
            catch (Exception e)
            {
                Logger.LogError($"Could not read frame {e}", e);
            }
            finally
            {
                _waitSemaphore.Release();
            }
            return(null);
        }