using System;
using System.Runtime.InteropServices;

namespace JustinIO {
?class CommPort {

??public string PortNum;
??public int BaudRate;
??public byte ByteSize;
??public byte Parity; // 0-4=no,odd,even,mark,space
??public byte StopBits; // 0,1,2 = 1, 1.5, 2
??public int ReadTimeout;
??
??//comm port win32 file handle
??private int hComm = -1;
??
??public bool Opened = false;
??
??//win32 api constants
??? private 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)]
??public struct DCB {
???//taken from c struct in platform sdk
???public int DCBlength;?????????? // sizeof(DCB)
???public int BaudRate;??????????? // 指定当前波特率 current baud rate
???// these are the c struct bit fields, bit twiddle flag to set
???public int fBinary;????????? // 指定是否允许二进制模式,在windows95中必须主TRUE binary mode, no EOF check
???public int fParity;????????? // 指定是否允许奇偶校验 enable parity checking
???public int fOutxCtsFlow;????? // 指定CTS是否用于检测发送控制,当为TRUE是CTS为OFF,发送将被挂起。 CTS output flow control
???public int fOutxDsrFlow;????? // 指定CTS是否用于检测发送控制 DSR output flow control
???public int fDtrControl;?????? // DTR_CONTROL_DISABLE值将DTR置为OFF, DTR_CONTROL_ENABLE值将DTR置为ON, DTR_CONTROL_HANDSHAKE允许DTR"握手" DTR flow control type
???public int fDsrSensitivity;?? // 当该值为TRUE时DSR为OFF时接收的字节被忽略 DSR sensitivity
???public int fTXContinueOnXoff; // 指定当接收缓冲区已满,并且驱动程序已经发送出XoffChar字符时发送是否停止。TRUE时,在接收缓冲区接收到缓冲区已满的字节XoffLim且驱动程序已经发送出XoffChar字符中止接收字节之后,发送继续进行。 FALSE时,在接收缓冲区接收到代表缓冲区已空的字节XonChar且驱动程序已经发送出恢复发送的XonChar之后,发送继续进行。XOFF continues Tx
???public int fOutX;????????? // TRUE时,接收到XoffChar之后便停止发送接收到XonChar之后将重新开始 XON/XOFF out flow control
???public int fInX;?????????? // TRUE时,接收缓冲区接收到代表缓冲区满的XoffLim之后,XoffChar发送出去接收缓冲区接收到代表缓冲区空的XonLim之后,XonChar发送出去 XON/XOFF in flow control
???public int fErrorChar;???? // 该值为TRUE且fParity为TRUE时,用ErrorChar 成员指定的字符代替奇偶校验错误的接收字符 enable error replacement
???public int fNull;????????? // eTRUE时,接收时去掉空(0值)字节 enable null stripping
???public int fRtsControl;???? // RTS flow control
???????????/*RTS_CONTROL_DISABLE时,RTS置为OFF
??? ????????RTS_CONTROL_ENABLE时, RTS置为ON
  ??????????RTS_CONTROL_HANDSHAKE时,
  ??????????当接收缓冲区小于半满时RTS为ON
 ??? ????????当接收缓冲区超过四分之三满时RTS为OFF
  ??????????RTS_CONTROL_TOGGLE时,
  ??????????当接收缓冲区仍有剩余字节时RTS为ON ,否则缺省为OFF*/

???public int fAbortOnError;?? // TRUE时,有错误发生时中止读和写操作 abort on error
???public int fDummy2;??????? // 未使用 reserved
???
???public uint flags;
???public ushort wReserved;????????? // 未使用,必须为0 not currently used
???public ushort XonLim;???????????? // 指定在XON字符发送这前接收缓冲区中可允许的最小字节数 transmit XON threshold
???public ushort XoffLim;??????????? // 指定在XOFF字符发送这前接收缓冲区中可允许的最小字节数 transmit XOFF threshold
???public byte ByteSize;?????????? // 指定端口当前使用的数据位?number of bits/byte, 4-8
???public byte Parity;???????????? // 指定端口当前使用的奇偶校验方法,可能为:EVENPARITY,MARKPARITY,NOPARITY,ODDPARITY? 0-4=no,odd,even,mark,space
???public byte StopBits;?????????? // 指定端口当前使用的停止位数,可能为:ONESTOPBIT,ONE5STOPBITS,TWOSTOPBITS? 0,1,2 = 1, 1.5, 2
???public char XonChar;??????????? // 指定用于发送和接收字符XON的值 Tx and Rx XON character
???public char XoffChar;?????????? // 指定用于发送和接收字符XOFF值 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,???????????????????????? // 要打开的串口名称
??? uint dwDesiredAccess,????????????????????? // 指定串口的访问方式,一般设置为可读可写方式
??? int dwShareMode,????????????????????????? // 指定串口的共享模式,串口不能共享,所以设置为0
??? int lpSecurityAttributes, // 设置串口的安全属性,WIN9X下不支持,应设为NULL
??? int dwCreationDisposition,??????????????? // 对于串口通信,创建方式只能为OPEN_EXISTING
??? int dwFlagsAndAttributes,???????????????? // 指定串口属性与标志,设置为FILE_FLAG_OVERLAPPED(重叠I/O操作),指定串口以异步方式通信
??? int hTemplateFile??????????????????????? // 对于串口通信必须设置为NULL
??);
??[DllImport("kernel32.dll")]
??private static extern bool GetCommState(
??? int hFile,? //通信设备句柄
??? ref DCB lpDCB??? // 设备控制块DCB
??);?
??[DllImport("kernel32.dll")]
??private static extern bool BuildCommDCB(
??? string lpDef,? // 设备控制字符串
??? ref DCB lpDCB???? // 设备控制块
??);
??[DllImport("kernel32.dll")]
??private static extern bool SetCommState(
??? int hFile,? // 通信设备句柄
??? ref DCB lpDCB??? // 设备控制块
??);
??[DllImport("kernel32.dll")]
??private static extern bool GetCommTimeouts(
??? int hFile,????????????????? // 通信设备句柄 handle to comm device
??? ref COMMTIMEOUTS lpCommTimeouts? // 超时时间 time-out values
??);?
??[DllImport("kernel32.dll")]?
??private static extern bool SetCommTimeouts(
??? int hFile,????????????????? // 通信设备句柄 handle to comm device
??? ref COMMTIMEOUTS lpCommTimeouts? // 超时时间 time-out values
??);
??[DllImport("kernel32.dll")]
??private static extern bool ReadFile(
??? int hFile,??????????????? // 通信设备句柄 handle to file
??? byte[] lpBuffer,???????????? // 数据缓冲区 data buffer
??? int nNumberOfBytesToRead,? // 多少字节等待读取 number of bytes to read
??? ref int lpNumberOfBytesRead, // 读取多少字节 number of bytes read
??? ref OVERLAPPED lpOverlapped??? // 溢出缓冲区 overlapped buffer
??);
??[DllImport("kernel32.dll")]?
??private static extern bool WriteFile(
??? int hFile,??????????????????? // 通信设备句柄 handle to file
??? byte[] lpBuffer,??????????????? // 数据缓冲区 data buffer
??? int nNumberOfBytesToWrite,???? // 多少字节等待写入 number of bytes to write
??? ref int lpNumberOfBytesWritten,? // 已经写入多少字节 number of bytes written
??? ref OVERLAPPED lpOverlapped??????? // 溢出缓冲区 overlapped buffer
??);
??[DllImport("kernel32.dll")]
??private static extern bool CloseHandle(
??? int hObject?? // handle to object
??);
??[DllImport("kernel32.dll")]
??private static extern uint GetLastError();
??
??public void Open()
??{
??
??????????? DCB dcbCommPort = new DCB();
??????????? COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();?
?? ?
??? // 打开串口 OPEN THE COMM PORT.
??????????? hComm = CreateFile(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("非法操作,不能打开串口!"));
???}
??
???// 设置通信超时时间 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.
???GetCommState(hComm, ref dcbCommPort);
???dcbCommPort.BaudRate=BaudRate;
???dcbCommPort.flags=0;
???//dcb.fBinary=1;
???dcbCommPort.flags|=1;
???if (Parity>0)
???{
?? ?? //dcb.fParity=1
??????? dcbCommPort.flags|=2;
???}
???dcbCommPort.Parity=Parity;
???dcbCommPort.ByteSize=ByteSize;
???dcbCommPort.StopBits=StopBits;
???if (!SetCommState(hComm, ref dcbCommPort))
???{
??? //uint ErrorNum=GetLastError();
?????? throw(new ApplicationException("非法操作,不能打开串口!"));
???}
??? //unremark to see if setting took correctly
??? //DCB dcbCommPort2 = new DCB();
??? //GetCommState(hComm, ref dcbCommPort2);
???Opened = true;
??}
??
??public void Close() {
???if (hComm!=INVALID_HANDLE_VALUE) {
????CloseHandle(hComm);
???}
??}
??
??public byte[] Read(int NumBytes) {
???byte[] BufBytes;
???byte[] OutBytes;
???BufBytes = new byte[NumBytes];
???if (hComm!=INVALID_HANDLE_VALUE) {
????OVERLAPPED ovlCommPort = new OVERLAPPED();
????int BytesRead=0;
????ReadFile(hComm,BufBytes,NumBytes,ref BytesRead,ref ovlCommPort);
????OutBytes = new byte[BytesRead];
????Array.Copy(BufBytes,OutBytes,BytesRead);
???}
???else {
????throw(new ApplicationException("串口未打开!"));
???}
???return OutBytes;
??}
??
??public void Write(byte[] WriteBytes) {
???if (hComm!=INVALID_HANDLE_VALUE) {
????OVERLAPPED ovlCommPort = new OVERLAPPED();
????int BytesWritten = 0;
????WriteFile(hComm,WriteBytes,WriteBytes.Length,ref BytesWritten,ref ovlCommPort);
???}
???else {
????throw(new ApplicationException("串口未打开!"));
???}??
??}
?}

