如何用C#向串口送數據(源代碼)

 

None.gif
None.gif 
None.gif 
None.gif如何用C#向(串)並口送數據(源代碼) 
None.gif 
None.gif
using  System;
None.gif
using  System.Runtime.InteropServices;
None.gif
None.gif
namespace  PrintExe
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif 
class writeLpt1
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif  
申明要引用的和並口要調用的API#region 申明要引用的和並口要調用的API
InBlock.gif  
//win32 api constants 
InBlock.gif
  private const uint GENERIC_READ = 0x80000000
InBlock.gif  
private const uint GENERIC_WRITE = 0x40000000
InBlock.gif  
private const int OPEN_EXISTING = 3;       
InBlock.gif  
private const int INVALID_HANDLE_VALUE = -1
InBlock.gif  
private const uint FILE_FLAG_OVERLAPPED = 0x40000000;   //異步通訊
InBlock.gif
  private const int MAXBLOCK = 4096;
InBlock.gif
InBlock.gif  
private const uint PURGE_TXABORT = 0x0001;  // Kill the pending/current writes to the comm port.
InBlock.gif
  private const uint PURGE_RXABORT = 0x0002;  // Kill the pending/current reads to the comm port.
InBlock.gif
  private const uint PURGE_TXCLEAR = 0x0004;  // Kill the transmit queue if there.
InBlock.gif
  private const uint PURGE_RXCLEAR = 0x0008;  // Kill the typeahead buffer if there.
InBlock.gif

InBlock.gif  
private const int EV_RXCHAR = 0x0001;
InBlock.gif
InBlock.gif  
//private const uint EV_RXCHAR = 
InBlock.gif
       
InBlock.gif  [StructLayout(LayoutKind.Sequential)] 
InBlock.gif   
private struct DCB  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
//taken from c struct in platform sdk  
InBlock.gif
   public int DCBlength;          
InBlock.gif   
public int BaudRate;            
InBlock.gif   
public int fBinary;    
InBlock.gif   
public int fParity;    
InBlock.gif   
public int fOutxCtsFlow;  
InBlock.gif   
public int fOutxDsrFlow;  
InBlock.gif   
public int fDtrControl;   
InBlock.gif   
public int fDsrSensitivity;  
InBlock.gif
InBlock.gif  
InBlock.gif   
public int fTXContinueOnXoff; 
InBlock.gif   
public int fOutX;    
InBlock.gif   
public int fInX;    
InBlock.gif   
public int fErrorChar;   
InBlock.gif   
public int fNull;    
InBlock.gif   
public int fRtsControl;   
InBlock.gif   
InBlock.gif   
public int fAbortOnError;  
InBlock.gif   
public int fDummy2;    
InBlock.gif   
public ushort wReserved;        
InBlock.gif
InBlock.gif   
public ushort XonLim;           
InBlock.gif   
public ushort XoffLim;          
InBlock.gif   
public byte ByteSize;           
InBlock.gif   
public byte Parity;             
InBlock.gif   
public byte StopBits;           
InBlock.gif   
public char XonChar;            
InBlock.gif   
public char XoffChar;           
InBlock.gif   
public char ErrorChar;          
InBlock.gif   
InBlock.gif   
InBlock.gif   
public char EofChar;            
InBlock.gif   
public char EvtChar;            
InBlock.gif   
public ushort wReserved1;       
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
InBlock.gif  [StructLayout(LayoutKind.Sequential)] 
InBlock.gif   
private struct COMMTIMEOUTS  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif   
public int ReadIntervalTimeout;  
InBlock.gif   
public int ReadTotalTimeoutMultiplier;  
InBlock.gif   
public int ReadTotalTimeoutConstant;  
InBlock.gif   
public int WriteTotalTimeoutMultiplier;  
InBlock.gif   
public int WriteTotalTimeoutConstant;  
ExpandedSubBlockEnd.gif  }
     
