公共覆盖 int read (byte[] dest、int timeoutMillis)
{
int numBytesRead = 0;
LOCK (mReadBufferLock)
{
int readAmt = Math.min (dest.Length、mReadBuffer.Length);
mConnection.BulkTransfer (mReadEndpoint、mReadBuffer、
readAmt、1000);// timeoutMillis);
如果(numBytesRead < 0)
{
返回0;
}
Buffer.BlockCopy (mReadBuffer、0、dest、0、numBytesRead);
}
返回数字字节 Read;
}
公共覆盖空 SetDTR (bool 值)
{
mDtr =值;
SetDtrRts();
}
公共覆盖空 SetRTS (bool 值)
{
MRTs =值;
SetDtrRts();
}
公共覆盖空设置参数(int 波特率、int dataBits、stopbits stopbits、奇偶校验)
{
波特率= 115200;
字节 stopBitsByte = 1;
字节 parityBitesByte = 0;
dataBits = 8;
byte[] msg ={
(字节)(波特率和0xff)、
(字节)(((波特率>> 8)& 0xff)、
(字节)(((波特率>> 16)& 0xff)、
(字节)(((波特率>> 24)& 0xff)、
(字节) dataBits、
stopBitsByte、
parityBitesByte
};
mConnection.ControlTransfer (((UsbAddeding)0x21、0x20、0、0、msg、 MSG.Length,5000);
SetDTR (真);
}
公共覆盖 int Write (byte[] src、int timeoutMillis)
{
int offset = 0;
while (offset < src.Length)
{
int writeLength;
int amtWritten;
锁定(mWriteBufferLock)
{
byte[] writeBuffer;
writeLength = System.Math.Min (src.Length - offset、mWriteBuffer.Length);
如果(偏移= 0)
{
writeBuffer = src;
}
其他
{
// bulkTransfer 不支持偏移,请制作副本。
Buffer.BlockCopy (src、offset、mWriteBuffer、0、writeLength);
writeBuffer = mWriteBuffer;
}
amtWritten = mConnection.BulkTransfer (mWriteEndpoint、
writeBuffer、writeLength、timeoutMillis);
}
if (amtWritten <= 0)
{
string temp =$"在偏移量{offset}length={src.Length}处写入{writeLength}字节时出错";
_myLogger.Log (temp);
抛出新 IOException (temp);
}
offset += amtWritten;
}
返回偏移;
}