实例介绍
【实例简介】
【实例截图】
【核心代码】
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;
using System.Collections;
using System.Threading;
namespace ComAssistant
{
public partial class Form1 : Form
{
/// <remarks></remarks>
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int ii = 0;
foreach (string s in SerialPort.GetPortNames())
{
if (ii >= 1)
{
comboBox1.Items.Add(s);
}
ii ;
}
if (SerialPort.GetPortNames().Length != 0)
{
comboBox1.Text = (string)comboBox1.Items[0];
}
string[] ss = new string[] { "9600", "19200", "57600", "115200" };
comboBox2.DataSource = ss;
comboBox3.DataSource = Enum.GetNames(typeof(Parity));
ss = new string[] { "5", "6", "7", "8" };
comboBox4.DataSource = ss;
comboBox4.Text = "8";
comboBox5.DataSource = Enum.GetNames(typeof(StopBits));
comboBox5.Text = Enum.Format(typeof(StopBits), StopBits.One, "G");
backgroundWorker1.RunWorkerAsync();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
}
else
{
serialPort1.PortName = comboBox1.Text;
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), comboBox3.Text);
serialPort1.DataBits = Int32.Parse(comboBox4.Text);
serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBox5.Text);
serialPort1.Encoding = Encoding.GetEncoding("Gb2312");
serialPort1.Open();
}
RefrespictureBox1();
}
catch (Exception)
{ }
}
private void button2_Click(object sender, EventArgs e)
{
try
{
string s = "";
if (checkBox1.Checked)
{
ArrayList al = MyClass.Str16ToArrayList(textBox2.Text);
byte[] by = new byte[al.Count];
int i = 0;
foreach (string stmp in al)
{
by[i] = Convert.ToByte(stmp, 16);
i ;
}
s = Encoding.GetEncoding("Gb2312").GetString(by);
}
else
{
s = textBox2.Text;
}
serialPort1.Write(s);
}
catch (Exception)
{ }
}
delegate void SetTextCallback(string text);
private void SetText(string text)
{
try
{
if (this.richTextBox2.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.richTextBox2.Text = text;
}
}
catch (Exception)
{
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
char[] c;
while (true)
{
try
{
if (serialPort1.IsOpen)
{
c = new char[serialPort1.BytesToRead];
serialPort1.Read(c, 0, c.Length);
if (c.Length > 0)
{
if (checkBox2.Checked)
{
byte[] by = Encoding.GetEncoding("Gb2312").GetBytes(c);
foreach (byte btmp in by)
{
string s = "00" string.Format("{0:X}", btmp);
s = s.Substring(s.Length - 2, 2);
SetText(new string(s.ToCharArray()));
SetText(" ");
}
}
else
{
SetText(new string(c));
}
}
}
}
catch (Exception) { }
Thread.Sleep(1);
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.Save();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
string s = "";
if (checkBox1.CheckState == CheckState.Checked)
{
byte[] by = Encoding.GetEncoding("Gb2312").GetBytes(textBox2.Text);
textBox2.Text = "";
foreach (byte btmp in by)
{
s = "00" string.Format("{0:X}", btmp);
textBox2.Text = s.Substring(s.Length - 2, 2);
textBox2.Text = " ";
}
}
else
{
ArrayList al = MyClass.Str16ToArrayList(textBox2.Text);
byte[] by = new byte[al.Count];
int i = 0;
foreach (string stmp in al)
{
by[i] = Convert.ToByte(stmp, 16);
i ;
}
textBox2.Text = Encoding.GetEncoding("Gb2312").GetString(by);
}
}
private void textBox1_Click(object sender, EventArgs e)
{
((TextBox)sender).SelectAll();
}
public void RefrespictureBox1()
{
if (serialPort1.IsOpen)
{
button1.BackColor = SystemColors.GradientActiveCaption;
button1.Text = "断开";
}
else
{
button1.BackColor = SystemColors.Control;
button1.Text = "连接";
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
richTextBox2.Text = "";
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (checkBox1.CheckState == CheckState.Checked)
{
if (!Uri.IsHexDigit(e.KeyChar) && e.KeyChar != ' ')
{
e.KeyChar = (char)0;
}
}
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论