Exemplo n.º 1
0
    private static bool TestSerialPort(SerialPort serialPort)
    {
        var escapedAck = EscapedBinaryProtocol.Write(_ack);

        serialPort.Write(escapedAck, 0, escapedAck.Length);
        var memory = new MemoryStream();
        var writer = new BinaryWriter(memory);

        try
        {
            while (true)
            {
                var read = serialPort.ReadByte();
                if (read >= 0)
                {
                    writer.Write((byte)read);
                }
            }
        }
        catch (TimeoutException)
        {
            memory.Position = 0;
            try
            {
                var message = EscapedBinaryProtocol.Read(memory);
                return(message.SequenceEqual(_ack));
            }
            catch
            {
                return(false);
            }
        }
    }
Exemplo n.º 2
0
    public void SetColors(RGBColor[] colors)
    {
        if (!_serial.IsOpen)
        {
            throw new InvalidOperationException("Color cannot be set. Serial device unavailable");
        }
        _lastColorAmount = colors.Length;
        var colorBytes = colors.SelectMany(c => c.ToByteArray()).ToArray();
        var converted  = EscapedBinaryProtocol.Write(colorBytes);

        _serial.Write(converted, 0, converted.Length);
    }