using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
public partial class MainWindow : Window
AldlFrame msg = new AldlFrame();
SerialPort Port = new SerialPort("COM1", 8192, Parity.None, 8, StopBits.One);
private void SerialPortList_SelectionChanged(object sender, SelectionChangedEventArgs e)
ConnectButton.IsEnabled = true;
private void SerialPortList_DropDownOpened(object sender, EventArgs e)
string[] ports = SerialPort.GetPortNames();
SerialPortList.Items.Clear();
foreach (string port in ports)
SerialPortList.Items.Add(port);
private void ConnectButton_Click(object sender, RoutedEventArgs e)
if((string)ConnectButton.Content == "Connect")
if(SerialPortList.Text != "")
SerialPortList.IsEnabled = false;
ConnectButton.Content = "Disconnect";
Port.PortName = SerialPortList.Text;
Port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
SerialPortList.IsEnabled = true;
ConnectButton.Content = "Connect";
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
SerialPort Port = (SerialPort)sender;
for (int i = Port.BytesToRead; i > 0; i--)
msg.AppendByte((byte)Port.ReadByte());
string s = msg.GetFrame();
Application.Current.Dispatcher.Invoke(new Action(() => { RxTextBox.AppendText("Rx: " + s + "\n"); }));
private void CrearTextBoxButton_Click(object sender, RoutedEventArgs e)
private static byte ByteCount = 0;
private static byte FramePayload = 0;
private static bool FrameValid = false;
private static Queue<byte[]> frames = new Queue<byte[]>();
public void AppendByte(byte data)
GetRawData[ByteCount++] = data;
if (ByteCount > 3) ProcessFrame();
private void ProcessFrame()
FramePayload = (byte)(GetRawData[1] - 0x55);
if(ByteCount == FramePayload + 3)
byte[] frame_buffer = new byte[FramePayload + 2];
for (byte b = 0; b < FramePayload + 2; b++)
frame_buffer[b] = GetRawData[b];
sum += GetRawData[FramePayload + 2];
frames.Enqueue(frame_buffer);
public static byte[] GetRawData { get; } = new byte[170];
byte[] frame = frames.Dequeue();
for (byte b = 0; b < frame.Length; b++)
message += frame[b].ToString("X2");