免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C#串口通信编程类(修改版) 这是从网上down下来的一个串口通信类,发现close函数忘记了设置Opened属性为false还有后面string转byte和byte转string的函数有错误,索性删掉了修改后的串口通信类如下:下一篇将把我的测试程序主程序部分全部代码贴出来可以坚强勇敢的用来实现串口通信。using System;using System.Runtime.InteropServices;namespace BusApp/ / / public class mycompublic mycom()/ / TODO: 在此处添加构造函数逻辑/public int PortNum; /1,2,3,4public int BaudRate; /1200,2400,4800,9600public byte ByteSize; /8 bitspublic byte Parity; / 0-4=no,odd,even,mark,space public byte StopBits; / 0,1,2 = 1, 1.5, 2 public int ReadTimeout; /10 /comm port win32 file handleprivate int hComm = -1; public bool Opened = false; /win32 api constantsprivate const uint GENERIC_READ = 0x80000000;private const uint GENERIC_WRITE = 0x40000000;private const int OPEN_EXISTING = 3; private const int INVALID_HANDLE_VALUE = -1; StructLayout(LayoutKind.Sequential)private struct DCB /taken from c struct in platform sdk public int DCBlength; / sizeof(DCB) public int BaudRate; / current baud rate public int fBinary; / binary mode, no EOF check public int fParity; / enable parity checking public int fOutxCtsFlow; / CTS output flow control public int fOutxDsrFlow; / DSR output flow control public int fDtrControl; / DTR flow control type public int fDsrSensitivity; / DSR sensitivity public int fTXContinueOnXoff; / XOFF continues Tx public int fOutX; / XON/XOFF out flow control public int fInX; / XON/XOFF in flow control public int fErrorChar; / enable error replacement public int fNull; / enable null stripping public int fRtsControl; / RTS flow control public int fAbortOnError; / abort on error public int fDummy2; / reserved public ushort wReserved; / not currently used public ushort XonLim; / transmit XON threshold public ushort XoffLim; / transmit XOFF threshold public byte ByteSize; / number of bits/byte, 4-8 public byte Parity; / 0-4=no,odd,even,mark,space public byte StopBits; / 0,1,2 = 1, 1.5, 2 public char XonChar; / Tx and Rx XON character public char XoffChar; / Tx and Rx XOFF character public char ErrorChar; / error replacement character public char EofChar; / end of input character public char EvtChar; / received event character public ushort wReserved1; / reserved; do not use StructLayout(LayoutKind.Sequential)private struct COMMTIMEOUTS public int ReadIntervalTimeout; public int ReadTotalTimeoutMultiplier; public int ReadTotalTimeoutConstant; public int WriteTotalTimeoutMultiplier; public int WriteTotalTimeoutConstant; StructLayout(LayoutKind.Sequential) private struct OVERLAPPED public int Internal; public int InternalHigh; public int Offset; public int OffsetHigh; public int hEvent; DllImport(kernel32.dll)private static extern int CreateFile(string lpFileName, / file nameuint dwDesiredAccess, / access modeint dwShareMode, / share modeint lpSecurityAttributes, / SDint dwCreationDisposition, / how to createint dwFlagsAndAttributes, / file attributesint hTemplateFile / handle to template file);DllImport(kernel32.dll)private static extern bool GetCommState(int hFile, / handle to communications deviceref DCB lpDCB / device-control block); DllImport(kernel32.dll)private static extern bool BuildCommDCB(string lpDef, / device-control stringref DCB lpDCB / device-control block);DllImport(kernel32.dll)private static extern bool SetCommState(int hFile, / handle to communications deviceref DCB lpDCB / device-control block);DllImport(kernel32.dll)private static extern bool GetCommTimeouts(int hFile, / handle to comm deviceref COMMTIMEOUTS lpCommTimeouts / time-out values); DllImport(kernel32.dll) private static extern bool SetCommTimeouts(int hFile, / handle to comm deviceref COMMTIMEOUTS lpCommTimeouts / time-out values);DllImport(kernel32.dll)private static extern bool ReadFile(int hFile, / handle to filebyte lpBuffer, / data bufferint nNumberOfBytesToRead, / number of bytes to readref int lpNumberOfBytesRead, / number of bytes readref OVERLAPPED lpOverlapped / overlapped buffer);DllImport(kernel32.dll) private static extern bool WriteFile(int hFile, / handle to filebyte lpBuffer, / data bufferint nNumberOfBytesToWrite, / number of bytes to writeref int lpNumberOfBytesWritten, / number of bytes writtenref OVERLAPPED lpOverlapped / overlapped buffer);DllImport(kernel32.dll)private static extern bool CloseHandle(int hObject / handle to object); public void Open() DCB dcbCommPort = new DCB();COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS(); / OPEN THE COMM PORT. hComm = CreateFile(COM + PortNum ,GENERIC_READ | GENERIC_WRITE,0, 0,OPEN_EXISTING,0,0); / IF THE PORT CANNOT BE OPENED, BAIL OUT.if(hComm = INVALID_HANDLE_VALUE) throw(new ApplicationException(Comm Port Can Not Be Opened); / SET THE COMM TIMEOUTS. GetCommTimeouts(hComm,ref ctoCommPort);ctoCommPort.ReadTotalTimeoutConstant = ReadTimeout;ctoCommPort.ReadTotalTimeoutMultiplier = 0;ctoCommPort.WriteTotalTimeoutMultiplier = 0;ctoCommPort.WriteTotalTimeoutConstant = 0; SetCommTimeouts(hComm,ref ctoCommPort); / SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS./ THERE ARE OTHER WAYS OF DOING SETTING THESE BUT THIS IS THE EASIEST./ IF YOU WANT TO LATER ADD CODE FOR OTHER BAUD RATES, REMEMBER/ THAT THE ARGUMENT FOR BuildCommDCB MUST BE A POINTER TO A STRING./ ALSO NOTE THAT BuildCommDCB() DEFAULTS TO NO HANDSHAKING. dcbCommPort.DCBlength = Marshal.SizeOf(dcbCommPort);GetCommState(hComm, ref dcbCommPort);dcbCommPort.BaudRate=BaudRate;dcbCommPort.Parity=Parity;dcbCommPort.ByteSize=ByteSize;dcbCommPort.StopBits=StopBits;SetCommState(hComm, ref dcbCommPort); Opened = true; public void Close() if (hComm!=INVALID_HANDLE_VALUE) CloseHandle(hComm); Opened=false; public byte Read(int NumBytes) byte BufBytes;byte Ou
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 塑木折叠晾衣架创新创业项目商业计划书
- (2025)国家开放大学电大专科《市场营销学》期末试题及答案
- 2025年跨学科肿瘤科(乳腺癌)靶向治疗(肿瘤内+病理+影像)路径优化考核试卷
- 人教版(2024)八年级上册英语Unit 3 Same or Different 单元复习练习题(含答案)
- 2024年六安霍山县消防救援局招聘政府专职消防员真题
- 2025年秀山土家族苗族自治县辅警招聘考试真题含答案详解(预热题)
- 2025年濮阳辅警协警招聘考试真题附答案详解(模拟题)
- 2025年铁岭辅警招聘考试题库附答案详解(完整版)
- 2025年遵义辅警协警招聘考试真题及1套参考答案详解
- 2025年漳州辅警协警招聘考试真题及一套答案详解
- DBJ04-T306-2025 建筑基坑工程技术标准
- 2025年俄语等级考试试卷及答案
- 货运代理安全管理制度
- GB/T 4446-2025造船与海上结构物系泊绞车
- 王缉慈编著《现代工业地理学》
- 病历车管理制度
- 国家广播电视总局直属事业单位招聘笔试真题2024
- 2024年系统架构师考试全面分析试题及答案
- 起重吊装安全作业专项方案
- 《思想道德与法治》(23版):绪论 担当复兴大任 成就时代新人
- 离婚协议书正规打印电子版(2025年版)
评论
0/150
提交评论