收藏 分销(赏)

自己用C写的一个定时接收的串口调试软件-所有代码.doc

上传人:a199****6536 文档编号:2776738 上传时间:2024-06-05 格式:DOC 页数:7 大小:145.04KB 下载积分:6 金币
下载 相关 举报
自己用C写的一个定时接收的串口调试软件-所有代码.doc_第1页
第1页 / 共7页
自己用C写的一个定时接收的串口调试软件-所有代码.doc_第2页
第2页 / 共7页


点击查看更多>>
资源描述
(完整版)自己用C写的一个定时接收的串口调试软件,所有代码 以下为所有的原代码: using System; using System。Collections.Generic; using System。ComponentModel; using System.Data; using System.Drawing; using System。Linq; using System。Text; using System。Threading; //添线程引用 using System.IO。Ports; //添加串口端口引用 using System。Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Control。CheckForIllegalCrossThreadCalls = false; //处理线程错误问题 // string[] PortName = System.IO。Ports.SerialPort。GetPortNames(); // 获取串口名字 string[] PortName = SerialPort.GetPortNames(); // 获取串口名字 using System。IO.Ports; 添加串口端口引用 if (PortName.Length == 0) { MessageBox.Show("请检查串口连接是否存在问题”); } else { int i; for (i = 0; i 〈 PortName.Length; i++) { comboBox1。Items。Add(PortName[i]); } foreach (SerialPort1Baudset rate in Enum.GetValues(typeof(SerialPort1Baudset))) { comboBox2。Items.Add(((int)rate).ToString()); } foreach (SerialPortDatabitsset bitset in Enum。GetValues(typeof(SerialPortDatabitsset))) { comboBox3。Items。Add(((int)bitset)。ToString()); } foreach(string str in Enum.GetNames(typeof(Parityset))) { comboBox4.Items。Add(str); } foreach (string Sbset in Enum.GetNames(typeof(StopBits))) { comboBox5。Items。Add(Sbset); } // 串口端口设置 comboBox1。SelectedIndex = 0; //boBox1.SelectedIndex = PortName。Length-1; this。comboBox2.SelectedIndex = 8; this。comboBox3。SelectedIndex = 3; boBox4.SelectedIndex = 0; this。comboBox5.SelectedIndex = 1; // 端口初始化 ResetPort(); } } private void ResetPort() { // this.serialPort1.BreakState = true; //中断状态 serialPort1.Encoding = Encoding.GetEncoding("GB2312"); serialPort1。PortName = comboBox1。Text; serialPort1.BaudRate = int。Parse(comboBox2。Text); serialPort1。DataBits = int.Parse(comboBox3。Text); serialPort1.Parity = (Parity)Enum.Parse(typeof(Parityset), boBox4。Text); // serialPort1.StopBits = (StopBits)Enum.Parse(typeof(Soptbitset), this。comboBox5.Text); serialPort1.ReadTimeout = 500; serialPort1。WriteTimeout = 1000; try { serialPort1。Open(); linkLabel1.Text = ”串口已连接"; timer1。Enabled = true; } catch { MessageBox。Show(”串口使用中,请选其它端口 \n ", ”操作提示"); linkLabel1。Text = "串口未连接”; } } private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) //选择波特率 { serialPort1。BaudRate = int.Parse(comboBox2。Text); } public enum SerialPort1Baudset : int //波特率参数 { BaudRate_75 = 75, BaudRate_110 = 110, BaudRate_150 = 150, BaudRate_300 = 300, BaudRate_600 = 600, BaudRate_1200 = 1200, BaudRate_2400 = 2400, BaudRate_4800 = 4800, BaudRate_9600 = 9600, BaudRate_14400 = 14400, BaudRate_19200 = 19200, BaudRate_28800 = 28800, BaudRate_38400 = 38400, BaudRate_56000 = 56000, BaudRate_57600 = 57600, BaudRate_115200 = 115200, BaudRate_128000 = 128000, BaudRate_230400 = 230400, BaudRate_256000 = 256000 } private void button3_Click(object sender, EventArgs e) //退出程序 { Application。Exit(); } private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) //选择数据位 { serialPort1。DataBits = int.Parse(comboBox3.Text); //用于将数字字符串转换为指定的数字类型 } public enum SerialPortDatabitsset : int { FiveBits = 5, SixBits = 6, SeventBits = 7, EightBits = 8 } private void comboBox4_SelectedIndexChanged(object sender, EventArgs e) //校验位 { serialPort1。Parity = (Parity)Enum.Parse(typeof(Parityset), this。comboBox4。Text); //serialPort1。Parity = (Parity)Enum.Parse(typeof(Parityset), comboBox4.SelectedItem.ToString()); } public enum Parityset { None, odd, Even, Mark, Space } private void comboBox5_SelectedIndexChanged(object sender, EventArgs e) //停止位 { serialPort1。StopBits = (StopBits)Enum.Parse(typeof(StopBits), this。comboBox5.Text); } /*public enum Soptbitset { None, One, Two, OnePointFive }*/ public string stopbits(int i) { string strstop; switch (i) { case 0: strstop = ”One"; break; case 1: strstop = ”Two”; break; case 2: strstop = "OnePointFive”; break; default: strstop = ”One"; break; } return strstop; } private void button2_Click(object sender, EventArgs e) //关闭 { serialPort1。Close(); linkLabel1。Text = ”串口未连接"; timer1。Enabled=false; } private void button1_Click(object sender, EventArgs e) { if (serialPort1。IsOpen) { MessageBox。Show("串口已经连接”); return; } else { ResetPort(); } } private void button5_Click(object sender, EventArgs e) //串口数据发送 { if(textBox2.Text =="") { MessageBox。Show("发送区内数据不能为空"); return; } try { SendStringData(serialPort1); } catch (Exception el) { MessageBox。Show(el。Message,"发送数据异常”); } // ReceiveData(serialPort1); //开启接收数据线程 } private void SendStringData(SerialPort serialPort1) //传输发送区数据 { serialPort1.Write(textBox2。Text); label11.Text = (int。Parse(label11.Text.Trim()) + 1)。ToString(); //统计发送区数据 组数 } private void ReceiveData(SerialPort serialPort1) //开启接收线程 { Thread threadReceiveSub = new Thread(new ParameterizedThreadStart(AsyReceiveData)); threadReceiveSub.Start(serialPort1); } private void AsyReceiveData(object serialPortobj) //数据接收 { SerialPort serialPort = (SerialPort)serialPortobj; System.Threading。Thread。Sleep(500); try { textBox3.Text = serialPort.ReadExisting(); label10。Text = (int。Parse(label10。Text。Trim()) + 1)。ToString(); //统计接收区数据 组数 } catch (Exception e) { MessageBox.Show(e.Message, "接收数据异常"); //处理错误 } } private void button4_Click(object sender, EventArgs e) //清空接收数据 { label10.Text = ”0”; label11.Text =”0"; } private void button6_Click(object sender, EventArgs e) { textBox3。Text = ""; } private void checkBox1_CheckedChanged(object sender, EventArgs e) { } private void timer1_Tick(object sender, EventArgs e) { ReceiveData(serialPort1); //开启接收数据线程 } } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服