InBlock.gif
InBlock.gif  [StructLayout(LayoutKind.Sequential)]    
InBlock.gif   
private struct OVERLAPPED  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{  
InBlock.gif   
public int  Internal;  
InBlock.gif   
public int  InternalHigh;  
InBlock.gif   
public int  Offset;  
InBlock.gif   
public int  OffsetHigh;  
InBlock.gif   
public int hEvent;  
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  [StructLayout(LayoutKind.Sequential)]
InBlock.gif   
private struct COMSTAT
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//*public int fCtsHold;
InBlock.gif   public int fDsrHold;
InBlock.gif   public int fRlsdHold;
InBlock.gif   public int fXoffHold;
InBlock.gif   public int fXoffSent;
InBlock.gif   public int fEof;
InBlock.gif   public int fTxim;
InBlock.gif   public int fReserved;
InBlock.gif   public int cbInQue;
ExpandedSubBlockEnd.gif   public int cbOutQue;
*/

InBlock.gif   
// Should have a reverse, i don't know why!!!!!
InBlock.gif
   public int cbOutQue;
InBlock.gif   
public int cbInQue;
InBlock.gif   
public int fReserved;
InBlock.gif   
public int fTxim;
InBlock.gif   
public int fEof;
InBlock.gif   
public int fXoffSent;
InBlock.gif   
public int fXoffHold;
InBlock.gif   
public int fRlsdHold;
InBlock.gif   
public int fDsrHold;
InBlock.gif   
public int fCtsHold;  
ExpandedSubBlockEnd.gif  }

InBlock.gif
#if FULLFRAMEWORK
InBlock.gif  [DllImport(
"kernel32")] 
InBlock.gif  
private static extern int CreateFile( 
InBlock.gif   
string lpFileName,                         // file name 
InBlock.gif
   uint dwDesiredAccess,                      // access mode 
InBlock.gif
   int dwShareMode,                          // share mode 
InBlock.gif
   int lpSecurityAttributes, // SD 
InBlock.gif
   int dwCreationDisposition,                // how to create 
InBlock.gif
   int dwFlagsAndAttributes,                 // file attributes 
InBlock.gif
   int hTemplateFile                        // handle to template file 
InBlock.gif
   );
InBlock.gif
#else
InBlock.gif  [DllImport(
"kernel32")]
InBlock.gif  
private static extern int CreateFile( 
InBlock.gif   
string lpFileName,                         // file name 
InBlock.gif
   uint dwDesiredAccess,                      // access mode 
InBlock.gif
   int dwShareMode,                          // share mode 
InBlock.gif
   int lpSecurityAttributes, // SD 
InBlock.gif
   int dwCreationDisposition,                // how to create 
InBlock.gif
   uint dwFlagsAndAttributes,                 // file attributes 
InBlock.gif
   int hTemplateFile                        // handle to template file 
InBlock.gif
   );
InBlock.gif
#endif
InBlock.gif
#if FULLFRAMEWORK
InBlock.gif  [DllImport(
"kernel32")] 
InBlock.gif  
private static extern bool GetCommState( 
InBlock.gif   
int hFile,  // handle to communications device 
InBlock.gif
   ref DCB lpDCB    // device-control block 
InBlock.gif
   );   
InBlock.gif
#else
InBlock.gif  
//取得並口狀態
InBlock.gif
  [DllImport("kernel32")] 
InBlock.gif  
private static extern bool GetCommState( 
InBlock.gif   
int hFile,  // handle to communications device 
InBlock.gif
   ref DCB lpDCB    // device-control block 
InBlock.gif
   );
InBlock.gif
#endif
InBlock.gif
#if FULLFRAMEWORK
InBlock.gif  [DllImport(
"kernel32")] 
InBlock.gif  
private static extern bool BuildCommDCB( 
InBlock.gif   
string lpDef,  // device-control string 
InBlock.gif
   ref DCB lpDCB     // device-control block 
InBlock.gif
   ); 
InBlock.gif
#else
InBlock.gif  [DllImport(
"kernel32")] 
InBlock.gif  
private static extern bool BuildCommDCB( 
InBlock.gif   
string lpDef,  // device-control string 
InBlock.gif
   ref DCB lpDCB     // device-control block 
InBlock.gif
   );
InBlock.gif
#endif
InBlock.gif
#if FULLFRAMEWORK
InBlock.gif  [DllImport(
"kernel32")] 
InBlock.gif  
private static extern bool SetCommState( 
InBlock.gif   
int hFile,  // handle to communications device 
InBlock.gif
   ref DCB lpDCB    // device-control block 
InBlock.gif
   );
