C# 用API操作读写串口Word格式文档下载.docx

上传人:b****3 文档编号:16758574 上传时间:2022-11-25 格式:DOCX 页数:14 大小:17.76KB
下载 相关 举报
C# 用API操作读写串口Word格式文档下载.docx_第1页
第1页 / 共14页
C# 用API操作读写串口Word格式文档下载.docx_第2页
第2页 / 共14页
C# 用API操作读写串口Word格式文档下载.docx_第3页
第3页 / 共14页
C# 用API操作读写串口Word格式文档下载.docx_第4页
第4页 / 共14页
C# 用API操作读写串口Word格式文档下载.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

C# 用API操作读写串口Word格式文档下载.docx

《C# 用API操作读写串口Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《C# 用API操作读写串口Word格式文档下载.docx(14页珍藏版)》请在冰豆网上搜索。

C# 用API操作读写串口Word格式文档下载.docx

this.m_ovRead.InternalHigh=0;

this.m_ovRead.Offset=0;

this.m_ovRead.OffsetHigh=0;

}

publicstringPortNum;

//COM1COM2COM3COM4

publicintBaudRate;

//1200,2400,4800,9600

publicbyteByteSize;

//8bits

publicbyteParity;

//0-4=no,odd,even,mark,space

publicbyteStopBits;

//0,1,2=1,1.5,2

publicintReadTimeout;

//10

//commportwin32filehandle

privateinthComm=-1;

publicboolOpened=false;

OVERLAPPEDm_ovWrite=newOVERLAPPED();

OVERLAPPEDm_ovRead=newOVERLAPPED();

//win32apiconstants

privateconstuintGENERIC_READ=0x80000000;

privateconstuintGENERIC_WRITE=0x40000000;

privateconstintOPEN_EXISTING=3;

privateconstintINVALID_HANDLE_VALUE=-1;

[StructLayout(LayoutKind.Sequential)]

/*

privatestructDCB貌似设置有错误位域没设置对

//takenfromcstructinplatformsdk

publicintDCBlength;

//sizeof(DCB)

//currentbaudrate

publicintfBinary;

//binarymode,noEOFcheck

publicintfParity;

//enableparitychecking

publicintfOutxCtsFlow;

//CTSoutputflowcontrol

publicintfOutxDsrFlow;

//DSRoutputflowcontrol

publicintfDtrControl;

//DTRflowcontroltype

publicintfDsrSensitivity;

//DSRsensitivity

publicintfTXContinueOnXoff;

//XOFFcontinuesTx

publicintfOutX;

//XON/XOFFoutflowcontrol

publicintfInX;

//XON/XOFFinflowcontrol

publicintfErrorChar;

//enableerrorreplacement

publicintfNull;

//enablenullstripping

publicintfRtsControl;

//RTSflowcontrol

publicintfAbortOnError;

//abortonerror

publicintfDummy2;

//reserved

publicushortwReserved;

//notcurrentlyused

publicushortXonLim;

//transmitXONthreshold

publicushortXoffLim;

//transmitXOFFthreshold

//numberofbits/byte,4-8

publiccharXonChar;

//TxandRxXONcharacter

publiccharXoffChar;

//TxandRxXOFFcharacter

publiccharErrorChar;

//errorreplacementcharacter

publiccharEofChar;

//endofinputcharacter

publiccharEvtChar;

//receivedeventcharacter

publicushortwReserved1;

//reserved;

donotuse

*/

privatestructDCB

publicintxData;

//x值一个32位

privatestructCOMMTIMEOUTS

publicintReadIntervalTimeout;

publicintReadTotalTimeoutMultiplier;

publicintReadTotalTimeoutConstant;

publicintWriteTotalTimeoutMultiplier;

publicintWriteTotalTimeoutConstant;

privatestructOVERLAPPED

publicintInternal;

publicintInternalHigh;

publicintOffset;

publicintOffsetHigh;

publicinthEvent;

[DllImport("

kernel32.dll"

)]

privatestaticexternintCreateFile(

stringlpFileName,//filename

uintdwDesiredAccess,//accessmode

intdwShareMode,//sharemode

intlpSecurityAttributes,//SD

intdwCreationDisposition,//howtocreate

intdwFlagsAndAttributes,//fileattributes

inthTemplateFile//handletotemplatefile

);

