//文件名:Form1.cs
//功能:查找可以使用的串口
//环境:VS2010 C#编程 C#窗口应用程序
//编者:张永辉 2013年3月19日
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading; //使用Thread类来创建和控制线程
using System.IO;
/*********************************************************************
包含的窗口
按钮1: 查找串口 -- butFindPort
按钮2: 打开串口 -- butSirPort
窗口 : 串口列表 -- listBox_Port
标签 : TXT显示 -- label_Port
*********************************************************************/
namespace sirport2
{
public partial class Form1 : Form
{
/// <summary
/// 窗口相关
/// </summary>
Thread Thread_ComxDeal;
public Form1()
{
InitializeComponent();
Thread_ComxDeal = new Thread(new ThreadStart(serialDeal));
Thread_ComxDeal.Start();
Thread.Sleep(1); //主线程休眠1MS
while (!Thread_ComxDeal.IsAlive) ; //等待oThread启动
}
//窗口起动事件
private void Form1_Load(object sender, EventArgs e)
{
}
//关闭事件
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (PortIsOpend == true)
{
PortIsOpend = false;
serialPort1.Close();
}
Thread_ComxDeal.Abort(); //终止oThread线程.
Thread_ComxDeal.Join(); //使主线程等待,直到oThread结束
}
/// <summary>
/// 串口操作
/// </summary>
string PortSelected; //当前选中的串口名:COMx
bool PortIsOpend = false;//串口当前状态
//【按钮:打开串口,关闭串口】
private void butSirPort_Click(object sender, EventArgs e)
{
if (PortIsOpend == false)
{
try
{
serialPort1.PortName = PortSelected; //PortSelected=="" 会异常
serialPort1.Open();
butSirPort.Text = "关闭串口";
label_Port.Text = "串口已经打开";
butFindPort.Enabled = false;
listBox_Port.Enabled = false;
PortIsOpend = true;
}
catch (Exception ee)
{
ee.GetType();
label_Port.Text = "连接失败";
butFindPort.Enabled = true;
listBox_Port.Enabled = true;
PortIsOpend = false;
}
}
else
{
serialPort1.Close();
butSirPort.Text = "打开串口";
label_Port.Text = "串口已经关闭";
butFindPort.Enabled = true;
listBox_Port.Enabled = true;
PortIsOpend = false;
}
}
//【按钮:查找串口】在listBox显示可以使用的串口
private void butFindPort_Click(object sender, EventArgs e)
{
int i = 1;
serialPort1.BaudRate = 115200;
listBox_Port.Items.Clear();
listBox_Port.Items.Add("NO COM");
butSirPort.Enabled = false;
while (i < 10)
{
if (Findport(i))
{
//找到可以使用的端口
//MessageBox.Show("open" + i.ToString() + "ok");
listBox_Port.Items.Add("COM"+i.ToString());
butSirPort.Enabled = true;
}
else
{
//MessageBox.Show("open" + i.ToString() + "no ok");
//listBox_Port.Items.Add("NO COM");
}
i++;
}
}
//【listBox_Port】从listBox选择要连接的串口号
private void listBox_Port_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox.SelectedObjectCollection v = listBox_Port.SelectedItems;
if (v.Count != 0)
{
PortSelected = v[0].ToString();
if (PortSelected == "NO COM")
{
PortSelected = "";
butSirPort.Enabled = false;
}
else
{
butSirPort.Enabled = true;
}
}
else
{
butSirPort.Enabled = false;
}
}
//返回该串口是否可以使用
private bool Findport(int portnum)
{
serialPort1.PortName = "COM" + portnum.ToString();
try
{
serialPort1.Open();
serialPort1.Close();
return true;
}
catch (Exception e)
{ //打开端口失败
e.GetType();
return false;
}
}
//向串口发送数据
public bool PortSends(string sc)
{
//char[] a = {0,1,2,3};
serialPort1.Write(sc);
return true;
}
//【串口收到数据事件】 每个字节会中断一次
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int a;
a = serialPort1.ReadChar();
string s = a.ToString();
MessageBox.Show(s);
}
//串口事件处理线程
public void serialDeal()
{
while(true)
{
Thread.Sleep(5);
}
}
}
}