InBlock.gif
#else
InBlock.gif  [DllImport(
"kernel32")] 
InBlock.gif  
private static extern bool SetCommState( 
InBlock.gif   
int hFile,  // handle to communications device 
InBlock.gif
   ref DCB lpDCB    // device-control block 
InBlock.gif
   );
InBlock.gif
#endif
InBlock.gif
#if FULLFRAMEWORK
InBlock.gif  [DllImport(
"kernel32")] 
InBlock.gif  
private static extern bool GetCommTimeouts( 
InBlock.gif   
int hFile,                  // handle to comm device 
InBlock.gif
   ref COMMTIMEOUTS lpCommTimeouts  // time-out values 
InBlock.gif
   );
InBlock.gif
#else
InBlock.gif  [DllImport(
"kernel32")] 
InBlock.gif  
private static extern bool GetCommTimeouts( 
InBlock.gif   
int hFile,                  // handle to comm device 
InBlock.gif
   ref COMMTIMEOUTS lpCommTimeouts  // time-out values 
InBlock.gif
   );
InBlock.gif
#endif
InBlock.gif
#if FULLFRAMEWORK
InBlock.gif  [DllImport(
"kernel32")]    
InBlock.gif  
private static extern bool SetCommTimeouts( 
InBlock.gif   
int hFile,                  // handle to comm device 
InBlock.gif
   ref COMMTIMEOUTS lpCommTimeouts  // time-out values 
InBlock.gif
   );
InBlock.gif
#else
InBlock.gif  [DllImport(
"kernel32")]    
InBlock.gif  
private static extern bool SetCommTimeouts( 
InBlock.gif   
int hFile,                  // handle to comm device 
InBlock.gif
   ref COMMTIMEOUTS lpCommTimeouts  // time-out values 
InBlock.gif
   );
InBlock.gif
#endif
InBlock.gif
#if FULLFRAMEWORK
InBlock.gif  [DllImport(
"kernel32")] 
InBlock.gif  
private static extern bool ReadFile( 
InBlock.gif   
int hFile,                // handle to file 
InBlock.gif
   byte[] lpBuffer,             // data buffer 
InBlock.gif
   int nNumberOfBytesToRead,  // number of bytes to read 
InBlock.gif
   ref int lpNumberOfBytesRead, // number of bytes read 
InBlock.gif
   ref OVERLAPPED lpOverlapped    // overlapped buffer 
InBlock.gif
   );
InBlock.gif
#else
InBlock.gif  [DllImport(
"kernel32")] 
InBlock.gif  
private static extern bool ReadFile( 
InBlock.gif   
int hFile,                // handle to file 
InBlock.gif
   byte[] lpBuffer,             // data buffer 
InBlock.gif
   int nNumberOfBytesToRead,  // number of bytes to read 
InBlock.gif
   ref int lpNumberOfBytesRead, // number of bytes read 
InBlock.gif
   ref OVERLAPPED lpOverlapped    // overlapped buffer 
InBlock.gif
   );
InBlock.gif
#endif
InBlock.gif
#if FULLFRAMEWORK
InBlock.gif  [DllImport(
"kernel32")] 
InBlock.gif  
private static extern bool WriteFile( 
InBlock.gif   
int hFile,                    // handle to file 
InBlock.gif
   byte[] lpBuffer,                // data buffer 
InBlock.gif
   int nNumberOfBytesToWrite,     // number of bytes to write 
InBlock.gif
   ref int lpNumberOfBytesWritten,  // number of bytes written 
InBlock.gif
   ref OVERLAPPED lpOverlapped        // overlapped buffer 
InBlock.gif
   ); 
InBlock.gif
#else
InBlock.gif  [DllImport(
"kernel32")] 
InBlock.gif  
private static extern bool WriteFile( 
InBlock.gif   
int hFile,                    // handle to file 
InBlock.gif
   byte[] lpBuffer,                // data buffer 
InBlock.gif
   int nNumberOfBytesToWrite,     // number of bytes to write 
InBlock.gif
   ref int lpNumberOfBytesWritten,  // number of bytes written 
InBlock.gif
   ref OVERLAPPED lpOverlapped        // overlapped buffer 
InBlock.gif
   );
InBlock.gif
#endif
InBlock.gif
#if FULLFRAMEWORK
InBlock.gif  [DllImport(
"kernel32")] 
InBlock.gif  
private static extern bool CloseHandle( 
InBlock.gif   
int hObject   // handle to object 
InBlock.gif
   );
