using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO.Ports;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace SerialCommunication{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private SerialPort serial = new SerialPort(); private void Form1_Load(object sender, EventArgs e) { } /// <summary> /// 打开串口 /// </summary> /// <param name="strPortName">串口号</param> /// <param name="nRate">波特率</param> /// <param name="nDataBit">数据位</param> /// <param name="stopBits">停止位</param> /// /// <param name="nParity">校验位</param> /// <returns></returns> public bool OpenSerial(string strPortName, int nRate, int nDataBit, float nStopBits, int nParity) { serial.DataReceived = new SerialDataReceivedEventHandler(ReciveSerialData); serial.PortName = strPortName;//串口号 serial.BaudRate = nRate;//波特率 float f = nStopBits;//停止位 if (f == 0) { serial.StopBits = StopBits.None; } else if (f == 1.5) { serial.StopBits = StopBits.OnePointFive; } else if (f == 1) { serial.StopBits = StopBits.One; } else { serial.StopBits = StopBits.Two; } serial.DataBits = nDataBit;//数据位 if (nParity == 0) //校验位 { serial.Parity = Parity.None; } else if (nParity == 1) { serial.Parity = Parity.Odd; } else if (nParity == 2) { serial.Parity = Parity.Even; } else { serial.Parity = Parity.None; } serial.ReadTimeout = 3000;//设置超时读取时间 serial.WriteTimeout = 500;//超时写入时间 try { if (!serial.IsOpen) { serial.Open(); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); return false; } return true; } /// <summary> /// 写入数据 /// </summary> /// <param name="verifyIndex"></param> /// <param name="btyData"></param> /// <returns></returns> private bool SerialWrite(int verifyIndex, byte[] btyData) { try { int numAdjust = 0; for (; verifyIndex < btyData.Length - 1; verifyIndex ) { numAdjust = Convert.ToInt32(btyData[verifyIndex].ToString()); } if (numAdjust > 0xff) { string strAdjust = Convert.ToString(numAdjust, 16); numAdjust = Convert.ToInt32(strAdjust.Substring(strAdjust.Length - 2), 16); } btyData[btyData.Length - 1] = (byte)numAdjust; serial.Write(btyData, 0, btyData.Length); } catch(Exception ex) { MessageBox.Show(ex.ToString()); return false; } return true; } /// <summary> /// 接收数据事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ReciveSerialData(object sender, SerialDataReceivedEventArgs e) { try { if (serial.BytesToRead == 0) { return; } byte[] btyReciveData = new byte[serial.BytesToRead]; byte[] btyResoureData = new byte[btyReciveData.Length]; string strData = string.Empty; int intSp = serial.Read(btyReciveData, 0, btyReciveData.Length);//在此就可以读取到当前缓冲区内的数据 int i = 0; string[] hex = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; for (i = 0; i < btyReciveData.Length; i ) { btyResoureData[i] = Convert.ToByte(("0x" (hex[btyReciveData[i] / 16]).ToString() (hex[btyReciveData[i] % 16]).ToString()), 16); } for (int a = 0; a < btyReciveData.Length; a ) { strData = btyResoureData[a].ToString("X2"); } //如果有接收,则发送成功 if (strData.IndexOf("5A55000002D101186A69") >= 0) { } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } /// <summary> /// 初始化串口命令 /// </summary> public void InitialSerialCommand(int nStaus) { byte[] btyData = new byte[100]; btyData[0] = 0x5A; btyData[1] = 0x55; btyData[2] = 0x00; btyData[3] = 0x00; btyData[4] = 0x02; btyData[5] = 0xD1; btyData[6] = 0x00; btyData[7] = 0x18; btyData[8] = 0x6A; btyData[9] = 0x69; //开灯 if (nStaus == 0) { btyData[6] = 0x01; } //全关 else { btyData[6] = 0x00; } //发送指令 if (serial != null) { try { SerialWrite(0, btyData); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } private void btnOpenSerial_Click(object sender, EventArgs e) { if (!OpenSerial("COM1", 115200, 8, 1, 0)) { //串口打开失败 MessageBox.Show("串口打开失败!"); } } private void btnSend_Click(object sender, EventArgs e) { InitialSerialCommand(0); } }}
下载c#232串口通讯源码用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论