using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp3
{
public partial class Drawer : Form
{
private const int Unit_length = 32;
private int DrawStep = 8;
private const int Y_Max = 512;
private const int MaxStep = 33;
private const int MinStep = 1;
private const int StartPrint = 32;
private List<byte> DataList = new List<byte>();
private Pen TablePen = new Pen(Color.FromArgb(0x00, 0x00, 0x00));
private Pen LinesPen = new Pen(Color.FromArgb(0xa0, 0x00, 0x00));
public ShowWindow ShowMainWindow;
public HideWindow HideMainWindow;
public OpenPort OpenSerialPort;
public ClosePort CloseSerialPort;
public GetMainPos GetMainPos;
public GetMainWidth GetMainWidth;
private bool KeyShift, KeyShowMain, KeyHideMain, KeyExit, KeyOpen, KeyClose, KeyStepUp, KeyStepDown;
public Drawer()
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer|ControlStyles.UserPaint|
ControlStyles.AllPaintingInWmPaint,true);
this.UpdateStyles();
InitializeComponent();
}
public void AddDate(byte[] Data)
{
for (int i = 0; i < Data.Length; i++)
{
DataList.Add(Data[i]);
}
Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
String Str = "";
System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
e.Graphics.FillRectangle(Brushes.White, e.Graphics.ClipBounds);
for (int i = 0; i <= this.ClientRectangle.Width / Unit_length; i++)
{
e.Graphics.DrawLine(TablePen, StartPrint + i * Unit_length, StartPrint, StartPrint + i * Unit_length, StartPrint + Y_Max);
gp.AddString((i * (Unit_length / DrawStep)).ToString(), this.Font.FontFamily, (int)FontStyle.Regular, 12, new RectangleF(StartPrint + i * Unit_length - 7, this.ClientRectangle.Height - StartPrint + 4, 400, 50), null);
}
for (int i = 0; i <= this.ClientRectangle.Height / Unit_length; i++)
{
e.Graphics.DrawLine(TablePen, StartPrint, (i + 1) * Unit_length, this.ClientRectangle.Width, (i + 1) * Unit_length);
Str = ((16 - i) * 16).ToString("X");
Str = "0x" + (Str.Length == 1 ? Str + "0" : Str);
if (i == 0)
Str = "0xFF";
if (i == 17)
break;
gp.AddString(Str, this.Font.FontFamily, (int)FontStyle.Regular, 14, new RectangleF(0, StartPrint + i * Unit_length - 8, 400, 50), null);
}
e.Graphics.DrawPath(Pens.Black, gp);
if (DataList.Count - 1 >= (this.ClientRectangle.Width - StartPrint) / DrawStep)
{
DataList.RemoveRange(0, DataList.Count - (this.ClientRectangle.Width - StartPrint) / DrawStep - 1);
}
for (int i = 0; i < DataList.Count - 1; i++)
{
e.Graphics.DrawLine(LinesPen, StartPrint + i * DrawStep, 17 * Unit_length - DataList[i] * 2, StartPrint + (i + 1) * DrawStep, 17 * Unit_length - DataList[i + 1] * 2);
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (KeyShift)
{
if (KeyShowMain)
{
ShowMainWindow();
Rectangle Rect = Screen.GetWorkingArea(this);
SetWindow(Rect.Width - GetMainWidth(), new Point(GetMainWidth(), GetMainPos().Y));
KeyShowMain = false;
}
else
{
if (KeyOpen)
{
OpenSerialPort();
KeyOpen = false;
}
else
{
if (KeyClose)
{
CloseSerialPort();
KeyClose = false;
}
else
{
if (KeyExit)
{
KeyExit = false;
Close();
}
else
{
if (KeyStepUp)
{
if (DrawStep < MaxStep)
DrawStep++;
Invalidate();
KeyStepUp = false;
}
else
{
if (KeyStepDown)
{
if (DrawStep > MinStep)
DrawStep--;
Invalidate();
KeyStepDown = false;
}
else
{
if (KeyHideMain)
{
HideMainWindow();
Rectangle Rect = Screen.GetWorkingArea(this);
SetWindow(Rect.Width, new Point(0, GetMainPos().Y));
}
}
}
}
}
}
}
}
else
{
KeyClose = false;
KeyOpen = false;
KeyShowMain = false;
KeyExit = false;
KeyStepUp = false;
KeyStepDown = false;
}
KeyShift = false;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Shift)
KeyShift = true;
switch (e.KeyCode)
{
case Keys.S://显示主窗口
KeyShowMain = true;
break;
case Keys.E://退出波形显示
KeyExit = true;
break;
case Keys.O://打开串口
KeyOpen = true;
break;
case Keys.C://关闭串口
KeyClose = true;
break;
case Keys.Up://放大波形
KeyStepUp = true;
break;
case Keys.H://隐藏主窗口
KeyHideMain = true;
break;
case Keys.Down://缩小波形
KeyStepDown = true;
break;
default:
break;
}
}
public void SetWindow(int width, Point Pt)
{
int height = this.ClientRectangle.Height;
height = this.Height - height;
int BorderWeigh = this.Width - this.ClientRectangle.Width;
this.Size = new Size(width - (width - BorderWeigh) % Unit_length, height + Y_Max + StartPrint + Unit_length);
this.Location = Pt;
}
private void Drawer_Load(object sender, EventArgs e)
{
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp3
{
public delegate void ShowWindow();
public delegate void HideWindow();
public delegate void OpenPort();
public delegate void ClosePort();
public delegate Point GetMainPos();
public delegate int GetMainWidth();
public partial class Form1 : Form
{
Drawer Displayer;
public Form1()
{
InitializeComponent();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public void ClosePort()
{
try
{
serialPort1.Close();
}
catch (System.Exception)
{
}
}
public void OpenPort()
{
try
{
serialPort1.Open();
}
catch (System.Exception)
{
MessageBox.Show("串口打开失败,请检查", "错误");
}
}
private Point GetMyPos()
{
return this.Location;
}
public void ShowMe()
{
this.Show();
}
public void HideMe()
{
this.Hide();
}
int GetMyWidth()
{
return this.Width;
}
private void CreateNewDrawer()
{
Displayer = new Drawer();
Displayer.ShowMainWindow = new ShowWindow(ShowMe);
Displayer.HideMainWindow = new HideWindow(HideMe);
Displayer.GetMainPos = new GetMainPos(GetMyPos);
Displayer.CloseSerialPort = new ClosePort(ClosePort);
Displayer.OpenSerialPort = new OpenPort(OpenPort);
Displayer.GetMainWidth = new GetMainWidth(GetMyWidth);
Displayer.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 20; i++)
{
comboBox1.Items.Add("COM"+i);
}
comboBox1.Text = "COM1";
comboBox2.Items.Add("9600");
}
private void SearAndAdd(SerialPort serialPort ,ComboBox comboBox)
{
string MyString ;
string Buffer;
comboBox3.Items.Clear();
for (int i = 0; i < 5; i++)
{
try
{
Buffer = "COM" + i.ToString();
serialPort.PortName = Buffer;
serialPort.Open();
MyString = Buffer;
comboBox3.Items.Add(MyString);
serialPort.Close();
}
catch
{
}
}
comboBox3.SelectedItem = comboBox3.Items[0];
}
private void port_DataReceive(object sender, SerialDataReceivedEventArgs e)
{
if (!radioButton3.Checked)
{
string str = serialPort1.ReadExisting();
textBox2.AppendText(str+"\r\n");
}
else
{
byte data;
data=(byte)serialPort1.ReadByte();
byte[] Data=new byte[serialPort1.BytesToRead];
serialPort1.Read(Data, 0, Data.Length);
if (Displayer != null)
{
Displayer.AddDate(Data);
}
string str=Convert.ToString(data,16).ToUpper();
textBox2.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + "\r\n");
}
}
private void button1_Click(object sender, EventArgs e)
{
byte[] Date=new byte[1];
if (serialPort1.IsOpen)
{
if (textBox1.Text != "")
{
if (!radioButton1.Checked)
{
try
{
serialPort1.Write(textBox1.Text);
}
catch
{
MessageBox.Show("串口数据写入错误");
}
}
else
{
for (int i = 0; i < (textBox1.Text.Length-textBox1.Text.Length%2)/2; i++)
{
Date[0] = Convert.ToByte(textBox1.Text.Substring(i*2,2),16);
serialPort1.Write(Date, 0, 1);
}
if (textBox1.Text.Length % 2 != 0)
{
Date[0] = Convert.ToByte(textBox2.Text.Substring(textBox1.Text.Length - 1, 1),16);
serialPort1.Write(Date, 0, 1);
}
}
}
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
try
{
serialPort1.PortName=comboBox1.Text;
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
serialPort1.Open();
button2.Enabled = false;
button3.Enabled=true;
}
catch
{
MessageBox.Show("端口错误");
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
SearAndAdd(serialPort1, comboBox3);
}
private void timer1_Tick(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
if (Displayer == null)
{
CreateDisplayer();
}
else
{
if (Displayer.IsDisposed)
{
CreateDisplayer();
}
}
}
private void CreateDisplayer()
{
this.Left = 0;
CreateNewDrawer();
Rectangle Rect = Screen.GetWorkingArea(this);
Displayer.SetWindow(Rect.Width - this.Width, new Point(this.Width, this.Top));
}
}
}