using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
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;
using System.Threading;
using System.IO.Ports;
namespace LED
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
SerialPort port = new SerialPort();
private void button_Click(object sender, RoutedEventArgs e)
{
string text = textBox.Text.Trim();
if (text != "")
{
byte[] h = new byte[] { 0xAA, 0x01, 0xBB, 0x51, 0x54 };
int sum = 0;
byte[] title = new byte[] { 0x01, 0x00, 0x00, 0x02, 0x63 };
byte[] text_ = Encoding.GetEncoding("GB2312").GetBytes(text);
byte end = 0xFF;
foreach (var i in title)
{
sum += i;
}
foreach (var i in text_)
{
sum += i;
}
byte sum_ = (byte)sum;
List<byte> Write_ = new List<byte>();
Write_.AddRange(h);
Write_.Add(sum_);
Write_.AddRange(title);
Write_.AddRange(text_);
Write_.Add(end);
byte[] WriteData = Write_.ToArray();
port.Open();
port.Write(WriteData, 0, WriteData.Length);
port.Close();
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
port.PortName = "COM6";
port.BaudRate = 9600;
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Environment.Exit(0);
}
}
}