?class HexCon {
?// 把十六进制字符串转换成字节型和把字节型转换成十六进制字符串 converter hex string to byte and byte to hex string
??public static string ByteToString(byte[] InBytes) {
???string StringOut="";
???foreach (byte InByte in InBytes) {
????StringOut=StringOut + String.Format("{0:X2} ",InByte);
???}
???return StringOut;
??}
??public static byte[] StringToByte(string InString) {
???string[] ByteStrings;
???ByteStrings = InString.Split(" ".ToCharArray());
???byte[] ByteOut;
???ByteOut = new byte[ByteStrings.Length-1];
???for (int i = 0;i==ByteStrings.Length-1;i++) {
????ByteOut[i] = Convert.ToByte(("0x" + ByteStrings[i]));
???}
???return ByteOut;
??}
?}
}

串口通讯JustinIO.cs相关推荐

  1. .NET Core 跨平台 串口通讯 ,Windows/Linux 串口通讯

    1,前言 开发环境:在 Visual Studio 2017,.NET Core 2.x 串口通讯用于设备之间,传递数据,物联网设备中广泛使用串口方式连接通讯,物联网通讯协议 :Modbus 协议 A ...

  2. .NET串口通讯解决方案

    网上关于.NET串口通讯的文章不少,但大多只是一些对.NET串口通讯函数的简单封装,对于实际的操作和使用,并没有太直接的帮助.本人做过几个.NET下串口通讯的小项目,借鉴了一些前人的经验(特别是Sha ...

  3. 【转】串口通讯—通信协议

    转:http://cs.nju.edu.cn/yangxc/dcc2003.files/jszc-sub/comif-09.htm 串口通讯-通信协议 所谓通信协议是指通信双方的一种约定.约定包括对数 ...

  4. c#rs232与三菱通讯_C#对三菱PLC的以太网和串口通讯以及台达PLC的以太网通讯

    [实例简介] C#对三菱PLC的以太网和串口通讯以及台达PLC的以太网通讯 [实例截图] [核心代码] Conn └── Conn ├── Conn.sln └── ConnTest ├── bin ...

  5. 锁相放大器sr830_SR830 斯坦福大学的高性能锁相放大器串口通讯源码,用于实时读取 的振幅,相位和频 Windows Develop 238万源代码下载- www.pudn.com...

    文件名称: SR830下载 收藏√  [ 5  4  3  2  1 ] 开发工具: C# 文件大小: 2547 KB 上传时间: 2014-02-13 下载次数: 15 详细说明:斯坦福大学的高性能 ...

  6. php 串口通讯,PHP使用RS232串口通讯传送文件的应用演示

    // -------------------------------------------------------------------------- // File name   : RS232 ...

  7. 串口 发送 接收 高位_电工进阶PLC大神,必备PLC串口通讯的基本知识!

    戳上方蓝字"技成电工课堂"快速关注!!! 电力作业人员在使用PLC的时候会接触到很多的通讯协议以及通讯接口,最基本的PLC串口通讯和基本的通讯接口你都了解吗?1,什么是串口通讯? ...

  8. plc和pc串口通讯接线_让你搞懂PLC串口通讯和通讯接口,这东西估计没几个能说清楚~...

    电力作业人员在使用PLC的时候会接触到很多的通讯协议以及通讯接口,最基本的PLC串口通讯和基本的通讯接口你都了解吗? 1,什么是串口通讯? 串口是计算机上一种非常通用设备通信的协议(不要与通用串行总线 ...

  9. android 串口开发_详细分析Esp8266上电信息打印的数据,如何做到串口通讯上电不乱码打印...

    01 写在前面: 上篇关于如何在内置仅1M的Esp8285做到 OTA 升级的同步到微信公众号,竟然被安信可的某些运维人员看到了,想要转载,我很欣慰,竟然自己的笔记可以被这么大型的公司员工认可! 我是 ...

最新文章

  1. CreateThread、_beginthreadex、AfxBeginThread
  2. Java跨平台实现原理及JVM垃圾回收、内存管理实战
  3. 前端er怎样操作剪切复制以及禁止复制+破解等
  4. Spring如何处理线程并发
  5. matlabif语句怎么用_公益心 码客行(2)—— 简单语句
  6. leetcode 47. 全排列 II 思考分析
  7. 1-4 数组元素的区间删除 (20 分)
  8. 企业实战_04_MyCat常用配置文件详解
  9. crowd counting_[crowd_counting]-SFCN-CVPR2019amp;amp;GCC dataset
  10. 堆栈在DNA计算机中的应用,堆栈和二叉树数据结构在DNA计算机中的设计与实现
  11. MySQL Workbench工具设置编码
  12. PKMS的queryIntentActivities分析
  13. java中的原型模式_java中的原型模式理解
  14. PermissionError: [Errno 13] in python
  15. html 拦截境外ip,Windows服务器系统怎么屏蔽国外IP的访问
  16. retainall java_瞬间教你学会使用java中list的retainAll方法
  17. javascript实现-右下角广告框效果
  18. 【DIY】热水器升级加装远程wifi控制功能,esp8266远程红外控制热水器启动
  19. 家庭wifi部署建议
  20. r语言 rgl 强制过程中_R语言中%||%是什么意思?

热门文章

  1. 桌上骑师的简单人体工程学技巧
  2. 广联达无领导小组面试题 新鲜出炉
  3. arcoshx怎么用计算机算,微积分常用公式及运算法则(上册).pdf.doc
  4. 计算机病毒论文百度百科,计算机病毒解析与防范论文.doc
  5. Cinema 4D初学者终极指南
  6. SMO算法在软间隔SVM分类中的应用
  7. OpenShift - 5分钟自动替换 OpenShift 集群故障节点(附视频)
  8. 【linux驱动分析】之dm9000驱动分析(三):sk_buff结构分析
  9. 【PHP】工银e缴费PHP版本对接?
  10. 微信小程序,时间戳转为日期格式