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 Microsoft.Win32;
using System.Threading;
namespace SerialPort_Assistant
{
public partial class Form1:Form
{
SerialPort serialPort;
System.Timers.Timer timerSendAuto;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cbComList.DataSource = GetComList();
cbBaudRateList.DataSource = GetRautBitsList();
cbParityList.DataSource = GetParityList();
cbDataBitsList.DataSource = GetDataBitsList();
cbStopBitsList.DataSource = GetStopBitsList();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort != null)
if (serialPort.IsOpen)//澶勪簬鎵撳紑鐘舵€?
{
serialPort.Close();
serialPort = null;
}
if (timerSendAuto != null)
{
timerSendAuto.Close();
timerSendAuto.Dispose();
}
}
//淇″彿鐏?
private void picComState_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Brush brush;
if (serialPort == null)
brush = Brushes.Red;
else if (serialPort.IsOpen)
brush = Brushes.Green;
else
brush = Brushes.Red;
g.FillEllipse(brush, e.ClipRectangle);
}
//鎵撳紑銆佸叧闂覆鍙?
private void btnComOpen_Click(object sender, EventArgs e)
{
if (serialPort == null)
{
serialPort = new SerialPort();
serialPort.RtsEnable = true;
serialPort.ReceivedBytesThreshold = 1;
serialPort.ErrorReceived =new SerialErrorReceivedEventHandler(serialPort_ErrorReceived);
serialPort.DataReceived = new SerialDataReceivedEventHandler(serialPort_DataReceived);
}
if (serialPort.IsOpen)//澶勪簬鎵撳紑鐘舵€?
{
serialPort.Close();
if (timerSendAuto != null)
timerSendAuto.Stop();
}
else
{
try
{
serialPort.PortName = cbComList.Text.Trim();
serialPort.BaudRate = int.Parse(cbBaudRateList.Text);
serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), cbParityList.Text);
serialPort.DataBits = int.Parse(cbDataBitsList.Text);
serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cbStopBitsList.Text);
serialPort.ReadBufferSize = serialPort.WriteBufferSize = 100;
serialPort.ReadTimeout = serialPort.WriteTimeout = 1000;
serialPort.Open();
}
catch(Exception e1)
{
MessageBox.Show("鎵撳紑涓插彛" cbComList.Text "澶辫触:" e1.Message);
}
}
btnComOpen.Text = serialPort.IsOpen ? "鍏抽棴涓插彛" : "鎵撳紑涓插彛";
picComState.Refresh();
}
/*串口接收*/
void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] buf = new byte[100];
int readcount = 0;
while (serialPort.BytesToRead > 0)
{
readcount = serialPort.Read(buf, 0, buf.Length);
if (!cbRevShowHex.Checked)
RevAppend(System.Text.Encoding.GetEncoding("gb2312").GetString(buf, 0, readcount));
else
for (int i = 0; i < readcount; i )
RevAppend(buf[i].ToString("X").PadLeft(2,'0') " ");
}
}
void serialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
MessageBox.Show("Error:" e.EventType.ToString());
}
private void btnSendHand_Click(object sender, EventArgs e)
{
if (serialPort == null)
return;
if (!serialPort.IsOpen)
return;
/*手动发送*/
if (!cbSendAuto.Checked)
{
SendBytes();
}
else
{
if (timerSendAuto == null)
{
timerSendAuto = new System.Timers.Timer();
timerSendAuto.Interval = double.Parse(tbSendPeriod.Text);
timerSendAuto.Elapsed =new System.Timers.ElapsedEventHandler(timerSendAuto_Elapsed);
}
timerSendAuto.Start();
}
}
void timerSendAuto_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
SendBytes();
}
private void cbSendAuto_CheckedChanged(object sender, EventArgs e)
{
if (!cbSendAuto.Checked)
{
if (timerSendAuto == null)
return;
timerSendAuto.Stop();
}
}
private void btnRevClear_Click(object sender, EventArgs e)
{
rtRev.Clear();
}
private void btnSendClear_Click(object sender, EventArgs e)
{
rtSend.Clear();
}
private void cbRevShowHex_CheckedChanged(object sender, EventArgs e)
{
if (cbRevShowHex.Checked)
{
byte[] buf = System.Text.Encoding.GetEncoding("gb2312").GetBytes(rtRev.Text);
rtRev.Clear();
for (int i = 0; i < buf.Length; i )
RevAppend(buf[i].ToString("X").PadLeft(2, '0') " ");
}
else
{
string str = rtRev.Text;
rtRev.Clear();
string[] arr = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in arr)
RevAppend(Convert.ToChar(Convert.ToByte(s, 16)).ToString());
}
}
private void cbSendHex_CheckedChanged(object sender, EventArgs e)
{
if (cbSendHex.Checked)
{
byte[] buf = System.Text.Encoding.GetEncoding("gb2312").GetBytes(rtSend.Text);
rtSend.Clear();
for (int i = 0; i < buf.Length; i )
SendAppend(buf[i].ToString("X").PadLeft(2, '0') " ");
}
else
{
string str = rtSend.Text;
rtSend.Clear();
string[] arr = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in arr)
SendAppend(Convert.ToChar(Convert.ToByte(s, 16)).ToString());
}
}
private void rtSend_KeyPress(object sender, KeyPressEventArgs e)
{
if (cbEnterSend.Checked)
{
if (e.KeyChar == '\r')
{
btnSendHand.PerformClick();
rtSend.Clear();
e.Handled = true;
}
}
}
void SendBytes()
{
byte[] buf;
string str = rtSend.Text;
/*16进制发送*/
if (cbSendHex.Checked)
{
/*输入处理*/
string[] arr = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
buf = new byte[arr.Length];
for (int i = 0; i < arr.Length; i )
buf[i] = Convert.ToByte(arr[i], 16);
}
else
buf = System.Text.Encoding.GetEncoding("gb2312").GetBytes(str);
serialPort.Write(buf, 0, buf.Length);
}
string[] GetComList()
{
RegistryKey subkey = Registry.LocalMachine.OpenSubKey("HARDWARE").OpenSubKey("DEVICEMAP").OpenSubKey("SERIALCOMM");
string[] strnames = subkey.GetValueNames();
string[] strkeys = new string[strnames.Length];
for (int i = 0; i < strnames.Length; i )
strkeys[i] = subkey.GetValue(strnames[i], "COMM-1").ToString();
subkey.Close();
return strkeys;
}
string[] GetParityList()
{
return Enum.GetNames(typeof(Parity));
}
string[] GetRautBitsList()
{
return new string[] { "110", "300", "600", "1200", "2400", "4800", "9600", "14400", "19200", "38400", "56000", "57600", "115200", "128000", "256000" };
}
string[] GetDataBitsList()
{
return new string[] { "8", "7", "6", "5", "4" };
}
string[] GetStopBitsList()
{
return Enum.GetNames(typeof(StopBits));//.Reverse().ToArray();
}
void RevAppend(string str)
{
if (rtRev.InvokeRequired)
this.Invoke(new MethodInvoker(delegate
{
rtRev.AppendText(str);
if (str == "FF ")
MessageBox.Show(str);
rtRev.SelectionStart = int.MaxValue;
rtRev.SelectionLength = 1;
rtRev.ScrollToCaret();
}));
else
{
rtRev.AppendText(str);
if (str == "FF ")
MessageBox.Show(str);
rtRev.SelectionStart = int.MaxValue;
rtRev.SelectionLength = 1;
rtRev.ScrollToCaret();
}
}
void SendAppend(string str)
{
if (rtSend.InvokeRequired)
this.Invoke(new MethodInvoker(delegate
{
rtSend.AppendText(str);
}));
else
{
rtSend.AppendText(str);
}
}
private void cbComList_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void picComState_Click(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
}
}
资源文件列表
WindowsApplication1/Backup/WindowsApplication1/Form1.cs , 10297
WindowsApplication1/Backup/WindowsApplication1/Form1.Designer.cs , 19068
WindowsApplication1/Backup/WindowsApplication1/Form1.resx , 5817
WindowsApplication1/Backup/WindowsApplication1/Program.cs , 481
WindowsApplication1/Backup/WindowsApplication1/Properties/AssemblyInfo.cs , 1186
WindowsApplication1/Backup/WindowsApplication1/Properties/Resources.Designer.cs , 2894
WindowsApplication1/Backup/WindowsApplication1/Properties/Resources.resx , 5612
WindowsApplication1/Backup/WindowsApplication1/Properties/Settings.Designer.cs , 1104
WindowsApplication1/Backup/WindowsApplication1/Properties/Settings.settings , 249
WindowsApplication1/Backup/WindowsApplication1/WindowsApplication1.csproj , 3245
WindowsApplication1/Backup/WindowsApplication1.sln , 946
WindowsApplication1/UpgradeLog.XML , 4789
WindowsApplication1/WindowsApplication1/bin/Debug/WindowsApplication1.vshost.exe , 21464
WindowsApplication1/WindowsApplication1/bin/Debug/WindowsApplication1.vshost.exe.manifest , 490
WindowsApplication1/WindowsApplication1/Form1.cs , 11050
WindowsApplication1/WindowsApplication1/Form1.Designer.cs , 19504
WindowsApplication1/WindowsApplication1/Form1.resx , 5817
WindowsApplication1/WindowsApplication1/obj/Debug/DesignTimeResolveAssemblyReferences.cache , 1229
WindowsApplication1/WindowsApplication1/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 6176
WindowsApplication1/WindowsApplication1/obj/Debug/TempPE/Properties.Resources.Designer.cs.dll , 4608
WindowsApplication1/WindowsApplication1/obj/Debug/WindowsApplication1.csproj.FileListAbsolute.txt , 1029
WindowsApplication1/WindowsApplication1/obj/WindowsApplication1.csproj.FileListAbsolute.txt , 873
WindowsApplication1/WindowsApplication1/Program.cs , 481
WindowsApplication1/WindowsApplication1/Properties/AssemblyInfo.cs , 1186
WindowsApplication1/WindowsApplication1/Properties/Resources.Designer.cs , 2877
WindowsApplication1/WindowsApplication1/Properties/Resources.resx , 5612
WindowsApplication1/WindowsApplication1/Properties/Settings.Designer.cs , 1118
WindowsApplication1/WindowsApplication1/Properties/Settings.settings , 249
WindowsApplication1/WindowsApplication1/WindowsApplication1.csproj , 3682
WindowsApplication1/WindowsApplication1.sln , 947
WindowsApplication1/WindowsApplication1.v11.suo , 36352
WindowsApplication1/_UpgradeReport_Files/UpgradeReport.css , 3348
WindowsApplication1/_UpgradeReport_Files/UpgradeReport.xslt , 12505
WindowsApplication1/_UpgradeReport_Files/UpgradeReport_Minus.gif , 69
WindowsApplication1/_UpgradeReport_Files/UpgradeReport_Plus.gif , 71
