日期:2014-05-17 浏览次数:21223 次
本人原创作品 希望提供给大家学习 共同学习 共同进步
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace SerialPortDebuggingTools
{
public partial class SerialPortDebugging : Form
{
public SerialPortDebugging()
{
InitializeComponent();
}
public byte[] recb;
delegate void SetTextCallback(string text, int count);
public bool song16jinz = false;
/// <summary>
/// 代理方法给UI输出值
/// </summary>
/// <param name="text"></param>
private void SetText(string text, int count)
{
if (this.richTextBoxjieshou.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text, count });
}
else { if (this.checkBoxenter.Checked) { RemoveText(); this.richTextBoxjieshou.AppendText(text + "\r\n"); } else { RemoveText(); this.richTextBoxjieshou.AppendText(text); } textBoxRx.Text = string.Format("RX:{0}", JShou(count)); }
}
public void RemoveText()
{
int count=richTextBoxjieshou.Lines.Length;
if (count>=18)
{
richTextBoxjieshou.Text = "";
}
}
/// <summary>
/// 打开串口或者关闭串口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void butCorO_Click(object sender, EventArgs e)
{
if (SerialProtTools.IsOpen)
{
if (!this.checkBoxzhouqi.Checked)
{
SerialProtTools.Close(); this.butCorO.Text = "打开串口";
this.pictureBoxEnabled.Image = global::SerialPortDebuggingTools.Properties.Resources.colse;
this.textBoxstatus.Text = SerialProtTools.PortName + " COLSED," + SerialProtTools.BaudRate + "," + this.comboBoxJyw.Text + "," + SerialProtTools.DataBits + "," + this.comboBoxTzw.Text;
}
else
{
MessageBox.Show("请先关闭自动发送才能关闭串口", "温馨提示!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}
else
{
try
{
SerialProtTools.Open();
this.butCorO.Text = "关闭串口";
this.pictureBoxEnabled.Image = global::SerialPortDebuggingTools.Properties.Resources.open;
this.textBoxstatus.Text = SerialProtTools.PortName + " OPENED," + SerialProtTools.BaudRate + "," + this.comboBoxJyw.Text + "," + SerialProtTools.DataBits + "," + this.comboBoxTzw.Text;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "温馨提示!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
this.butCorO.Text = "打开串口";
this.pictureBoxEnabled.Image = global::SerialPortDebuggingTools.Properties.Resources.colse;
this.textBoxstatus.Text = SerialProtTools.PortName + " COLSED," + SerialProtTools.BaudRate + "," + this.comboBoxJyw.Text + "," + SerialProtTools.DataBits + "," + this.comboBoxTzw.Text;
}
}
}
/// <summary>
/// 初始化串口
/// </summary>
public void initSerialProt()
{
bool YmY = false;
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
this.comboBoxserialPort.Items.Add(port);
YmY = true;
}
if (YmY)
{
this.comboBoxserial