一、背景

  工作上需要利用串口往下位机写入数据,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#实现一个简单的串口程序记录的更多相关文章

  1. 利用ANTLR4实现一个简单的四则运算计算器

    利用ANTLR4实现一个简单的四则运算计算器 ANTLR4介绍 ANTLR能够自动地帮助你完成词法分析和语法分析的工作, 免去了手写去写词法分析器和语法分析器的麻烦 它是基于LL(k)的, 以递归下降 ...

  2. 利用JSP编程技术实现一个简单的购物车程序

    实验二   JSP编程 一.实验目的1. 掌握JSP指令的使用方法:2. 掌握JSP动作的使用方法:3. 掌握JSP内置对象的使用方法:4. 掌握JavaBean的编程技术及使用方法:5. 掌握JSP ...

  3. 利用 nodeJS 搭建一个简单的Web服务器(转)

    下面的代码演示如何利用 nodeJS 搭建一个简单的Web服务器: 1. 文件 WebServer.js: //-------------------------------------------- ...

  4. 利用 Docker 构建一个简单的 java 开发编译环境

    目前 Java 语言的版本很多,除了常用的 Java 8,有一些遗留项目可能使用了 Java 7,也可能有一些比较新的的项目使用了 Java 10 以上的版本.如果想切换自己本地的 Java 开发环境 ...

  5. 通过汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的

    秦鼎涛  <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验一 通过汇编一个简单的C程序,分析汇编代码 ...

  6. 3.2 Lucene实战:一个简单的小程序

    在讲解Lucene索引和检索的原理之前,我们先来实战Lucene:一个简单的小程序! 一.索引小程序 首先,new一个java project,名字叫做LuceneIndex. 然后,在project ...

  7. 编写一个简单的C++程序

    编写一个简单的C++程序 每个C++程序都包含一个或多个函数(function),其中一个必须命名为main.操作系统通过调用main来运行C++程序.下面是一个非常简单的main函数,它什么也不干, ...

  8. 使用Go开发一个简单的服务器程序

    最近有个小项目,需要一个简单的后台程序来支撑,本来想用Nodejs来做,但是由于本人js一直很菜,并且很讨厌callback,虽然我也很喜欢异步模型,但我一直都觉得JS是反人类的.后台就用了go处理, ...

  9. 一个简单的flask程序

    初始化 所有Flask程序都必须创建一个程序实例. 程序实例是Flask类的对象,经常使用下述代码创建: from flask import Flask app = Flask(__name__) F ...

随机推荐

  1. shutil模块

    shutil模块 提供了大量的文件的高级操作,特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作 常用方法 shutil.copyfile(src, dst) 复制文件内容(不包含元数据)从 ...

  2. WPF中ListBox控件在选择模式(SelectionMode)为Single时仍然出现多个Item被选中的问题

    最近在学习WPF过程中使用到了ListBox控件,在使用时遇到下面的奇怪问题: 代码如下: listBox.Items.Add("绘图"); listBox.Items.Add(& ...

  3. 【Beta版本】冲刺-Day6

    队伍:606notconnected 会议时间:12月14日 目录 一.行与思 二.站立式会议图片 三.燃尽图 四.代码Check-in 一.行与思 张斯巍(433) 今日进展:修改界面规范,应用图标 ...

  4. POJ 2240 - Arbitrage(bellman_ford & floyd)

    题意: 给出一些货币和货币之间的兑换比率,问是否可以使某种货币经过一些列兑换之后,货币值增加. 举例说就是1美元经过一些兑换之后,超过1美元.可以输出Yes,否则输出No. 分析: 首先我们要把货币之 ...

  5. java学习总结

    1 获得项目绝对路径 String path = request.getContextPath(); String basePath = request.getScheme() + ":// ...

  6. Install latest R for ubuntu

    ### delete old version rm -rf /usr/local/lib/R /usr/lib/R ~/**/R sudo apt-get autoremove rstudio sud ...

  7. spring-data-jpa Repository的基本知识

    1.项目中的Repository对象的使用 2.Repository 引入的两种方式 继承和使用注解 3.Repository接口的定义 Repository 接口是 spring Data 的一个核 ...

  8. win7安装virtualbox

    1.下载软件 VirtualBox-4.3.24-98716-Win.1425444683.exe 2.修改安装路径 3.确定选择下一步 4.下一步 5.yes 6.安装 7.安装完成 到此win7 ...

  9. angularjs笔记(一)

    简介 AngularJS API angularjs是javascript框架,通过指令(指令就是自定义的html标签属性)扩展了HTML,并且可以通过表达式(表达式使用)绑定数据到HTML. 1.a ...

  10. 使用RawSocket进行网络抓包

    aw socket,即原始套接字,可以接收本机网卡上的数据帧或者数据包,对与监听网络的流量和分析是很有作用的,一共可以有3种方式创建这种socket. 中文名 原始套接字 外文名 RAW SOCKET ...