InBlock.gif
#else
InBlock.gif  [DllImport(
"kernel32")] 
InBlock.gif  
private static extern bool CloseHandle( 
InBlock.gif   
int hObject   // handle to object 
InBlock.gif
   );
InBlock.gif
#endif
InBlock.gif
#if FULLFRAMEWORK
InBlock.gif  [DllImport(
"kernel32")]
InBlock.gif  
private static extern bool ClearCommError(
InBlock.gif   
int hFile,     // handle to file
InBlock.gif
   ref int lpErrors,
InBlock.gif   
ref COMSTAT lpStat
InBlock.gif  );
InBlock.gif
#else
InBlock.gif  [DllImport(
"kernel32")]
InBlock.gif  
private static extern bool ClearCommError(
InBlock.gif   
int hFile,     // handle to file
InBlock.gif
   ref int lpErrors,
InBlock.gif   
ref COMSTAT lpStat
InBlock.gif   );
InBlock.gif
#endif
InBlock.gif
#if FULLFRAMEWORK
InBlock.gif  [DllImport(
"kernel32")]
InBlock.gif  
private static extern bool PurgeComm(
InBlock.gif   
int hFile,     // handle to file
InBlock.gif
   uint dwFlags
InBlock.gif   );
InBlock.gif
#else
InBlock.gif  [DllImport(
"kernel32")]
InBlock.gif  
private static extern bool PurgeComm(
InBlock.gif   
int hFile,     // handle to file
InBlock.gif
   uint dwFlags
InBlock.gif   );
InBlock.gif
#endif
InBlock.gif
#if FULLFRAMEWORK
InBlock.gif  [DllImport(
"kernel32")]
InBlock.gif  
private static extern bool SetupComm(
InBlock.gif   
int hFile,
InBlock.gif   
int dwInQueue,
InBlock.gif   
int dwOutQueue
InBlock.gif  );
InBlock.gif
#else
InBlock.gif  [DllImport(
"kernel32")]
InBlock.gif  
private static extern bool SetupComm(
InBlock.gif   
int hFile,
InBlock.gif   
int dwInQueue,
InBlock.gif   
int dwOutQueue
InBlock.gif   );
InBlock.gif
#endif
InBlock.gif
InBlock.gif
#if FULLFRAMEWORK
InBlock.gif  [DllImport(
"kernel32")] 
InBlock.gif  
private static extern bool SetCommMask( 
InBlock.gif  
int hFile,
InBlock.gif  
int dwEvtMask   //梓隴岈璃腔栚鎢
InBlock.gif
   );
InBlock.gif
#else
InBlock.gif  [DllImport(
"kernel32")]
InBlock.gif  
private static extern bool SetCommMask( 
InBlock.gif   
int hFile,
InBlock.gif   
int dwEvtMask   //表明事件的掩碼
InBlock.gif
   );
InBlock.gif
#endif
InBlock.gif
InBlock.gif
#if FULLFRAMEWORK
InBlock.gif  [DllImport(
"kernel32")] 
InBlock.gif  
private static extern bool WaitCommEvent( 
InBlock.gif  
int hFile,
InBlock.gif  
ref int lpEvtMask,
InBlock.gif  
ref OVERLAPPED lpOverlapped
InBlock.gif      
InBlock.gif  );
InBlock.gif
#else
InBlock.gif  [DllImport(
"kernel32")]
InBlock.gif  
private static extern bool WaitCommEvent( 
InBlock.gif   
int hFile,
InBlock.gif   
int dwEvtMask,
InBlock.gif   
ref OVERLAPPED lpOverlapped
InBlock.gif   );
InBlock.gif
#endif
InBlock.gif
InBlock.gif
#if FULLFRAMEWORK
InBlock.gif  [DllImport(
"kernel32")] 
InBlock.gif  
private static extern int CreateEvent( 
InBlock.gif  
int lpSecurityAttributes,
InBlock.gif  
bool bManualReset,
InBlock.gif  
bool bInitialState,
InBlock.gif  
string lpName
InBlock.gif  );
InBlock.gif
#else
InBlock.gif  [DllImport(
"kernel32")]
InBlock.gif  
private static extern int CreateEvent( 
InBlock.gif   
InBlock.gif   
int lpSecurityAttributes,
InBlock.gif   
bool bManualReset,
InBlock.gif   
bool bInitialState,
InBlock.gif   
string lpName
InBlock.gif   );
InBlock.gif
#endif
InBlock.gif  
//private static extern int
ExpandedSubBlockEnd.gif
  #endregion

