示例#1
0
    /// <summary>
    /// Sends a packet to the arduino.
    /// </summary>
    /// <param name="cmd">The command number.</param>
    /// <param name="buffer">The bytearray to be sent.</param>
    public void Send(int cmd, ArduinoDataType dataType, byte[] buffer)
    {
        // build serial packet
        int checksum = Crc16.ComputeChecksum(buffer);

        byte[] serialPacket = new byte[buffer.Length + headerSize];

        serialPacket[0] = (byte)(buffer.Length & 0xff);
        serialPacket[1] = (byte)((buffer.Length >> 8) & 0xff);
        serialPacket[2] = (byte)(checksum & 0xff);
        serialPacket[3] = (byte)((checksum >> 8) & 0xff);
        serialPacket[4] = (byte)cmd;
        serialPacket[5] = (byte)dataType;

        Array.Copy(buffer, 0, serialPacket, headerSize, buffer.Length);

        // encode packet
        byte[] encodedBuffer     = new byte[COBS.GetEncodedBufferSize(serialPacket.Length)];
        int    encodedBufferSize = COBS.Encode(ref serialPacket, serialPacket.Length, ref encodedBuffer);

        Array.Resize(ref encodedBuffer, encodedBufferSize + 1);
        encodedBuffer[encodedBufferSize] = 0;

        // add msg to outgoing queue
        lock (sendLockObject)
        {
            sendPackets.Enqueue(encodedBuffer);
        }
    }
        private void SendDisplayString(string displayString)
        {
            if (displayString == null || String.Equals(displayString, _lastDisplayStringSent, StringComparison.Ordinal))
            {
                return;
            }

            lock (_serialPortLock)
            {
                try
                {
                    if (_serialPort == null || !_serialPort.IsOpen || displayString == null)
                    {
                        return;
                    }
                    var cobsEncodedPacket = COBS.Encode(Encoding.ASCII.GetBytes(displayString));
                    _serialPort.Write(cobsEncodedPacket, offset: 0, count: cobsEncodedPacket.Length);
                    _serialPort.Write(new byte[] { 0 }, 0, 1);
                    _serialPort.BaseStream.Flush();
                    _lastDisplayStringSent = displayString;
                    _bytesSentSignal.State = cobsEncodedPacket.Length;
                }
                catch (Exception e)
                {
                    _log.Error(e.Message, e);
                }
            }
        }
示例#3
0
        public void TestEncodingMaxSize()
        {
            // Create an array of maximum allowed size
            var maxSizeArray = new byte[254];

            // Attempt to encode the array
            var result = COBS.Encode(maxSizeArray);

            // Assert the array was created correctly
            Assert.True(result.All(c => c == 1));
        }
示例#4
0
 public void TestEncoding()
 {
     Assert.True(encoded1.SequenceEqual(COBS.Encode(decoded1)));
     Assert.True(encoded2.SequenceEqual(COBS.Encode(decoded2)));
     Assert.True(encoded3.SequenceEqual(COBS.Encode(decoded3)));
     Assert.True(encoded4.SequenceEqual(COBS.Encode(decoded4)));
     Assert.True(encoded5.SequenceEqual(COBS.Encode(decoded5)));
     Assert.True(encoded6.SequenceEqual(COBS.Encode(decoded6)));
     Assert.True(encoded7.SequenceEqual(COBS.Encode(decoded7)));
     Assert.True(encoded8.SequenceEqual(COBS.Encode(decoded8)));
     Assert.True(encoded9.SequenceEqual(COBS.Encode(decoded9)));
 }
        private void SendDrawingCommands(string svgPathString)
        {
            lock (_serialPortLock)
            {
                try
                {
                    if (_serialPort == null || !_serialPort.IsOpen || svgPathString == null)
                    {
                        return;
                    }
                    var drawPoints = new SVGPathToVectorScopePointsListConverter(bezierCurveInterpolationSteps: BEZIER_CURVE_INTERPOLATION_STEPS)
                                     .ConvertToDrawPoints(svgPathString)
                                     .ApplyCentering(_config.Centering)
                                     .ApplyInversion(VIEWBOX_WIDTH, VIEWBOX_HEIGHT, invertX: false, invertY: true)
                                     .ApplyRotation(VIEWBOX_WIDTH / 2.0, VIEWBOX_HEIGHT / 2.0, _config.RotationDegrees)
                                     .ApplyScaling(_config.Scaling.ScaleX, _config.Scaling.ScaleY, VIEWBOX_WIDTH, VIEWBOX_HEIGHT)
                                     .ApplyCalibration(_config.XAxisCalibrationData, _config.YAxisCalibrationData)
                                     .ApplyClipping(VIEWBOX_WIDTH, VIEWBOX_HEIGHT)
                    ;

                    var drawPointsAsBytes = drawPoints.Select(x => (byte[])x).SelectMany(x => x).ToArray();

                    var cobsEncodedPacket = COBS.Encode(drawPointsAsBytes);

                    _serialPort.Write(cobsEncodedPacket, offset: 0, count: cobsEncodedPacket.Length);
                    _serialPort.Write(new byte[] { 0 }, 0, 1);
                    _serialPort.BaseStream.Flush();

                    _numDrawPointsSentSignal.State = drawPoints.Count();
                    _bytesSent.State = cobsEncodedPacket.Length;
                }
                catch (Exception e)
                {
                    _log.Error(e.Message, e);
                }
            }
        }
示例#6
0
 public void Encoding8()
 {
     byte[] expected8 = COBS.Encode(decoded8);
     Assert.IsTrue(expected8.SequenceEqual(encoded8));
 }
示例#7
0
 public void TestEncodingEmpty()
 {
     Assert.True(new List <byte>().SequenceEqual(COBS.Encode(new List <byte>())));
 }
示例#8
0
 public void TestEncodingNull()
 {
     Assert.Null(COBS.Encode(null));
 }
示例#9
0
 public void Encoding9()
 {
     byte[] expected9 = COBS.Encode(decoded9);
     Assert.IsTrue(expected9.SequenceEqual(encoded9));
 }
示例#10
0
 public void Encoding10()
 {
     byte[] expected10 = COBS.Encode(decoded10);
     Assert.IsTrue(expected10.SequenceEqual(encoded10));
 }
示例#11
0
 public void Encoding7()
 {
     byte[] expected7 = COBS.Encode(decoded7);
     Assert.IsTrue(expected7.SequenceEqual(encoded7));
 }
示例#12
0
 public void Encoding1()
 {
     byte[] expected1 = COBS.Encode(decoded1);
     Assert.IsTrue(expected1.SequenceEqual(encoded1));
 }
示例#13
0
 public void Encoding6()
 {
     byte[] expected6 = COBS.Encode(decoded6);
     Assert.IsTrue(expected6.SequenceEqual(encoded6));
 }
示例#14
0
 public void Encoding5()
 {
     byte[] expected5 = COBS.Encode(decoded5);
     Assert.IsTrue(expected5.SequenceEqual(encoded5));
 }
示例#15
0
 public void Encoding4()
 {
     byte[] expected4 = COBS.Encode(decoded4);
     Assert.IsTrue(expected4.SequenceEqual(encoded4));
 }
示例#16
0
 public void Encoding3()
 {
     byte[] expected3 = COBS.Encode(decoded3);
     Assert.IsTrue(expected3.SequenceEqual(encoded3));
 }
示例#17
0
 public void Encoding2()
 {
     byte[] expected2 = COBS.Encode(decoded2);
     Assert.IsTrue(expected2.SequenceEqual(encoded2));
 }