📄 用c#读取gps数据.htm
字号:
指定是否允许奇偶校验 enable parity checking <BR> public int
fOutxCtsFlow; //
指定CTS是否用于检测发送控制,当为TRUE是CTS为OFF,发送将被挂起。 CTS output flow control
<BR> public int
fOutxDsrFlow; // 指定CTS是否用于检测发送控制 DSR
output flow control <BR> public int
fDtrControl; //
DTR_CONTROL_DISABLE值将DTR置为OFF, DTR_CONTROL_ENABLE值将DTR置为ON,
DTR_CONTROL_HANDSHAKE允许DTR"握手" DTR flow control type
<BR> public int fDsrSensitivity; //
当该值为TRUE时DSR为OFF时接收的字节被忽略 DSR sensitivity
<BR> public int fTXContinueOnXoff; //
指定当接收缓冲区已满,并且驱动程序已经发送出XoffChar字符时发送是否停止。TRUE时,在接收缓冲区接收到缓冲区已满的字节XoffLim且驱动程序已经发送出XoffChar字符中止接收字节之后,发送继续进行。 FALSE时,在接收缓冲区接收到代表缓冲区已空的字节XonChar且驱动程序已经发送出恢复发送的XonChar之后,发送继续进行。XOFF
continues Tx <BR> public int
fOutX; //
TRUE时,接收到XoffChar之后便停止发送接收到XonChar之后将重新开始 XON/XOFF out flow control
<BR> public int
fInX; //
TRUE时,接收缓冲区接收到代表缓冲区满的XoffLim之后,XoffChar发送出去接收缓冲区接收到代表缓冲区空的XonLim之后,XonChar发送出去
XON/XOFF in flow control <BR> public int
fErrorChar; //
该值为TRUE且fParity为TRUE时,用ErrorChar 成员指定的字符代替奇偶校验错误的接收字符 enable error
replacement <BR> public int
fNull; //
eTRUE时,接收时去掉空(0值)字节 enable null stripping
<BR> public int
fRtsControl; // RTS flow control
<BR> /*RTS_CONTROL_DISABLE时,RTS置为OFF<BR> RTS_CONTROL_ENABLE时,
RTS置为ON<BR> RTS_CONTROL_HANDSHAKE时,<BR> 当接收缓冲区小于半满时RTS为ON<BR> 当接收缓冲区超过四分之三满时RTS为OFF<BR> RTS_CONTROL_TOGGLE时,<BR> 当接收缓冲区仍有剩余字节时RTS为ON
,否则缺省为OFF*/</P>
<P> public int fAbortOnError; //
TRUE时,有错误发生时中止读和写操作 abort on error <BR> public int
fDummy2; // 未使用 reserved
<BR> <BR> public uint
flags;<BR> public ushort
wReserved; //
未使用,必须为0 not currently used <BR> public ushort
XonLim;
// 指定在XON字符发送这前接收缓冲区中可允许的最小字节数 transmit XON threshold
<BR> public ushort
XoffLim;
// 指定在XOFF字符发送这前接收缓冲区中可允许的最小字节数 transmit XOFF threshold
<BR> public byte
ByteSize;
// 指定端口当前使用的数据位 number of bits/byte, 4-8
<BR> public byte
Parity;
//
指定端口当前使用的奇偶校验方法,可能为:EVENPARITY,MARKPARITY,NOPARITY,ODDPARITY
0-4=no,odd,even,mark,space <BR> public byte
StopBits;
// 指定端口当前使用的停止位数,可能为:ONESTOPBIT,ONE5STOPBITS,TWOSTOPBITS 0,1,2
= 1, 1.5, 2 <BR> public char
XonChar;
// 指定用于发送和接收字符XON的值 Tx and Rx XON character
<BR> public char
XoffChar;
// 指定用于发送和接收字符XOFF值 Tx and Rx XOFF character
<BR> public char
ErrorChar; //
本字符用来代替接收到的奇偶校验发生错误时的值 error replacement character
<BR> public char
EofChar;
// 当没有使用二进制模式时,本字符可用来指示数据的结束 end of input character
<BR> public char
EvtChar;
// 当接收到此字符时,会产生一个事件 received event character
<BR> public ushort
wReserved1; // 未使用
reserved; do not use <BR> }</P>
<P> [StructLayout(LayoutKind.Sequential)]<BR> private
struct COMMTIMEOUTS <BR> {
<BR> public int ReadIntervalTimeout;
<BR> public int ReadTotalTimeoutMultiplier;
<BR> public int ReadTotalTimeoutConstant;
<BR> public int WriteTotalTimeoutMultiplier;
<BR> public int WriteTotalTimeoutConstant;
<BR> } </P>
<P> [StructLayout(LayoutKind.Sequential)] <BR> private
struct OVERLAPPED <BR> { <BR> public
int Internal; <BR> public int
InternalHigh; <BR> public int Offset;
<BR> public int OffsetHigh;
<BR> public int hEvent; <BR> }
<BR> <BR> [DllImport("coredll.dll")]<BR> private
static extern int CreateFile(<BR> string
lpFileName,
// 要打开的串口名称<BR> uint
dwDesiredAccess,
// 指定串口的访问方式,一般设置为可读可写方式<BR> int
dwShareMode,
// 指定串口的共享模式,串口不能共享,所以设置为0<BR> int
lpSecurityAttributes, //
设置串口的安全属性,WIN9X下不支持,应设为NULL<BR> int
dwCreationDisposition,
// 对于串口通信,创建方式只能为OPEN_EXISTING<BR> int
dwFlagsAndAttributes,
//
指定串口属性与标志,设置为FILE_FLAG_OVERLAPPED(重叠I/O操作),指定串口以异步方式通信<BR> int
hTemplateFile
//
对于串口通信必须设置为NULL<BR> );<BR> [DllImport("coredll.dll")]<BR> private
static extern bool GetCommState(<BR> int
hFile, //通信设备句柄<BR> ref DCB
lpDCB //
设备控制块DCB<BR> ); <BR> [DllImport("coredll.dll")]<BR> private
static extern bool BuildCommDCB(<BR> string
lpDef, // 设备控制字符串<BR> ref DCB
lpDCB //
设备控制块<BR> );<BR> [DllImport("coredll.dll")]<BR> private
static extern bool SetCommState(<BR> int
hFile, // 通信设备句柄<BR> ref DCB
lpDCB //
设备控制块<BR> );<BR> [DllImport("coredll.dll")]<BR> private
static extern bool GetCommTimeouts(<BR> int
hFile,
// 通信设备句柄 handle to comm device<BR> ref
COMMTIMEOUTS lpCommTimeouts // 超时时间 time-out
values<BR> ); <BR> [DllImport("coredll.dll")] <BR> private
static extern bool SetCommTimeouts(<BR> int
hFile,
// 通信设备句柄 handle to comm device<BR> ref
COMMTIMEOUTS lpCommTimeouts // 超时时间 time-out
values<BR> );<BR> [DllImport("coredll.dll")]<BR> private
static extern bool ReadFile(<BR> int
hFile,
// 通信设备句柄 handle to file<BR> byte[]
lpBuffer,
// 数据缓冲区 data buffer<BR> int
nNumberOfBytesToRead, // 多少字节等待读取 number of bytes to
read<BR> ref int lpNumberOfBytesRead, // 读取多少字节
number of bytes read<BR> ref OVERLAPPED
lpOverlapped // 溢出缓冲区 overlapped
buffer<BR> );<BR> [DllImport("coredll.dll")] <BR> private
static extern bool WriteFile(<BR> int
hFile,
// 通信设备句柄 handle to file<BR> byte[]
lpBuffer,
// 数据缓冲区 data buffer<BR> int
nNumberOfBytesToWrite, // 多少字节等待写入 number of
bytes to write<BR> ref int
lpNumberOfBytesWritten, // 已经写入多少字节 number of bytes
written<BR> ref OVERLAPPED
lpOverlapped // 溢出缓冲区
overlapped
buffer<BR> );<BR> [DllImport("coredll.dll")]<BR> private
static extern bool CloseHandle(<BR> int
hObject // handle to
object<BR> );<BR> [DllImport("coredll.dll")]<BR> private
static extern uint
GetLastError();<BR> <BR> public void
Open()<BR> {<BR> <BR> DCB
dcbCommPort = new DCB();<BR> COMMTIMEOUTS
ctoCommPort = new COMMTIMEOUTS(); <BR>
<BR> // 打开串口 OPEN THE COMM
PORT.<BR> hComm = CreateFile(PortNum ,GENERIC_READ
| GENERIC_WRITE,0, 0,OPEN_EXISTING,0,0);<BR> //
如果串口没有打开,就打开 IF THE PORT CANNOT BE OPENED, BAIL
OUT.<BR> if(hComm == INVALID_HANDLE_VALUE)
<BR> {<BR> throw(new
ApplicationException("非法操作,不能打开串口!"));<BR> }<BR> <BR> //
设置通信超时时间 SET THE COMM
TIMEOUTS.<BR> GetCommTimeouts(hComm,ref
ctoCommPort);<BR> ctoCommPort.ReadTotalTimeoutConstant
=
ReadTimeout;<BR> ctoCommPort.ReadTotalTimeoutMultiplier
= 0;<BR> ctoCommPort.WriteTotalTimeoutMultiplier =
0;<BR> ctoCommPort.WriteTotalTimeoutConstant =
0; <BR> SetCommTimeouts(hComm,ref
ctoCommPort);<BR> <BR> // 设置串口 SET BAUD
RATE, PARITY, WORD SIZE, AND STOP
BITS.<BR> GetCommState(hComm, ref
dcbCommPort);<BR> dcbCommPort.BaudRate=BaudRate;<BR> dcbCommPort.flags=0;<BR> //dcb.fBinary=1;<BR> dcbCommPort.flags|=1;<BR> if
(Parity>0)<BR> {<BR> //dcb.fParity=1<BR> dcbCommPort.flags|=2;<BR> }<BR> dcbCommPort.Parity=Parity;<BR> dcbCommPort.ByteSize=ByteSize;<BR> dcbCommPort.StopBits=StopBits;<BR> if
(!SetCommState(hComm, ref
dcbCommPort))<BR> {<BR> //uint
ErrorNum=GetLastError();<BR> throw(new
ApplicationException("非法操作,不能打开串口!"));<BR> }<BR> //unremark
to see if setting took correctly<BR> //DCB
dcbCommPort2 = new DCB();<BR> //GetCommState(hComm,
ref dcbCommPort2);<BR> Opened =
true;<BR> }<BR> <BR> public void
Close() <BR> {<BR> if
(hComm!=INVALID_HANDLE_VALUE)
<BR> {<BR> CloseHandle(hComm);<BR> }<BR> }<BR> <BR> public
byte[] Read(int NumBytes)
<BR> {<BR> byte[]
BufBytes;<BR> byte[]
OutBytes;<BR> BufBytes = new
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -