一、会用到一个串口控件:

serialPort

1.串口写入数据

2.串口读取数据

读取数据要在窗体加载的时侯添加一个数据接收事件。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;namespace _20_串口助手
{public partial class Form1 : Form{public Form1(){InitializeComponent();System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;}private void label1_Click(object sender, EventArgs e){}private void Form1_Load(object sender, EventArgs e){duanKou.SelectedIndex = 0;boTeiLv.SelectedIndex = 4;//表示已通过serialPort1对象接收了数据。//数据接收事件serialPort2.DataReceived += new SerialDataReceivedEventHandler(port_DataReeived);//必须手动添加事件处理}//为DataReeived提供数据private void port_DataReeived(object sender, SerialDataReceivedEventArgs e)//串口接收数据{if (!rad3.Checked)//如果接收模式为字符模式{string str = serialPort2.ReadExisting();//outMsg.Text = str;}else{byte data;data = (byte)serialPort2.ReadByte();//此处需要强制类型转换,将int类型数据转换为byte类型string str = Convert.ToString(data, 16).ToUpper();//转换为大写十六进制字符串outMsg.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");//空位补"0"}}//打开端口private void openDuan_Click(object sender, EventArgs e){try{serialPort1.PortName = duanKou.Text;//获取端口名serialPort1.BaudRate = Convert.ToInt32(boTeiLv.Text, 10);//获取波特率,十进制数据转换serialPort1.Open();//端口打开// serialPort2.PortName = duanKou.Text;//获取端口名serialPort2.BaudRate = Convert.ToInt32(boTeiLv.Text, 10);//获取波特率,十进制数据转换serialPort2.Open();//openDuan.Enabled = false;//打开端口按钮显示灰色不可用closeDuan.Enabled = true;//关闭端口按钮显示为可用}catch{MessageBox.Show("端口错误,请检查");}}//关闭端口private void closeDuan_Click(object sender, EventArgs e){try{serialPort1.Close();//关闭端口serialPort2.Close();//关闭端口openDuan.Enabled = true;closeDuan.Enabled = false;}catch{MessageBox.Show("端口异常,请检查");}}//发送数据private void send_Click(object sender, EventArgs e){byte[] data=new byte[1];//每次发送一个字节if (serialPort1.IsOpen)//端口打开才执行下面的语句{if (inMsg.Text.Length!=0)//判断是否有输入内容{if (!rad1.Checked)//如果发送模式是字符模式{try{serialPort1.WriteLine(inMsg.Text);//将输入的内容通过端口写入。}catch{MessageBox.Show("端口异常,请检查");serialPort1.Close();//关闭端口duanKou.Enabled = true;//打开端口按钮改为可用。closeDuan.Enabled = false;//关闭端口按钮改为不可用。}}else{for(int i = 0; i < inMsg.Text.Length / 2; i++){data[0]=Convert.ToByte(inMsg.Text.Substring(i*2,2),16);//将获取到的数值转换为16进制;serialPort1.Write(data, 0, 1);//通过端口循环发送(如果输入的是OAOAB,则只发送OA,OB)}if (inMsg.Text.Length % 2 != 0) {data[0] =Convert.ToByte(inMsg.Text.Substring(Text.Length-1,1),16);//单独发送BserialPort1.Write(data, 0, 1);//通过端口发送。}}}}else{MessageBox.Show("端口未开启,请检查");}}}
}

C#串口通讯之-PC串口发送数据相关推荐

  1. STM32串口通信中使用printf发送数据配置方法 开发环境 Keil

    STM32串口通信中使用printf发送数据配置方法(开发环境 Keil RVMDK) 已有 12456 次阅读2011-6-29 23:29 | 在STM32串口通信程序中使用printf发送数据, ...

  2. java 采用串口通信实现LED屏发送数据中文乱码问题

    java 采用串口通信实现LED屏发送数据中文乱码问题 1.定义:Pointer p = new Pointer(MemoryBlockFactory.createMemoryBlock(1024)) ...

  3. codesys 串口通讯实例_串口通讯例子,(已调试成功可以接收数据)

    io.open();//打开控制台importcom.activeX io.print("qq:761153454,本程序为串口通讯") com1 =com.CreateObjec ...

  4. c++学习 | MFC —— 串口通信(四)发送数据

    文章目录 一.写串口函数 1.源函数 2.API 函数详解 (1)ClearCommError()函数--读取串行端口现在的状态 (2)PurgeComm()函数--清空缓冲区 (3)WriteFil ...

  5. ros串口通讯(读取串口数据)

    ros串口通讯是非常重要的通讯手段,通常跟下位机或者各种usb口外设都是通过串口进行通讯的. 那么我们跟着教程来学习一下如何读取手机通过无线串口发送给电脑的数据. 这里我通过一个usb-ttl工具将蓝 ...

  6. python隐藏画笔箭头的函数是hide_Python开发的串行通信上位机程序三部曲第三部分,串口,通讯,用,图形,方式,显示,数据...

    用Visual Basic或者C#, LabVIEW等软件开发的上位机界面,和文本式交互界面比较而言,就是可以用图形方式来显示数据.那么python可不可以用图形来显示数据呢?Of course! 和 ...

  7. c51单片机串口通信电脑向单片机发送数据

    main.c #include <REGX52.H> #include "UART.H"void Delay(int t) { while(--t); }void ma ...

  8. codesys 串口通讯实例_串口通信RS232的基本接法,原来这么简单,今天终于弄明白了...

    目前较为常用的串口有9针串口(DB9)和25针串口(DB25),通信距离较近时(<12m),可以用电缆线直接连接标准RS232端口(RS422,RS485较远),若距离较远,需附加调制解调器(M ...

  9. plc串口通讯 qt_Qt 编写串口调试助手

    一.成品图展示 成品图如下所示: 二.串口通讯步骤 1.在工程文件(.pro)中添加串口通信相关运行库:QT += serialport 2.在头文件中添加: #include #include 3. ...

最新文章

  1. Android 在Fragment中执行onActivityResult不被调用的简单解决方法
  2. confirm修改按钮文字_踏入MG动画设计的门,才知道文字动画这么重要……
  3. 指针(pointers)和引用(references)区别
  4. lua_path环境变量设置linux,ubuntu16.04安装lua环境
  5. Spring Boot笔记-get请求发送json数据(方便前端vue解析)
  6. condition的作用
  7. 机器学习实战 | 数据探索(缺失值处理)
  8. DPDK如何释放大页内存(巨页内存hugepage)
  9. Maven学习总结(35)——Maven项目错误 JAX-RS (REST Web Services) 2.0 can not be installed问题...
  10. 单例模式、使用getInstance()方法的原因及作用
  11. 随想录(关于核心技术)
  12. 物理知识(量子物理)
  13. 书荒季 | “超长待机”的假期,收好这份信息安全书单!
  14. SI24R1可以替代NRF24L01P软件硬件DIY兼容成功
  15. 台式计算机无线网卡怎么找,win7台式机找不到无线网卡怎么办
  16. 路由器装linux系统,怎样在路由器上安装Linux / DD-WRT固件
  17. Docker build创建指定容器镜像
  18. LSB利器-zsteg
  19. Leetcode 2327. Number of People Aware of a Secret [Python]
  20. BugKu:WEB——gam1

热门文章

  1. python matlabplot画表格_matplotlib-绘制精美图表
  2. html meta标签大全
  3. 线程导入大数据入库_多线程批量插入数据小结
  4. linux and windows 双系统启动(详细情况分类说明)
  5. 如何更改Eclipse的workspace路径
  6. java集合详解之ArrayList
  7. 实现远程访问PLC的方法篇一
  8. CentOS上安装部署gitblit
  9. ThreadLocal详解分析
  10. python长度单位换算_python中温度单位转换的实例方法