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;
SerialPortList.IsEnabled = true;
ConnectButton.Content = "Connect";
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
SerialPort Port = (SerialPort)sender;
msg.AppendByte((byte)Port.ReadByte());
private static byte ByteCount = 0;
private static byte FramePayload = 0;
public void AppendByte(byte data)
GetRawData[ByteCount++] = data;
if (ByteCount > 3) ProcessFrame();
private void ProcessFrame()
FramePayload = GetRawData[1] - 0x55;
if(ByteCount == FramePayload + 3)
foreach (byte b in GetRawData)
MainWindow.RxTextBox.Append(Convert.ToString(GetRawData));
public static byte[] GetRawData { get; } = new byte[170];