VS上利用C#实现一个简单的串口程序记录
一、背景
工作上需要利用串口往下位机写入数据,VC太老,正好借此机会来熟悉一直很想接触的VS之C#。
感谢Tony托尼哥的串口通信代码,感谢梦真的C#的技术支持。
二、正文
1、项目架构:(以我现有的知识认知来说)
一共有3个文件:
a、"Program.cs"保存的是主程序入口。
b、"Form1.Designer.cs"图形控件的实现。
c、"Form1.cs"里面实现的既是用户逻辑。
典型代码结构如下:
最先运行文件"Program.cs"内的主程序
namespace TB528E4PSE_APP
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new VOUTCTRL());-----------------------|
} |
} |
} |
然后接着运行"Form1.cs" 文件内的"VOUTCTRL()". |
namespace TB528E4PSE_APP |
{ |
public partial class VOUTCTRL : Form |
{ |
SerialPort serialPort; |
|
public VOUTCTRL() <---------------------------------------|
{
//IntializeComponent()函数在"Form1.Designer.cs"里实现,
//功能是初始化图形界面
//在图形界面操作时,该代码会被VS自动更改
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
//获取串口
String[] serialPortArray = SerialPort.GetPortNames();
if (serialPortArray != null && serialPortArray.Length != )
{
//对串口进行排序
Array.Sort<String>(serialPortArray);
foreach (String port in serialPortArray)
{
//添加到combox的item
if (port != null && port.Length != )
comboBox_SPort.Items.Add(port);
}
}
//设置初始显示的值
comboBox_SPort.SelectedIndex = ;
serialPort = new SerialPort();
} private void button_SPort_Click(object sender, EventArgs e)
{
// 在图形界面编辑时,双击Button控件,该函数由VS自动生成
// 打开窗口
if (button_SPort.Text == "Open")
{
if (serialPort.IsOpen)
serialPort.Close();
try
{
setPort();
serialPort.Open();
button_SPort.Text = "Close";
}
catch (Exception ex)
{
MessageBox.Show("Uable to open this serial port!\n");
}
}
else
{
button_SPort.Text = "Open";
serialPort.Close();
}
} private void trackBar_VOUT1_Scroll(object sender, EventArgs e)
{
// 在图形界面编辑时,双击trackBar类控件,该函数由VS自动生成
// 拖动trackBar_VOUT1控件实现的逻辑
/* 一些代码逻辑,就不详细贴了。
...
setSerialVOUT(_Channel_1, _LV0);
...
*/
} private void trackBar_VOUT2_Scroll(object sender, EventArgs e)
{
// 在图形界面编辑时,双击trackBar类控件,该函数由VS自动生成
// 拖动trackBar_VOUT2控件实现的逻辑
/* 一些代码逻辑,就不详细贴了。
...
setSerialVOUT(_Channel_2, _LV0);
...
*/
} private void setPort() // 初始化串口
{
try
{
serialPort.PortName = (String)(comboBox_SPort.SelectedItem);
serialPort.BaudRate = ;
serialPort.DataBits = ;
serialPort.StopBits = StopBits.One;
serialPort.Parity = Parity.None;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
} private void setSerialVOUT(byte channel, byte level)
{
byte[] buf = new byte[]; // C#创建数组的方法
buf[] = channel;
buf[] = level;
buf[] = (byte)''; if (serialPort != null && serialPort.IsOpen)
{
serialPort.Write(buf,,); // 向串口写入数据
}
}
}
} 文件"Form1.Designer.cs"里包含的函数"InitializeComponent();"
namespace TB528E4PSE_APP
{
partial class VOUTCTRL
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = \
new System.ComponentModel.ComponentResourceManager(typeof(VOUTCTRL));
this.trackBar_VOUT2 = new System.Windows.Forms.TrackBar();
this.trackBar_VOUT1 = new System.Windows.Forms.TrackBar();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.textBox_VOUT1 = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox_VOUT2 = new System.Windows.Forms.TextBox();
this.comboBox_SPort = new System.Windows.Forms.ComboBox();
this.label3 = new System.Windows.Forms.Label();
this.button_SPort = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)\
(this.trackBar_VOUT2)).BeginInit();
((System.ComponentModel.ISupportInitialize)\
(this.trackBar_VOUT1)).BeginInit();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// trackBar_VOUT2
//
this.trackBar_VOUT2.LargeChange = ;
this.trackBar_VOUT2.Location = new System.Drawing.Point(, );
this.trackBar_VOUT2.Maximum = ;
this.trackBar_VOUT2.Name = "trackBar_VOUT2";
this.trackBar_VOUT2.Size = new System.Drawing.Size(, );
this.trackBar_VOUT2.TabIndex = ;
this.trackBar_VOUT2.Scroll += new \
System.EventHandler(this.trackBar_VOUT2_Scroll);
//
// trackBar_VOUT1
//
this.trackBar_VOUT1.BackColor = System.Drawing.SystemColors.Control;
this.trackBar_VOUT1.LargeChange = ;
this.trackBar_VOUT1.Location = new System.Drawing.Point(, );
this.trackBar_VOUT1.Maximum = ;
this.trackBar_VOUT1.Name = "trackBar_VOUT1";
this.trackBar_VOUT1.Size = new System.Drawing.Size(, );
this.trackBar_VOUT1.TabIndex = ;
this.trackBar_VOUT1.Scroll += new \
System.EventHandler(this.trackBar_VOUT1_Scroll);
//
// groupBox1
//
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)\
((System.Windows.Forms.AnchorStyles.Bottom | \
System.Windows.Forms.AnchorStyles.Left)));
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.textBox_VOUT1);
this.groupBox1.Controls.Add(this.trackBar_VOUT1);
this.groupBox1.Location = new System.Drawing.Point(, );
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(, );
this.groupBox1.TabIndex = ;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "VOUT1";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(, );
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(, );
this.label1.TabIndex = ;
this.label1.Text = "LEVEL";
//
// textBox_VOUT1
//
this.textBox_VOUT1.Location = new System.Drawing.Point(, );
this.textBox_VOUT1.Name = "textBox_VOUT1";
this.textBox_VOUT1.ReadOnly = true;
this.textBox_VOUT1.Size = new System.Drawing.Size(, );
this.textBox_VOUT1.TabIndex = ;
this.textBox_VOUT1.Text = "LV 0";
//
// groupBox2
//
this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)\
((System.Windows.Forms.AnchorStyles.Bottom | \
System.Windows.Forms.AnchorStyles.Left)));
this.groupBox2.Controls.Add(this.label2);
this.groupBox2.Controls.Add(this.textBox_VOUT2);
this.groupBox2.Controls.Add(this.trackBar_VOUT2);
this.groupBox2.Location = new System.Drawing.Point(, );
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(, );
this.groupBox2.TabIndex = ;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "VOUT2";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(, );
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(, );
this.label2.TabIndex = ;
this.label2.Text = "LEVEL";
//
// textBox_VOUT2
//
this.textBox_VOUT2.Location = new System.Drawing.Point(, );
this.textBox_VOUT2.Name = "textBox_VOUT2";
this.textBox_VOUT2.ReadOnly = true;
this.textBox_VOUT2.Size = new System.Drawing.Size(, );
this.textBox_VOUT2.TabIndex = ;
this.textBox_VOUT2.Text = "LV 0";
//
// comboBox_SPort
//
this.comboBox_SPort.FormattingEnabled = true;
this.comboBox_SPort.Location = new System.Drawing.Point(, );
this.comboBox_SPort.Name = "comboBox_SPort";
this.comboBox_SPort.Size = new System.Drawing.Size(, );
this.comboBox_SPort.TabIndex = ;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(, );
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(, );
this.label3.TabIndex = ;
this.label3.Text = "SerialPort";
//
// button_SPort
//
this.button_SPort.Location = new \
System.Drawing.Point(, );
this.button_SPort.Name = "button_SPort";
this.button_SPort.Size = new \
System.Drawing.Size(, );
this.button_SPort.TabIndex = ;
this.button_SPort.Text = "Open";
this.button_SPort.UseVisualStyleBackColor = true;
this.button_SPort.Click += new \
System.EventHandler(this.button_SPort_Click);
//
// VOUTCTRL
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.button_SPort);
this.Controls.Add(this.label3);
this.Controls.Add(this.comboBox_SPort);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.groupBox2);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "VOUTCTRL";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "TB528E4PSE";
((System.ComponentModel.ISupportInitialize)(this.trackBar_VOUT2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.trackBar_VOUT1)).EndInit();
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.TrackBar trackBar_VOUT2;
private System.Windows.Forms.TrackBar trackBar_VOUT1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox_VOUT1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox_VOUT2;
private System.Windows.Forms.ComboBox comboBox_SPort;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button button_SPort; }
}
至此,代码跟踪至此。
2、更改窗体在屏幕首次出现的位置
即当APP运行时,窗体会在电脑屏幕的哪个位置弹出。点击窗体->属性,
找到“startposition”,如下图:
注意以上图片包含的信息:Text选项,此处则可以表明窗体显示的信息。
3,更改窗体显示的图标。
即当APP运行时,窗体在Text旁边的图片,点击窗体->属性,
找到“Icon”,如下图:
4、如何发布程序:
点击项目栏“生成”,点击“发布”选项即可发布程序了,如下图:
生成的文件如下图:
点击后缀名为".application"即可实现安装。
5、发布安装后,程序在桌面显示的图标,以及支持的".NET frameworkXX.XX"设置。
在项目右击,选择属性,会跳出窗体"项目名.csproj"文件,
里面可进行相应设置,如下图:
三、参考链接
Tony被参考的源代码github地址:
https://github.com/TonySudo/SerialPortW
至此,记录完毕
记录时间:2016-8-26
记录地点:深圳WZ
VS上利用C#实现一个简单的串口程序记录的更多相关文章
- 利用ANTLR4实现一个简单的四则运算计算器
利用ANTLR4实现一个简单的四则运算计算器 ANTLR4介绍 ANTLR能够自动地帮助你完成词法分析和语法分析的工作, 免去了手写去写词法分析器和语法分析器的麻烦 它是基于LL(k)的, 以递归下降 ...
- 利用JSP编程技术实现一个简单的购物车程序
实验二 JSP编程 一.实验目的1. 掌握JSP指令的使用方法:2. 掌握JSP动作的使用方法:3. 掌握JSP内置对象的使用方法:4. 掌握JavaBean的编程技术及使用方法:5. 掌握JSP ...
- 利用 nodeJS 搭建一个简单的Web服务器(转)
下面的代码演示如何利用 nodeJS 搭建一个简单的Web服务器: 1. 文件 WebServer.js: //-------------------------------------------- ...
- 利用 Docker 构建一个简单的 java 开发编译环境
目前 Java 语言的版本很多,除了常用的 Java 8,有一些遗留项目可能使用了 Java 7,也可能有一些比较新的的项目使用了 Java 10 以上的版本.如果想切换自己本地的 Java 开发环境 ...
- 通过汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的
秦鼎涛 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验一 通过汇编一个简单的C程序,分析汇编代码 ...
- 3.2 Lucene实战:一个简单的小程序
在讲解Lucene索引和检索的原理之前,我们先来实战Lucene:一个简单的小程序! 一.索引小程序 首先,new一个java project,名字叫做LuceneIndex. 然后,在project ...
- 编写一个简单的C++程序
编写一个简单的C++程序 每个C++程序都包含一个或多个函数(function),其中一个必须命名为main.操作系统通过调用main来运行C++程序.下面是一个非常简单的main函数,它什么也不干, ...
- 使用Go开发一个简单的服务器程序
最近有个小项目,需要一个简单的后台程序来支撑,本来想用Nodejs来做,但是由于本人js一直很菜,并且很讨厌callback,虽然我也很喜欢异步模型,但我一直都觉得JS是反人类的.后台就用了go处理, ...
- 一个简单的flask程序
初始化 所有Flask程序都必须创建一个程序实例. 程序实例是Flask类的对象,经常使用下述代码创建: from flask import Flask app = Flask(__name__) F ...
随机推荐
- [iOS 基于CoreBluetooth的蓝牙4.0通讯]
一.首先大致介绍下蓝牙4.0的模式,中心和周边: 一般情况下,iPhone作为中心,接收来自周边传感器(比如手环等)采集的数据. 二.那整一个数据通讯的协议是怎样的呢? 为什么要一层层搞这么复杂呢?据 ...
- [iOS 主要框架的总结]
原文地址:http://blog.csdn.net/GooHong/article/details/28911301 在iOS中框架是一个目录,包含了共享资源库,用于访问该资源库中储存的代码的头文件, ...
- HTML之:让网页中的<a>标签属性统一设置-如‘新窗口打开’
在开发过程中,我们往往想在页面中,给<a>设置一个统一的默认格式,例如我们想让链接:“在新窗口打开”,我们就可以使用<base>标签 在网页中添加这段代码: <head& ...
- xml小记1
xml小记1 关于边框的实现 这是一个比较简单的东西,但是今天莫名的低效率,在这上面花了比较多的时间.之前有咨询过同学如何实现单向的边框,他们采用的方法是调用别人的接口. 我采用的方法如下: < ...
- Linux rsync网站目录同步功能的实现
实现目标: 172.16.1.64服务器上的/var/www/sw_service目录,与172.16.1.60服务器上的/var/www/sw_service目录实现同步, 即1.60主动向1.64 ...
- CSS--结构和层叠
选择器的特殊性 特殊性值表述为4个部分,如0,0,0,0.具体特殊性如下所示: 举例说明一下: 通配符选择器的特殊性 通配符选择器其特殊性为0,0,0,0 !important重要性 大家都知道内联样 ...
- mybatis的逆向工程
mybatis的逆向工程是很大的减少了程序员对代码的编写工作,由于mybatis是半自动的sql语句使用,我们在项目中一般都是采用逆向工程来生成mybatis的文件,mapper接口相当于我们平常所说 ...
- webView(简单的浏览器)
#import "MJViewController.h" @interface MJViewController () <UISearchBarDelegate, UIWeb ...
- mongdb查询与排序
db.QResult.find({'CreateDate':{'$gte' : ISODate('2016-07-01'), '$lte' : ISODate('2016-08-01')}}).sor ...
- Java排序算法——冒泡排序
import java.util.Arrays; //================================================= // File Name : Bubble_S ...