资源简介
简单的串口通讯例子

代码片段和文件信息
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Communication
{
public partial class Form1 : Form
{
///
/// 创建串口对象
///
private SerialPort port = new SerialPort();
///
/// 接受消息
///
private StringBuilder MsgStr = new StringBuilder();
///
/// 构造函数
///
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false; //防止跨线程访问出错,好多地方会用到
}
///
/// 窗口加载函数
///
///
///
private void Form1_Load(object sender EventArgs e)
{
///获取窗口列表
string[] ports = SerialPort.GetPortNames();
if (ports.Length > 0)
{
///添加可选通讯口
com.Items.AddRange(ports);
///设置默认选择
com.SelectedIndex = 0;
bot.SelectedIndex = 0;
databit.SelectedIndex = 0;
stopbit.SelectedIndex = 0;
jy.SelectedIndex = 0;
}
else
{
MessageBox.Show(“没有可用的串口!“);
}
}
///
/// 确认按钮
///
///
///
private void button1_Click(object sender EventArgs e)
{
try
{
///检测串口是否开着
if (!port.IsOpen)
{
///设置串口名称
port.PortName = com.SelectedItem.ToString();
///设置波特率
port.BaudRate = Convert.ToInt32(bot.Text);
///数据位
port.DataBits = Convert.ToInt32(databit.Text);
///设置无协议
port.Handshake = Handshake.None;
///设置读取不超时
port.ReadTimeout = -1;
//port.WriteTimeout = -1;
///设置停止位
port.StopBits = GetStopBits(stopbit.SelectedIndex + 1);
port.DtrEnable = true;
///无校验
port.Parity = GetParity(jy.SelectedIndex);
///设置数据接收事件
port.DataReceived += ReciveData;
///打开串口
port.Open();
open.Text = “关闭串口“;
UpdateStutas();
}
else
{
///关闭串口
port.Close();
port.DataReceived -= ReciveData;
open.Text = “打开串口“;
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
..A..H. 53248 2017-03-07 12:49 Communication\.vs\Communication\v14\.suo
文件 189 2017-03-03 14:53 Communication\Communication\App.config
文件 15360 2017-03-07 12:44 Communication\Communication\bin\Debug\Communication.exe
文件 189 2017-03-03 14:53 Communication\Communication\bin\Debug\Communication.exe.config
文件 24064 2017-03-07 12:44 Communication\Communication\bin\Debug\Communication.pdb
文件 22696 2017-03-07 12:44 Communication\Communication\bin\Debug\Communication.vshost.exe
文件 189 2017-03-03 14:53 Communication\Communication\bin\Debug\Communication.vshost.exe.config
文件 490 2015-11-13 10:24 Communication\Communication\bin\Debug\Communication.vshost.exe.manifest
文件 3924 2017-03-03 17:22 Communication\Communication\Communication.csproj
文件 7962 2017-03-07 12:49 Communication\Communication\Form1.cs
文件 11645 2017-03-07 12:44 Communication\Communication\Form1.Designer.cs
文件 6017 2017-03-07 12:44 Communication\Communication\Form1.resx
文件 870 2017-03-07 12:44 Communication\Communication\obj\Debug\Communication.csproj.FileListAbsolute.txt
文件 1012 2017-03-07 12:44 Communication\Communication\obj\Debug\Communication.csproj.GenerateResource.Cache
文件 2384 2017-03-03 17:22 Communication\Communication\obj\Debug\Communication.csprojResolveAssemblyReference.cache
文件 15360 2017-03-07 12:44 Communication\Communication\obj\Debug\Communication.exe
文件 180 2017-03-07 12:44 Communication\Communication\obj\Debug\Communication.Form1.resources
文件 24064 2017-03-07 12:44 Communication\Communication\obj\Debug\Communication.pdb
文件 180 2017-03-03 17:22 Communication\Communication\obj\Debug\Communication.Properties.Resources.resources
文件 1464 2017-03-06 09:29 Communication\Communication\obj\Debug\DesignTimeResolveAssemblyReferences.cache
文件 7220 2017-03-03 14:53 Communication\Communication\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 0 2017-03-03 14:53 Communication\Communication\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
文件 0 2017-03-03 14:53 Communication\Communication\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
文件 0 2017-03-03 14:53 Communication\Communication\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
文件 525 2017-03-03 14:53 Communication\Communication\Program.cs
文件 1334 2017-03-03 14:53 Communication\Communication\Properties\AssemblyInfo.cs
文件 2839 2017-03-03 14:53 Communication\Communication\Properties\Resources.Designer.cs
文件 5612 2017-03-03 14:53 Communication\Communication\Properties\Resources.resx
文件 1100 2017-03-03 14:53 Communication\Communication\Properties\Settings.Designer.cs
文件 249 2017-03-03 14:53 Communication\Communication\Properties\Settings.settings
............此处省略15个文件信息
相关资源
- Modbus协议官方文档中、英文全
- 易语言OTG串口通信
- delphi 串口通讯
- STM32蓝牙和串口程序
- LCD显示温度+串口接收温度.rar
- WPF USB 网络 串口 通信软件
- DELPHI与西门子200PLC的串口通信实例
- B/S 网上订餐系统
- Verilog FPGA UART串口控制器
- USB转串口驱动,FT232R驱动程序,最新
- 教室管理系统.rar
- PC -- 单片机的串口数据传输系统设计
- 小鸡快跑游戏.
- STM32F103 串口程序(完整版)
- stm32 ds18b20 温度传感器 测试通过
- PC 串口调试软件
- 51单片机读取温度数据存储到SD卡中并
-
分别适用于.NET fr
amework 2.0和4.0的E - 汽车租赁系统............................
- 单片机与PC机串口通讯仿真
- 串口调试助手V5.0
- 双串口调试助手 V4.0 _ 可同时调试两个
- 读取串口数据并画实时曲线的VC 程序
- 德卡D8读写器关于读写感应卡的一些代
- halcon 测量助手
- 网络(UDP)转串口程序
- 图片存储到数据库保存二进制文件并
- 串口绘制曲线 将收到的数据进行曲线
- 基于ARM蓝牙传输源程序
- JS操作本地网页串口源码
评论
共有 条评论