📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
namespace RS232Capture
{
public partial class Form1 : Form
{
Mutex m_ImageLock = new Mutex();
Mutex m_FrameLock = new Mutex();
Queue<byte[]> m_Frames = new Queue<byte[]>();
public Form1()
{
InitializeComponent();
m_ControlThread = new Thread(Listen);
m_ControlThread.Start();
m_DrawThread = new Thread(Draw);
m_DrawThread.Start();
m_Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
}
void pictureBox1_Paint(object sender, PaintEventArgs e)
{
m_ImageLock.WaitOne();
e.Graphics.DrawImage(m_Image, 0.0f, 0.0f);
m_ImageLock.ReleaseMutex();
}
public void Listen()
{
while (_continue)
{
if (ThePort.IsOpen)
{
try
{
byte[] pixels = new byte[ThePort.BytesToRead];
ThePort.Read(pixels, 0, ThePort.BytesToRead);
/*if (!File.Exists("data.out"))
{
FileStream f = File.Open("data.out", FileMode.OpenOrCreate);
f.Write(pixels, 0, pixels.Length);
f.Close();
}*/
m_FrameLock.WaitOne();
m_Frames.Enqueue(pixels);
m_FrameLock.ReleaseMutex();
Thread.Sleep(0);
}
catch (Exception e)
{
//do nothing
}
}
}
}
public void Draw()
{
while (_continue)
{
if (m_Frames.Count > 0)
{
m_FrameLock.WaitOne();
byte[] pixels = m_Frames.Dequeue();
m_FrameLock.ReleaseMutex();
DrawPixels(pixels);
Thread.Sleep(0);
}
}
}
private delegate void PixelDrawer(byte[] pixels);
private void DrawPixels(byte[] pixels)
{
for (int i = 0; i < pixels.Length; i++)
{
m_Image.SetPixel(m_CurrentPixel % m_ImageWidth, m_CurrentPixel / m_ImageWidth, Color.FromArgb(pixels[i], pixels[i], pixels[i]));
m_CurrentPixel = (m_CurrentPixel == m_ImageWidth * m_ImageHeight - 1) ? 0 : m_CurrentPixel + 1;
if (m_CurrentPixel == 0)
{
pictureBox1.Invalidate();
m_CurrentFrame++;
Thread.Sleep(100);
Graphics g = Graphics.FromImage(m_Image);
g.Clear(Color.White);
textBox2.Text = m_CurrentFrame.ToString();
}
//textBox1.Text += "." + pixels[i].ToString();
}
}
private int m_CurrentPixel = 0;
private Bitmap m_Image;
private int m_ImageWidth = 320;
private int m_ImageHeight = 240;
private Thread m_ControlThread;
private Thread m_DrawThread;
private bool _continue = true;
private int m_CurrentFrame = 0;
private void button1_Click(object sender, EventArgs e)
{
try
{
ThePort.Open();
byte[] handshake = new Byte[1];
handshake[0] = 3;
ThePort.Write(handshake, 0, 1);
}
catch (Exception ex)
{
textBox1.Text += "\n" + ex.Message;
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
ThePort.Close();
}
catch (Exception ex)
{
textBox1.Text += "\n" + ex.Message;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
_continue = false;
ThePort.Close();
ThePort.Dispose();
}
catch (Exception ex)
{
textBox1.Text += "\n" + ex.Message;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -