已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C#串口通信总结作者:不详 出处:网络转载 2011/10/10 16:01:52 阅读 0 次 我们知道对于标准DLL,可以采用DllImport进行调用。例如:DllImport(KMY350X.dll)private static extern int OpenPort(int PortNum, int BaudRate); 甘特图Xgantt(全球首款甘特图32年历史) Visual WebGui(完美的用户界面解决方案) Stimulsoft Reports.Net(报表生成控件) DXperience Uni Premium 白金版(慧都独家) 如果一些厂家比较懒的话,没有提供相应的dll,我们只能对它进行串口通信编程了。以前从没接触过串口编程,最近在一个项目中有几个地方都需要采用串口通信,跟公司一个老手请教后,感觉学到了很多东西,特在此做个总结: 一:首先我们来认识下什么是串口: 右键 我的电脑-管理-设备管理器-端口,选择一个端口,点击属性。 我们可以看到该串口的属性,在C#中我们使用SerialPort类来表示串口 ConfigClass config = new ConfigClass();comm.serialPort.PortName = config.ReadConfig(SendHealCard);/波特率comm.serialPort.BaudRate = 9600;/数据位comm.serialPort.DataBits = 8;/两个停止位comm.serialPort.StopBits = System.IO.Ports.StopBits.One;/无奇偶校验位comm.serialPort.Parity = System.IO.Ports.Parity.None;comm.serialPort.ReadTimeout = 100;comm.serialPort.WriteTimeout = -1; 二:串口调试工具: 在对串口进行编程时候,我们要向串口发送指令,然后我们解析串口返回的指令。在这里向大家推荐一款工具。 串口调试助手.exe 将要发送的指令用空格隔开,选择HEX显示为放回的字符串。 三:正式编程: 编写Comm类: public class Commpublic delegate void EventHandle(byte readBuffer);public event EventHandle DataReceived; public SerialPort serialPort;Thread thread;volatile bool _keepReading; public Comm()serialPort = new SerialPort();thread = null;_keepReading = false; public bool IsOpengetreturn serialPort.IsOpen; private void StartReading()if (!_keepReading)_keepReading = true;thread = new Thread(new ThreadStart(ReadPort);thread.Start(); private void StopReading()if (_keepReading)_keepReading = false;thread.Join();thread = null; private void ReadPort()while (_keepReading)if (serialPort.IsOpen)int count = serialPort.BytesToRead;if (count 0)byte readBuffer = new bytecount;tryApplication.DoEvents();serialPort.Read(readBuffer, 0, count);if(DataReceived != null)DataReceived(readBuffer);Thread.Sleep(100);catch (TimeoutException) public void Open()Close();serialPort.Open();if (serialPort.IsOpen)StartReading();elseMessageBox.Show(串口打开失败!); public void Close()StopReading();serialPort.Close(); public void WritePort(byte send, int offSet, int count)if (IsOpen)serialPort.Write(send, offSet, count); 注册串口: Comm comm = new Comm();ConfigClass config = new ConfigClass();comm.serialPort.PortName = config.ReadConfig(SendHealCard);/波特率comm.serialPort.BaudRate = 9600;/数据位comm.serialPort.DataBits = 8;/两个停止位comm.serialPort.StopBits = System.IO.Ports.StopBits.One;/无奇偶校验位comm.serialPort.Parity = System.IO.Ports.Parity.None;comm.serialPort.ReadTimeout = 100;comm.serialPort.WriteTimeout = -1;comm.Open();if (comm.IsOpen)comm.DataReceived += new Comm.EventHandle(comm_DataReceived); 发送指令: / / 发卡到机口/ private void SendCardToOut()is_read_card = false;sendCardToOut = true;byte send = 0x02, 0x46, 0x43, 0x34, 0x03, 0x30 ;if (comm.IsOpen)comm.WritePort(send, 0, send.Length); 收到指令,并解析: void comm_DataReceived(byte readBuffer1)/log.Info(HexCon.ByteToString(readBuffer);if (readBuffer1.Length = 1)receive = HealCardClass.ByteToString(readBuffer1);string str = 06;if (string.Equals(receive.Trim(), str, StringComparison.CurrentCultureIgnoreCase)tryif (is_read_card)byte send = new byte1;send0 = 0x05;comm.WritePort(send, 0, send.Length);Thread.Sleep(500);comm.DataReceived -= new Comm.EventHandle(comm_DataReceived);InitReadComm();if (sendCardToOut)byte send = new byte1;send0 = 0x05;comm.WritePort(send, 0, send.Length); readComm.DataReceived -= new Comm.EventHandle(readComm_DataReceived);readComm.Close(); log.Info(发卡完成!);lblMsg.Text = 发卡成
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年佳木斯辅警招聘考试题库及一套完整答案详解
- 2023年衡阳辅警招聘考试题库含答案详解(黄金题型)
- 2023年绥化辅警招聘考试真题附答案详解(培优a卷)
- 2023年贵阳辅警协警招聘考试备考题库附答案详解(达标题)
- 2024年和田辅警协警招聘考试备考题库及参考答案详解一套
- 2024年宿州辅警协警招聘考试真题完整答案详解
- 2024年内江辅警招聘考试真题及答案详解(全优)
- 2024年天津辅警协警招聘考试备考题库附答案详解(黄金题型)
- 2024年安庆辅警协警招聘考试真题含答案详解(预热题)
- 2023年秦皇岛辅警招聘考试真题含答案详解(巩固)
- 2025年注册兽医《兽医临床诊疗学》备考题库及答案解析
- 2025年小学五年级数学上学期单元测试专项训练(含答案)
- 2025宁夏交通建设投资集团有限公司校园招聘和社会招聘230人(1号)考试笔试备考试题及答案解析
- 2025汉中市级机关遴选公务员及选聘事业单位人员(54人)笔试考试备考试题及答案解析
- 2025广东广州市海珠区教育系统高校“优才计划”招聘68人笔试考试参考试题及答案解析
- 甘肃省陇南市西和县2025-2026学年八年级上学期周期学业能力评鉴数学试卷(含解析)
- 2025年中小学课外读物进校园落实情况自查报告
- 防爆弹课件教学课件
- 2025年护理专业单招试题及答案
- 电梯基础知识培训教程课件
- 气质联用仪课件
评论
0/150
提交评论