InBlock.gif
InBlock.gif  
// SerialPort成員
InBlock.gif
  private int hComm = INVALID_HANDLE_VALUE;
InBlock.gif  
private bool bOpened = false;   //標志並口初始化成功與否的標志
InBlock.gif

InBlock.gif  
public bool Opened      //用屬性返回標志變量
ExpandedSubBlockStart.gifContractedSubBlock.gif
  dot.gif{
InBlock.gif   
get
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
return bOpened;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
///並口的初始化函數
InBlock.gif  
///lpFileName 端口名
InBlock.gif  
///baudRate  波特率
InBlock.gif  
///parity  校驗位
InBlock.gif  
///byteSize  數據位
InBlock.gif  
///stopBits  停止位
ExpandedSubBlockEnd.gif  
/// <summary>

InBlock.gif  public  bool OpenPort(string lpFileName,int baudRate,byte parity, byte byteSize, byte stopBits)  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{    
InBlock.gif   
// OPEN THE COMM PORT.   
InBlock.gif   
//hComm = CreateFile(lpFileName ,GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); 
InBlock.gif   
// IF THE PORT CANNOT BE OPENED, BAIL OUT. 
InBlock.gif
   if(hComm == INVALID_HANDLE_VALUE)  
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
// OPEN THE COMM PORT.   
InBlock.gif
    hComm = CreateFile(lpFileName ,GENERIC_READ | GENERIC_WRITE, 00, OPEN_EXISTING, 00); 
InBlock.gif    SetCommMask(hComm,EV_RXCHAR);
InBlock.gif    
//hComm = CreateFile("COM" + PortNum, GENERIC_READ | GENERIC_WRITE,0, 0,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0);
InBlock.gif
    return true
ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
//System.MessageBox.Show("並口已打開");
InBlock.gif
    throw(new ApplicationException("並口已打開"));
ExpandedSubBlockEnd.gif   }

InBlock.gif   
//SetCommMask();
InBlock.gif

InBlock.gif   SetupComm(hComm, MAXBLOCK, MAXBLOCK);
InBlock.gif       
InBlock.gif   
// SET THE COMM TIMEOUTS.
InBlock.gif
   COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();
InBlock.gif   GetCommTimeouts(hComm,
ref ctoCommPort); 
InBlock.gif   ctoCommPort.ReadIntervalTimeout 
= Int32.MaxValue;
InBlock.gif   ctoCommPort.ReadTotalTimeoutConstant 
= 0
InBlock.gif   ctoCommPort.ReadTotalTimeoutMultiplier 
= 0
InBlock.gif   ctoCommPort.WriteTotalTimeoutMultiplier 
= 10
InBlock.gif   ctoCommPort.WriteTotalTimeoutConstant 
= 1000;   
InBlock.gif   SetCommTimeouts(hComm,
ref ctoCommPort); 
InBlock.gif       
InBlock.gif   
// SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS. 
InBlock.gif   
// THERE ARE OTHER WAYS OF DOING SETTING THESE BUT THIS IS THE EASIEST. 
InBlock.gif   
// IF YOU WANT TO LATER ADD CODE FOR OTHER BAUD RATES, REMEMBER 
InBlock.gif   
// THAT THE ARGUMENT FOR BuildCommDCB MUST BE A POINTER TO A STRING. 
InBlock.gif   
// ALSO NOTE THAT BuildCommDCB() DEFAULTS TO NO HANDSHAKING. 
InBlock.gif
   DCB dcbCommPort = new DCB(); 
InBlock.gif   dcbCommPort.DCBlength 
= Marshal.SizeOf(dcbCommPort); 
InBlock.gif   GetCommState(hComm, 
ref dcbCommPort);    //取得並口的狀態
InBlock.gif

InBlock.gif   dcbCommPort.BaudRate 
= baudRate; 
InBlock.gif   dcbCommPort.Parity 
= parity; 
InBlock.gif   dcbCommPort.ByteSize 
= byteSize; 
InBlock.gif   dcbCommPort.StopBits 
= stopBits; 
InBlock.gif
InBlock.gif   
if (!SetCommState(hComm, ref dcbCommPort))
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
throw(new ApplicationException("非法操作不能帶開並口"));
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   PurgeComm(hComm, PURGE_RXCLEAR 
| PURGE_RXABORT);  //清空發送緩沖區的所有數據 
InBlock.gif
   PurgeComm(hComm, PURGE_TXCLEAR | PURGE_TXABORT);  //清空收掃沖的骨有據
InBlock.gif
   SetCommMask(hComm,EV_RXCHAR);
InBlock.gif   bOpened 
= true
InBlock.gif   
return true;
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
// 關閉並口
InBlock.gif
  public bool ClosePort()  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
if (hComm == INVALID_HANDLE_VALUE)  
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
return false;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
InBlock.gif   
if (CloseHandle(hComm))
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    hComm 
= INVALID_HANDLE_VALUE;
InBlock.gif    bOpened 
= false;
InBlock.gif    
return true;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
return false;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
// 往並口寫數據
InBlock.gif
  public bool WritePort(byte[] WriteBytes)  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
if(hComm == INVALID_HANDLE_VALUE)  //如果並口未打開  
ExpandedSubBlockStart.gifContractedSubBlock.gif
   dot.gif
InBlock.gif    
return false;
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   COMSTAT ComStat 
= new COMSTAT();
InBlock.gif   
int dwErrorFlags = 0;
InBlock.gif
InBlock.gif   ClearCommError(hComm, 
ref dwErrorFlags, ref ComStat);
InBlock.gif   
if (dwErrorFlags != 0)
InBlock.gif    PurgeComm(hComm, PURGE_TXCLEAR 
| PURGE_TXABORT);
InBlock.gif
InBlock.gif   OVERLAPPED ovlCommPort 
= new OVERLAPPED(); 
InBlock.gif   
int BytesWritten = 0
InBlock.gif   
// SET THE COMM TIMEOUTS.
InBlock.gif
   COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();
InBlock.gif   GetCommTimeouts(hComm,
ref ctoCommPort); 
InBlock.gif   ctoCommPort.ReadIntervalTimeout 
= Int32.MaxValue;
InBlock.gif   ctoCommPort.ReadTotalTimeoutConstant 
= 0
InBlock.gif   ctoCommPort.ReadTotalTimeoutMultiplier 
= 0
InBlock.gif   ctoCommPort.WriteTotalTimeoutMultiplier 
= 10
InBlock.gif   ctoCommPort.WriteTotalTimeoutConstant 
= 3000;   
InBlock.gif   
if(!SetCommTimeouts(hComm,ref ctoCommPort))
InBlock.gif    
return false
InBlock.gif   WriteFile(hComm, WriteBytes, WriteBytes.Length, 
ref BytesWritten, ref ovlCommPort);
InBlock.gif   
if(BytesWritten>0)
InBlock.gif    
return true;
InBlock.gif   
else
InBlock.gif    
return false;
ExpandedSubBlockEnd.gif  }