privatestaticexternboolGetCommTimeouts(

inthFile,//handletocommdevice

refCOMMTIMEOUTSlpCommTimeouts//time-outvalues

privatestaticexternboolGetCommState(

inthFile,//handletocommunicationsdevice

refDCBlpDCB//device-controlblock

privatestaticexternboolSetCommState(

privatestaticexternboolSetCommTimeouts(

privatestaticexternboolReadFile(

inthFile,//handletofile

byte[]lpBuffer,//databuffer

intnNumberOfBytesToRead,//numberofbytestoread

refintlpNumberOfBytesRead,//numberofbytesread

refOVERLAPPEDlpOverlapped//overlappedbuffer

privatestaticexternboolWriteFile(

intnNumberOfBytesToWrite,//numberofbytestowrite

refintlpNumberOfBytesWritten,//numberofbyteswritten

privatestaticexternboolCloseHandle(

inthObject//handletoobject

privatestaticexternintGetLastError(

privatestaticexternintWaitForSingleObject(

inthHandle,

intdwMilliseconds

privatestaticexternboolGetOverlappedResult(

inthFile,//handletofile,pipe,orcommdevice

refOVERLAPPEDlpOverlapped,//pointertooverlappedstructure

refintlpNumberOfBytesTransferred,//pointertoactualbytescount

boolbWait//waitflag

privatestaticexternboolPurgeComm(

inthFile,//handletocommunicationsresource

intdwFlags//actiontoperform

privatestaticexternboolSetupComm(

intdwInQueue,//sizeofinputbuffer

intdwOutQueue//sizeofoutputbuffer

privatestaticexternintCreateEvent(

intlpSECURITY_ATTRIBUTES,//传递个NULL就是0

//pointertosecurityattributes

boolbManualReset,//flagformanual-resetevent

boolbInitialState,//flagforinitialstate

intlpName//pointertoevent-objectname

publicvoidOpen()

DCBdcbCommPort=newDCB();

COMMTIMEOUTSctoCommPort=newCOMMTIMEOUTS();

//OPENTHECOMMPORT.

intFILE_FLAG_OVERLAPPED=0x40000000;

hComm=CreateFile(PortNum,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0);

//IFTHEPORTCANNOTBEOPENED,BAILOUT.

if(hComm==INVALID_HANDLE_VALUE)

throw(newApplicationException(this.PortNum+"

串口打开失败"

));

//SETBAUDRATE,PARITY,WORDSIZE,ANDSTOPBITS.

//THEREAREOTHERWAYSOFDOINGSETTINGTHESEBUTTHISISTHEEASIEST.

//IFYOUWANTTOLATERADDCODEFOROTHERBAUDRATES,REMEMBER

//THATTHEARGUMENTFORBuildCommDCBMUSTBEAPOINTERTOASTRING.

//ALSONOTETHATBuildCommDCB()DEFAULTSTONOHANDSHAKING.

dcbCommPort.DCBlength=Marshal.SizeOf(dcbCommPort);

GetCommState(hComm,refdcbCommPort);

dcbCommPort.BaudRate=BaudRate;

dcbCommPort.Parity=Parity;

dcbCommPort.ByteSize=ByteSize;

dcbCommPort.StopBits=StopBits;

dcbCommPort.wReserved1=0;

SetCommState(hComm,refdcbCommPort);

boolret=SetupComm(hComm,200,200);

//十分重要不然会写进去字节过多发不出去那么多

if(!

ret)

MessageBox.Show("

设置缓冲区失败"

);

//SETTHECOMMTIMEOUTS.

GetCommTimeouts(hComm,refctoCommPort);

ctoCommPort.ReadIntervalTimeout=10*9600/this.BaudRate+100;

//9600:

110

ctoCommPort.ReadTotalTimeoutConstant=ReadTimeout;

ctoCommPort.ReadTotalTimeoutMultiplier=10*9600/this.BaudRate;

ctoCommPort.WriteTotalTimeoutMultiplier=0;

ctoCommPort.WriteTotalTimeoutConstant=2*9600/this.BaudRate;

SetCommTimeouts(hComm,refctoCommPort);

Opened=true;

this.ClearInOut();

publicvoidClose()

if(hComm!

=INVALID_HANDLE_VALUE)

CloseHandle(hComm);

Opened=false;

CloseHandle(m_ovRead.hEvent);

CloseHandle(m_ovWrite.hEvent);

publicvoidClearInOut()

if(this.hComm==INVALID_HANDLE_VALUE)

return;

intPURGE_TXABORT=0x0001;

//Killthepending/currentwritestothecommport.

intPURGE_RXABORT=0x0002;

//Killthepending/currentreadstothecommport.

intPURGE_TXCLEAR=0x0004;

//Killthetransmitqueueifthere.

intPURGE_RXCLEAR=0x0008;

//Killthetypeaheadbufferifthere.

PurgeComm(hComm,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);

publicbyte[]Read(intNumBytes)

byte[]BufBytes;

byte[]OutBytes=null;

BufBytes=newbyte[NumBytes];

intERROR_IO_PENDING=997;

intBytesRead=0;

boolbOK=ReadFile(hComm,BufBytes,NumBytes,refBytesRead,refm_ovRead);

if(bOK==true)

OutBytes=newbyte[BytesRead];

Array.Copy(BufBytes,OutBytes,BytesRead);

else

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 公共行政管理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1