基于c#的一个串口给STM32发送图片的程序
基于c#的一个串口给STM32发送图片的程序
下面每天会更新一些这个小项目的具体实现方法。
初次接触c#,加上有c语言基础,首先自学了所有的基本语法。要从面向过程的语言向面对对象的语言转换还是比较困难,还好目前已经能写点小程序了。界面如下:
可以实现的功能1.根据默认路径在treeview中遍历文件夹和文件,若选择图片文件,则可以显示出图像信息。2.支持热插拔,识别串口信息,并且在串口中显示COM口。3.连接COM口后可以向stm32中发送图片的RGB像素信息。以实现在STM32上面的刷图。4.int文件用来储存你的默认路径,方便设置默认路径。
为了使项目更加有层次,在工程中建自一个类库的项目MyDll,用来调用库以及增加自定义控件。基础的项目如下MyDll为类库,Upper_LCD为form主程序:
这个项目中的核心就是串口通讯成功与否。如果想要通讯稳定,建议使用HID的协议,但是由于STM32上面的硬件限制,我在此项目中使用CP2012的USB转的串口。
首先建立解决方案(不多说)。
第一步:串口识别的自定义控件,如图:
用定时器定时检测是否有串口:
public void Check_USB_Connect()
{
string[] t = SerialPort.GetPortNames();
if ((beforeS != t.Length) && (!ComPort.IsOpen))
{
foreach (string com in t)
{
USBConBox.Items.Add(com);
}
}
beforeS = t.Length;
if (beforeS == 0) //判断是否找到串口
{
USBConBox.Text = "";
OpenBut.Enabled = false;
OpenBut.ForeColor = Color.Gray;
FindS.Text = "无串口";
FindS.ForeColor = Color.Red;
}
else
{
USBConBox.Text = t[0];
OpenBut.Enabled = true;
OpenBut.ForeColor = Color.Black;
FindS.Text = "找到串口";
FindS.ForeColor = Color.Green;
if (ComPort.IsOpen) //判断是否连接
{
OpenBut.Text = "关闭串口";
ComStause.Text = "串口已经连接";
ComStause.ForeColor = Color.Green;
}
else
{
OpenBut.Text = "打开串口";
ComStause.Text = "串口未连接";
ComStause.ForeColor = Color.Red;
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
Check_USB_Connect();
}
打开串口按键按下:
private void OpenBut_Click(object sender, EventArgs e)
{
if (ComPort.IsOpen)
{
ComPort.Close();
}
else
{
ComPort.PortName = USBConBox.Text;
while (!ComPort.IsOpen)
{
ComPort.BaudRate =Convert.ToInt32(BoundBox.Text);
ComPort.Open();
}
}
}
初始化给串口设置波特率,以及为定时器挂上终中断事件的部分:
public USBConnect()
{
InitializeComponent();
timer1.Tick += new EventHandler(timer1_Tick); //给timer挂起事件
timer1.Enabled = true;//使timer可用
timer1.Interval = 100; //设置时间间隔,以毫秒为单位
string[] bounds = { "2400", "4800", "9600", "14400", "19200", "38400", "56000", "57600", "115200" };
foreach (string bound in bounds)
{
BoundBox.Items.Add(bound);
}
BoundBox.SelectedIndex = 8;
}
总结:很简单的代码,容易出错的点就是定时器enable要设置为True
完整的代码如下(可以直接调用控件):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO.Ports;
namespace MyDll
{
public partial class USBConnect : UserControl
{
static int beforeS;
public USBConnect()
{
InitializeComponent();
timer1.Tick += new EventHandler(timer1_Tick); //给timer挂起事件
timer1.Enabled = true;//使timer可用
timer1.Interval = 100; //设置时间间隔,以毫秒为单位
string[] bounds = { "2400", "4800", "9600", "14400", "19200", "38400", "56000", "57600", "115200" };
foreach (string bound in bounds)
{
BoundBox.Items.Add(bound);
}
BoundBox.SelectedIndex = 8;
}
public void USBConBox_TextChanged(object sender, EventArgs e)
{
}
public void Check_USB_Connect()
{
string[] t = SerialPort.GetPortNames();
if ((beforeS != t.Length) && (!ComPort.IsOpen))
{
foreach (string com in t)
{
USBConBox.Items.Add(com);
}
}
beforeS = t.Length;
if (beforeS == 0) //判断是否找到串口
{
USBConBox.Text = "";
OpenBut.Enabled = false;
OpenBut.ForeColor = Color.Gray;
FindS.Text = "无串口";
FindS.ForeColor = Color.Red;
}
else
{
USBConBox.Text = t[0];
OpenBut.Enabled = true;
OpenBut.ForeColor = Color.Black;
FindS.Text = "找到串口";
FindS.ForeColor = Color.Green;
if (ComPort.IsOpen) //判断是否连接
{
OpenBut.Text = "关闭串口";
ComStause.Text = "串口已经连接";
ComStause.ForeColor = Color.Green;
}
else
{
OpenBut.Text = "打开串口";
ComStause.Text = "串口未连接";
ComStause.ForeColor = Color.Red;
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
Check_USB_Connect();
}
private void OpenBut_Click(object sender, EventArgs e)
{
if (ComPort.IsOpen)
{
ComPort.Close();
}
else
{
ComPort.PortName = USBConBox.Text;
while (!ComPort.IsOpen)
{
ComPort.BaudRate =Convert.ToInt32(BoundBox.Text);
ComPort.Open();
}
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void USBConBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}