InBlock.gif  
InBlock.gif  
// 從並口讀數據
InBlock.gif
  public bool ReadPort(int NumBytes, ref byte[] commRead)  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
if (hComm == INVALID_HANDLE_VALUE)  
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
return false;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
InBlock.gif   COMSTAT ComStat 
= new COMSTAT();
InBlock.gif   
int dwErrorFlags = 0;
InBlock.gif
InBlock.gif   ClearCommError(hComm, 
ref dwErrorFlags, ref ComStat);
InBlock.gif   
if (dwErrorFlags != 0)
InBlock.gif    PurgeComm(hComm, PURGE_RXCLEAR 
| PURGE_RXABORT);
InBlock.gif    
InBlock.gif   
if (ComStat.cbInQue > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    OVERLAPPED ovlCommPort 
= new OVERLAPPED(); 
InBlock.gif    
int BytesRead = 0
InBlock.gif    
return ReadFile(hComm, commRead, NumBytes, ref BytesRead, ref ovlCommPort);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
return false;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }
 
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif 
None.gif 
None.gif
None.gif
原贴地址<a  href='http://spaces.msn.com/members/hunter32/'>http://spaces.msn.com/members/hunter32/</a>

转载于:https://www.cnblogs.com/tory320/archive/2005/08/07/209510.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值