PinkConnection2/Legacy Tools/PinkConnection2-TestApp/PinkConnection2-TestApp/MainForm.cs

115 lines
3.8 KiB
C#

using System;
using System.IO.Ports;
using Eto.Forms;
using Eto.Drawing;
namespace PinkConnection2_TestApp
{
public class MainForm : Form
{
SerialPort port = null;
byte b = 0;
TextBox CommandTextBox = new TextBox { MaxLength = 2 };
DropDown SerialPortDropDown = new DropDown();
public MainForm()
{
Title = "PinkConnection2 Test App";
Size = new Size(480, 300);
Maximizable = false;
Resizable = false;
foreach (string s in SerialPort.GetPortNames())
{
SerialPortDropDown.Items.Add(s);
}
SerialPortDropDown.SelectedValueChanged += (sender, e) => selectSerialPort();
DynamicLayout layout = new DynamicLayout { DefaultSpacing = new Size(10, 10), DefaultPadding = new Padding(5, 5, 5, 5) };
layout.BeginVertical();
layout.BeginHorizontal();
layout.Add(new Label { Text = "Serial Port" });
layout.EndHorizontal();
layout.BeginHorizontal();
layout.Add(SerialPortDropDown, true);
layout.EndHorizontal();
layout.EndVertical();
layout.BeginVertical();
layout.BeginHorizontal();
layout.Add(new Label { Text = "Channels" });
layout.EndHorizontal();
layout.BeginHorizontal();
layout.Add(new Button { Text = "Channel 1", Command = new Command((sender, e) => { if ((b&1)==1) b-=1; else b+=1; sendFrame(); }) }, true);
layout.Add(new Button { Text = "Channel 2", Command = new Command((sender, e) => { if ((b&2)==2) b-=2; else b+=2; sendFrame(); }) }, true);
layout.Add(new Button { Text = "Channel 3", Command = new Command((sender, e) => { if ((b&4)==4) b-=4; else b+=4; sendFrame(); }) }, true);
layout.Add(new Button { Text = "Channel 4", Command = new Command((sender, e) => { if ((b&8)==8) b-=8; else b+=8; sendFrame(); }) }, true);
layout.EndHorizontal();
layout.BeginHorizontal();
layout.Add(new Button { Text = "Channel 5", Command = new Command((sender, e) => { if ((b&16)==16) b-=16; else b+=16; sendFrame(); }) }, true);
layout.Add(new Button { Text = "Channel 6", Command = new Command((sender, e) => { if ((b&32)==32) b-=32; else b+=32; sendFrame(); }) }, true);
layout.Add(new Button { Text = "Channel 7", Command = new Command((sender, e) => { if ((b&64)==64) b-=64; else b+=64; sendFrame(); }) }, true);
layout.Add(new Button { Text = "Channel 8", Command = new Command((sender, e) => { if ((b&128)==128) b-=128; else b+=128; sendFrame(); }) }, true);
layout.EndHorizontal();
layout.EndVertical();
layout.BeginVertical();
layout.BeginHorizontal();
layout.Add(new Label { Text = "Send Command" });
layout.EndHorizontal();
layout.BeginHorizontal();
layout.Add(CommandTextBox, true);
layout.Add(new Button { Text = "Send", Command = new Command((sender, e) => sendRawSafe()) });
layout.EndHorizontal();
layout.EndVertical();
layout.BeginVertical();
layout.Add(null, true);
layout.EndVertical();
Content = layout;
}
void sendFrame()
{
if (port == null)
{
MessageBox.Show("Please select a serial port");
return;
}
byte byte1 = (byte)(64 | ((b & 8) | (b & 4) | (b & 2) | (b & 1)));
byte byte2 = (byte)(64 | (((b & 128) | (b & 64) | (b & 32) | (b & 16)) >> 4));
port.Write(((char)byte1).ToString());
port.Write(((char)byte2).ToString());
}
void sendRawSafe()
{
if (CommandTextBox.Text.Length != 2)
{
MessageBox.Show("Command length must not be less than 2");
return;
}
if (port == null)
{
MessageBox.Show("Please select a serial port");
return;
}
port.Write(CommandTextBox.Text);
}
void selectSerialPort()
{
if (port != null) port.Close();
port = new SerialPort((string)SerialPortDropDown.SelectedKey, 9600, Parity.None, 8, StopBits.One);
port.Open();
}
}
}