public int Write(byte[] WriteBytes) { int result = 0; if (this.hComm != -1) { try { SerialPort.OVERLAPPED oVERLAPPED = default(SerialPort.OVERLAPPED); SerialPort.WriteFile(this.hComm, WriteBytes, WriteBytes.Length, ref result, ref oVERLAPPED); } catch { LogMSG.AddInfo("串口写入错误!", 2); } } return(result); }
public byte[] Read(int NumBytes) { byte[] array = new byte[NumBytes]; byte[] array2 = new byte[0]; if (this.hComm != -1) { try { SerialPort.OVERLAPPED oVERLAPPED = default(SerialPort.OVERLAPPED); int num = 0; SerialPort.ReadFile(this.hComm, array, NumBytes, ref num, ref oVERLAPPED); array2 = new byte[num]; Array.Copy(array, array2, num); } catch { LogMSG.AddInfo("串口读数据错误!", 2); } } return(array2); }
private static extern int WriteFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, ref int lpNumberOfBytesWritten, ref SerialPort.OVERLAPPED lpOverlapped);
private static extern int ReadFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToRead, ref int lpNumberOfBytesRead, ref SerialPort.OVERLAPPED lpOverlapped);