第一个版本的串口调试器
来源:互联网 发布:中国次贷危机 知乎 编辑:程序博客网 时间:2024/04/29 06:06
虽然我试过了还不能在别的电脑上独立运行,但是本人从十年前走出校园到现在的第一个电脑软件的制作让我很是兴奋,本人也是第一次使用C#编程,(注:我是搞硬件的,只会单片机编程) 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.IO; namespace my_project{ public partial class Form_main : Form { string read_buff_string = ""; public Form_main() { InitializeComponent(); this.comboBox_com.SelectedIndex = 0; this.comboBox_baudrate.SelectedIndex = 2; //this.my_uart.PortName = "COM3"; this.my_uart.BaudRate = 9600; this.comboBox_start_code.SelectedIndex = 0; this.checkBox_add_start.Checked = true; this.checkBox_add_end.Checked = true; } public string HexToStr(string mHex) // 返回十六进制代表的字符串 { mHex = mHex.Replace(" ", ""); if (mHex.Length <= 1) return ""; byte[] vBytes = new byte[mHex.Length / 2]; for (int i = 0; i < (mHex.Length - mHex.Length%2); i += 2) if (!byte.TryParse(mHex.Substring(i, 2), System .Globalization .NumberStyles.HexNumber , null, out vBytes[i / 2])) vBytes[i / 2] = 0; return ASCIIEncoding.Default.GetString(vBytes); } //去掉发送数组中的空格 public string delspace(string putin) { string putout= ""; for(int i=0;i <putin.Length;i++) { if(putin[i]!= ' ') putout+=putin[i]; } return putout; } private void button_send_Click(object sender, EventArgs e) { string temps=""; if(checkBox_add_start.Checked==true) temps=delspace(this.comboBox_start_code .Text +this.text_send .Text); else temps=delspace(this.text_send .Text); byte[] tempb = new byte[temps.Length/2+2]; int j = 0; for (int i = 0; i < temps.Length-1; i = i + 2, j++) tempb[j] = Convert.ToByte(temps.Substring(i, 2), 16); byte lrc=0; for(int i=4;i<tempb.Length ;i++) { lrc +=tempb[i]; } tempb [temps .Length /2]=lrc; tempb [temps .Length /2+1]=10; this.my_uart.Write(tempb,0,tempb.Length ); } private void button_close_Click(object sender, EventArgs e) { this.my_uart.Close(); this.Close(); } private void comboBox_com_SelectedIndexChanged(object sender, EventArgs e) { this.my_uart.Close(); if(comboBox_com .SelectedIndex ==0) this.my_uart.PortName = "COM1"; else if (comboBox_com.SelectedIndex == 1) this.my_uart.PortName = "COM2"; else if (comboBox_com.SelectedIndex == 2) this.my_uart.PortName = "COM3"; else if (comboBox_com.SelectedIndex == 3) this.my_uart.PortName = "COM4"; try { if ( this.my_uart.IsOpen) { this.my_uart.Close(); this.my_uart.Open(); //打开串口 } else { this.my_uart.Open();//打开串口 } } catch (IOException ) { { //MessageBox.Show( ee.Message); MessageBox.Show("打开串口失败,请检查串口是否存在或被其他程序占用!"); } } catch (UnauthorizedAccessException) { { MessageBox.Show("打开串口失败,请检查串口是否存在或被其他程序占用!"); } } } //请求“System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, //PublicKeyToken=b77a5c561934e089”类型的权限已失败。 private void Form_main_Load(object sender, EventArgs e) { string[] ports = SerialPort.GetPortNames(); Control.CheckForIllegalCrossThreadCalls = false; this.label_port_list .Text = "有效串口号:"; foreach (string port in ports) { this.label_port_list .Text = this.label_port_list .Text+" " + port; } } private void button_clear_incept_Click(object sender, EventArgs e) { this.text_incept.Text = ""; read_buff_string = ""; } private void comboBox_baudrate_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox_baudrate.SelectedIndex == 0) this.my_uart.BaudRate = 2400; else if (comboBox_baudrate.SelectedIndex == 1) this.my_uart.BaudRate = 4800; else if (comboBox_baudrate.SelectedIndex == 2) this.my_uart.BaudRate = 9600; else if (comboBox_baudrate.SelectedIndex == 3) this.my_uart.BaudRate = 19200; else if (comboBox_baudrate.SelectedIndex == 4) this.my_uart.BaudRate = 38400; } public void my_uart_DataReceived(object sender, SerialDataReceivedEventArgs e) { byte[] buffer = new byte[my_uart.BytesToRead]; my_uart.Read(buffer, 0, buffer.Length); for (int i = 0; i < buffer.Length; i++) { read_buff_string = read_buff_string+ buffer[i].ToString("X2")+" "; } this.text_incept.Text = read_buff_string; // my_uart.Write(buffer, 0, buffer.Length); } private void text_incept_TextChanged(object sender, EventArgs e) { } private void button_clear_send_Click(object sender, EventArgs e) { this.text_send.Text = ""; } private void Form_main_SizeChanged(object sender, EventArgs e) { } }}
- 第一个版本的串口调试器
- PjComm:控制台版本的串口调试助手
- 发布DocX的第一个版本
- 我的第一个Android升级版本
- LintCode-第一个错误的代码版本
- LintCode 第一个错误的代码版本
- LintCode : 第一个错误的代码版本
- 第一个错误的代码版本
- LintCode : 第一个错误的代码版本
- 第一个错误的代码版本
- 第一个错误的代码版本
- LintCode : 第一个错误代码的版本
- 第一个错误的代码版本
- 第一个错误的代码版本
- 我的第一个VC++调试
- 我的第一个调试软件
- 第一个Java卡应用的调试
- 调试你的第一个javascript程序
- Asp.net常用的一些代码(转载
- IT學習力
- 我是一名计算机大学生
- 网商基本法:网上贸易,用网商币
- 使用Delphi编写DLL心得
- 第一个版本的串口调试器
- 基于pxa255的数码相框实现
- 文件快速拷贝工具FastCopy 使用教程
- 影响中国B2B电子商务发展的五大问题
- ARM---RO, RW, ZI
- 品牌是网络营销最后依归
- 兴趣与爱情
- B2C网站十宗罪
- VC++ 配置OpenGL