📄 sercomm.cs
字号:
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
namespace CADmaping
{
public class SerComm
{
private int mintHandle = INVALID_HANDLE_VALUE;
private int mintPort = 1;
private byte mbytParity = NOPARITY;
private byte mbytDataBits = DATABITS_8;
private byte mbytStopBits = STOPBITS_10;
private int mintBaudRate = CBR_1200;
private int mintTXBufferSize = 512;
private int mintRXBufferSize = 512;
private int mintTimeout = 100;
private byte[] mbytTxBuffer;
private byte[] mbytRxBuffer;
#region "constants"
private const int GENERIC_READ = unchecked((int)0x80000000);
private const int GENERIC_WRITE = 0x40000000;
private const int OPEN_EXISTING = 3;
private const int INVALID_HANDLE_VALUE = -1;
private const int PURGE_TXCLEAR = 0x4;
private const int PURGE_RXCLEAR = 0x8;
private const byte STOPBITS_10 = 0x1;
private const byte STOPBITS_15 = 0x2;
private const byte STOPBITS_20 = 0x4;
private const byte DATABITS_7 = 0x4;
private const byte DATABITS_8 = 0x8;
private const byte EVENPARITY = 2;
private const byte MARKPARITY = 3;
private const byte NOPARITY = 0;
private const byte ODDPARITY = 1;
private const byte SPACEPARITY = 4;
private const int CBR_110 = 110;
private const int CBR_1200 = 1200;
private const int CBR_2400 = 2400;
private const int CBR_4800 = 4800;
private const int CBR_9600 = 9600;
#endregion
#region "Typedefs"
#region "COMMTIMEOUTS"
private struct COMMTIMEOUTS
{
public Int32 ReadIntervalTimeout;
public Int32 ReadTotalTimeoutMultiplier;
public Int32 ReadTotalTimeoutconstant;
public Int32 WriteTotalTimeoutMultiplier;
public Int32 WriteTotalTimeoutconstant;
}
#endregion
#region "DCB"
private struct DCB
{
public int DCBlength;
public int BaudRate;
public int fBinary;
public int fParity;
public int fOutxCtsFlow; // CTS (clear-to-send)
public int fOutxDsrFlow; // DSR (data-set-ready)
public int fDtrControl; // DTR (data-terminal-ready)
public int fDsrSensitivity;
public int fTXContinueOnXoff;
public int fOutX;
public int fInX;
public int fErrorChar;
public int fNull;
public int fRtsControl; // RTS (request-to-send)
public int fAbortOnError;
public int fDummy2;
public Int16 wReserved;
public Int16 XonLim;
public Int16 XoffLim;
public byte ByteSize;
public byte Parity;
public byte StopBits;
public char XonChar;
public char XoffChar;
public char ErrorChar;
public char EofChar;
public char EvtChar;
public Int16 wReserved1;
}
#endregion
#region "OVERLAPPED"
private class OVERLAPPED
{
public int Internal;
public int InternalHigh;
public int Offset;
public int OffsetHigh;
public int hEvent;
}
#endregion
#endregion
#region "API functions"
#region "Comm"
#region "GetCommState"
[DllImport ("coredll.dll")]
private static extern int GetCommState(int hCommDev ,out DCB lpDCB);
#endregion
#region "PurgeComm"
[DllImport ("coredll.dll")]
private static extern int PurgeComm(int hFile, int dwFlags);
#endregion
#region "SetCommState"
[DllImport ("coredll.dll")]
private static extern int SetCommState(int hFile, ref DCB lpDCB);
#endregion
#region "SetCommTimeouts"
[DllImport ("coredll.dll")]
private static extern int SetCommTimeouts(int hFile, COMMTIMEOUTS lpCommTimeouts);
#endregion
#region "SetupComm"
[DllImport ("coredll.dll")]
private static extern int SetupComm(int hFile, int dwInQueue, int dwOutQueue);
#endregion
#endregion
#region "File I/O"
#region "CloseHandle"
[DllImport ("coredll.dll")]
private static extern int CloseHandle(int hObject);
#endregion
#region "CreateFile"
[DllImport ("coredll.dll")]
private static extern int CreateFile(
string lpFileName,
int dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile);
#endregion
#region "ReadFile"
[DllImport ("coredll.dll")]
private static extern int ReadFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToRead,
ref int lpNumberOfBytesRead,
ref OVERLAPPED lpOverlapped);
#endregion
#region "WriteFile"
[DllImport ("coredll.dll")]
private static extern int WriteFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToWrite,
ref int lpNumberOfBytesWritten,
ref OVERLAPPED lpOverlapped);
#endregion
#endregion
#endregion
#region "public instance properties"
#region "BaudRate"
public int BaudRate
{
get
{
return mintBaudRate;
}
set
{
if (value == CBR_1200 ||
value == CBR_2400 ||
value == CBR_4800 ||
value == CBR_9600 )
{
mintBaudRate = value;
}
else
{
throw new Exception("无效的波特率数值!");
}
}
}
#endregion
#region "CommID"
public int CommID
{
get
{
return mintHandle;
}
}
#endregion
#region "CommPort"
public int CommPort
{
get
{
return mintPort;
}
set
{
mintPort = value;
if (mintPort < 1 || mintPort > 16)
{
throw new Exception("Invalid port number");
}
else
{
mintHandle = CreateFile("COM" + mintPort.ToString() + ":",
GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (mintHandle == INVALID_HANDLE_VALUE)
{
throw new Exception("端口已被占用!");
}
else
{
mintHandle = Close(mintHandle);
}
}
}
}
#endregion
#region "DataBits"
public int DataBits
{
get
{
int retval = 0;
switch (mbytDataBits)
{
case DATABITS_7:
retval = 7;
break;
case DATABITS_8:
retval = 8;
break;
}
return retval;
}
set
{
switch (value)
{
case 7:
mbytDataBits = DATABITS_7;
break;
case 8:
mbytDataBits = DATABITS_8;
break;
default:
throw new Exception("无效的数据位!");
}
}
}
#endregion
#region "InputLen"
public int InputLen
{
get
{
return mintRXBufferSize;
}
set
{
mintRXBufferSize = value;
}
}
#endregion
#region "OutputLen"
public int OutputLen
{
get
{
return mintTXBufferSize;
}
set
{
mintTXBufferSize = value;
}
}
#endregion
#region "Parity"
public String Parity
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -