资源简介
C# 上位机 串口通讯
代码片段和文件信息
/*
* Project: Serial Port Interface with C#
* Company: StudentCompanion
* Created: April 2013
* Updated: February 2020
* Visual Studio: 2019
* .NET framework: 4.6.2
* Notes: This is a simple demontration on how to use the SerialPort control for
* communicating with your PC‘s COM Port.
* This is for educational purposes only and not for any commercial use.
* For more on this please visit: https://www.studentcompanion.co.za/creating-a-serial-port-interface-with-c/
* */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports; //This is a namespace that contains the SerialPort class
namespace Serial_Communication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender EventArgs e)
{
updatePorts(); //Call this function everytime the page load
//to update port names
CheckForIllegalCrossThreadCalls = false;
}
private void updatePorts()
{
// Retrieve the list of all COM ports on your Computer
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
cmbPortName.Items.Add(port);
}
}
private SerialPort ComPort = new SerialPort(); //Initialise ComPort Variable as SerialPort
private void connect()
{
bool error = false;
// Check if all settings have been selected
if (cmbPortName.SelectedIndex != -1 & cmbBaudRate.SelectedIndex != -1 & cmbParity.SelectedIndex != -1 & cmbDataBits.SelectedIndex != -1 & cmbStopBits.SelectedIndex != -1)
{
//if yes than Set The Port‘s settings
ComPort.PortName = cmbPortName.Text;
ComPort.BaudRate = int.Parse(cmbBaudRate.Text); //convert Text to Integer
ComPort.Parity = (Parity)Enum.Parse(typeof(Parity) cmbParity.Text); //convert Text to Parity
ComPort.DataBits = int.Parse(cmbDataBits.Text); //convert Text to Integer
ComPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits) cmbStopBits.Text); //convert Text to stop bits
try //always try to use this try and catch method to open your port.
//if there is an error your program will not display a message instead of freezing.
{
//Open Port
ComPort.Open();
ComPort.DataReceived += SerialPortD
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2020-02-02 22:56 Serial_Communication\
目录 0 2020-02-01 07:41 Serial_Communication\.vs\
目录 0 2020-02-01 07:41 Serial_Communication\.vs\Serial_Communication\
目录 0 2020-02-01 07:57 Serial_Communication\.vs\Serial_Communication\v16\
文件 29696 2020-02-02 22:57 Serial_Communication\.vs\Serial_Communication\v16\.suo
目录 0 2020-02-01 07:46 Serial_Communication\.vs\Serial_Communication\v16\Server\
目录 0 2020-02-02 22:57 Serial_Communication\.vs\Serial_Communication\v16\Server\sqlite3\
文件 0 2020-02-01 07:46 Serial_Communication\.vs\Serial_Communication\v16\Server\sqlite3\db.lock
文件 569344 2020-02-02 22:57 Serial_Communication\.vs\Serial_Communication\v16\Server\sqlite3\storage.ide
文件 163 2020-02-02 22:12 Serial_Communication\app.config
目录 0 2014-02-18 21:52 Serial_Communication\bin\
目录 0 2020-02-02 22:22 Serial_Communication\bin\Debug\
文件 30208 2020-02-02 22:22 Serial_Communication\bin\Debug\Serial_Communication.exe
文件 163 2020-02-02 22:12 Serial_Communication\bin\Debug\Serial_Communication.exe.config
文件 30208 2020-02-02 22:22 Serial_Communication\bin\Debug\Serial_Communication.pdb
文件 11600 2013-04-26 21:09 Serial_Communication\bin\Debug\Serial_Communication.vshost.exe
文件 490 2010-03-18 04:39 Serial_Communication\bin\Debug\Serial_Communication.vshost.exe.manifest
目录 0 2015-11-30 02:06 Serial_Communication\bin\Release\
目录 0 2014-02-18 21:52 Serial_Communication\bin\Release\app.publish\
目录 0 2014-02-18 21:52 Serial_Communication\bin\Release\app.publish\Application Files\
目录 0 2014-02-18 21:52 Serial_Communication\bin\Release\app.publish\Application Files\Serial_Communication_1_0_0_0\
文件 15872 2013-04-26 21:19 Serial_Communication\bin\Release\app.publish\Application Files\Serial_Communication_1_0_0_0\Serial_Communication.exe.deploy
文件 6747 2013-04-26 21:20 Serial_Communication\bin\Release\app.publish\Application Files\Serial_Communication_1_0_0_0\Serial_Communication.exe.manifest
文件 5658 2013-04-26 21:20 Serial_Communication\bin\Release\app.publish\Serial_Communication.application
文件 428960 2013-04-26 21:20 Serial_Communication\bin\Release\app.publish\setup.exe
文件 1938 2015-11-30 02:10 Serial_Communication\bin\Release\Serial_Communication.application
文件 15872 2015-11-30 02:10 Serial_Communication\bin\Release\Serial_Communication.exe
文件 3101 2015-11-30 02:10 Serial_Communication\bin\Release\Serial_Communication.exe.manifest
文件 34304 2015-11-30 02:10 Serial_Communication\bin\Release\Serial_Communication.pdb
文件 1938 2015-11-30 02:10 Serial_Communication\bin\Release\Serial_Communication.vshost.application
文件 24224 2015-11-30 02:10 Serial_Communication\bin\Release\Serial_Communication.vshost.exe
............此处省略47个文件信息
相关资源
- C#递归实现资源管理器
-
欧姆龙PLC通讯程序源码(HOST li
nk 实 - C# RS232串口通信
- 使用C#实现的电子常用计算
- 三菱Q-PLC与C#串口
- C#实现Spy++
- 寿星万年历的c#实现版本sxtwl_csharp_t
- C#实现特殊主框架界面效果
- 门诊收费管理系统的设计与实现(数
- c# 串口通讯的(收发数据)
- C#联合halcon实现鼠标滚轮事件
- C#网站项目使用SignalR技术实现实时聊
- Csharp串口通讯
- c#获取串口
- 网络升级上位机源码(STM32远程升级程
- C#实现闪屏程序
- Zebar打印机
- C# 实现 信捷PLC通讯协议
- C# 通过S7.NET方式实现与西门子PLC通信
- WPF开发串口调试助手
- C#实现调色板
- 物联网设备通讯协议实现客户端(I
- 串口通讯工具源码
- C#串口通讯
- visual C#使用指纹识别器实现员工考勤
- C# 撤销与恢复功能实现(Command模式)
- 上位机语音识别控制PLC源码(附西门
- RSA算法实现加、解密
- 串口上位机
- C#上位机电表数据采集
评论
共有 条评论