如题以下是我的串口操作代码public void Open()
{ DCB dcbCommPort = new DCB();
COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS(); // OPEN THE COMM PORT.
hComm = CreateFile(PortName, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); // IF THE PORT CANNOT BE OPENED, BAIL OUT.
if (hComm == INVALID_HANDLE_VALUE)
return; SetupComm(hComm, MAXBLOCK, MAXBLOCK); // SET THE COMM TIMEOUTS.
GetCommTimeouts(hComm, ref ctoCommPort);
ctoCommPort.ReadIntervalTimeout = Int32.MaxValue;
ctoCommPort.ReadTotalTimeoutConstant = ReadTimeout;
ctoCommPort.ReadTotalTimeoutMultiplier = 0;
ctoCommPort.WriteTotalTimeoutMultiplier = 10;
ctoCommPort.WriteTotalTimeoutConstant = 1000;
SetCommTimeouts(hComm, ref ctoCommPort); // SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
// THERE ARE OTHER WAYS OF DOING SETTING THESE BUT THIS IS THE EASIEST.
// IF YOU WANT TO LATER ADD CODE FOR OTHER BAUD RATES, REMEMBER
// THAT THE ARGUMENT FOR BuildCommDCB MUST BE A POINTER TO A STRING.
// ALSO NOTE THAT BuildCommDCB() DEFAULTS TO NO HANDSHAKING.
dcbCommPort.DCBlength = Marshal.SizeOf(dcbCommPort);
GetCommState(hComm, ref dcbCommPort);
dcbCommPort.BaudRate = BaudRate;
dcbCommPort.Parity = Parity;
dcbCommPort.ByteSize = ByteSize;
dcbCommPort.StopBits = StopBits;
SetCommState(hComm, ref dcbCommPort); PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
PurgeComm(hComm, PURGE_TXCLEAR | PURGE_TXABORT); Opened = true;
}
/// <summary>
/// 关闭串口
/// </summary>
public void Close()
{
if (hComm != INVALID_HANDLE_VALUE)
{
if (CloseHandle(hComm))
{
hComm = INVALID_HANDLE_VALUE;
Opened = false;
}
}
}
/// <summary>
/// 写入串口数据
/// </summary>
/// <param name="WriteBytes"></param>
/// <returns></returns>
public int Write(byte[] WriteBytes)
{
int BytesWritten = 0; if (hComm != INVALID_HANDLE_VALUE)
{
COMSTAT ComStat = new COMSTAT(); int dwErrorFlags = 0;
ClearCommError(hComm, ref dwErrorFlags, ref ComStat); if (dwErrorFlags != 0)
PurgeComm(hComm, PURGE_TXCLEAR | PURGE_TXABORT); OVERLAPPED ovlCommPort = new OVERLAPPED();
WriteFile(hComm, WriteBytes, WriteBytes.Length, ref BytesWritten, ref ovlCommPort);
} return BytesWritten;
}
/// <summary>
/// 读取串口数据
/// </summary>
/// <param name="NumBytes"></param>
/// <returns></returns>
public byte[] Read(int NumBytes)
{
byte[] BufBytes;
byte[] OutBytes; BufBytes = new byte[NumBytes]; if (hComm != INVALID_HANDLE_VALUE)
{
COMSTAT ComStat = new COMSTAT(); int dwErrorFlags = 0;
ClearCommError(hComm, ref dwErrorFlags, ref ComStat); if (dwErrorFlags != 0)
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT); OVERLAPPED ovlCommPort = new OVERLAPPED(); int BytesRead = 0; ReadFile(hComm, BufBytes, NumBytes, ref BytesRead, ref ovlCommPort); OutBytes = new byte[BytesRead]; Array.Copy(BufBytes, OutBytes, BytesRead); //if (ComStat.cbInQue > 0)
//{ //}
//else
//{
// throw (new ApplicationException("Comm Port Not Open"));
//}
}
else
{
throw (new ApplicationException("Comm Port Not Open"));
} return OutBytes;
}
}
{ DCB dcbCommPort = new DCB();
COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS(); // OPEN THE COMM PORT.
hComm = CreateFile(PortName, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); // IF THE PORT CANNOT BE OPENED, BAIL OUT.
if (hComm == INVALID_HANDLE_VALUE)
return; SetupComm(hComm, MAXBLOCK, MAXBLOCK); // SET THE COMM TIMEOUTS.
GetCommTimeouts(hComm, ref ctoCommPort);
ctoCommPort.ReadIntervalTimeout = Int32.MaxValue;
ctoCommPort.ReadTotalTimeoutConstant = ReadTimeout;
ctoCommPort.ReadTotalTimeoutMultiplier = 0;
ctoCommPort.WriteTotalTimeoutMultiplier = 10;
ctoCommPort.WriteTotalTimeoutConstant = 1000;
SetCommTimeouts(hComm, ref ctoCommPort); // SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
// THERE ARE OTHER WAYS OF DOING SETTING THESE BUT THIS IS THE EASIEST.
// IF YOU WANT TO LATER ADD CODE FOR OTHER BAUD RATES, REMEMBER
// THAT THE ARGUMENT FOR BuildCommDCB MUST BE A POINTER TO A STRING.
// ALSO NOTE THAT BuildCommDCB() DEFAULTS TO NO HANDSHAKING.
dcbCommPort.DCBlength = Marshal.SizeOf(dcbCommPort);
GetCommState(hComm, ref dcbCommPort);
dcbCommPort.BaudRate = BaudRate;
dcbCommPort.Parity = Parity;
dcbCommPort.ByteSize = ByteSize;
dcbCommPort.StopBits = StopBits;
SetCommState(hComm, ref dcbCommPort); PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
PurgeComm(hComm, PURGE_TXCLEAR | PURGE_TXABORT); Opened = true;
}
/// <summary>
/// 关闭串口
/// </summary>
public void Close()
{
if (hComm != INVALID_HANDLE_VALUE)
{
if (CloseHandle(hComm))
{
hComm = INVALID_HANDLE_VALUE;
Opened = false;
}
}
}
/// <summary>
/// 写入串口数据
/// </summary>
/// <param name="WriteBytes"></param>
/// <returns></returns>
public int Write(byte[] WriteBytes)
{
int BytesWritten = 0; if (hComm != INVALID_HANDLE_VALUE)
{
COMSTAT ComStat = new COMSTAT(); int dwErrorFlags = 0;
ClearCommError(hComm, ref dwErrorFlags, ref ComStat); if (dwErrorFlags != 0)
PurgeComm(hComm, PURGE_TXCLEAR | PURGE_TXABORT); OVERLAPPED ovlCommPort = new OVERLAPPED();
WriteFile(hComm, WriteBytes, WriteBytes.Length, ref BytesWritten, ref ovlCommPort);
} return BytesWritten;
}
/// <summary>
/// 读取串口数据
/// </summary>
/// <param name="NumBytes"></param>
/// <returns></returns>
public byte[] Read(int NumBytes)
{
byte[] BufBytes;
byte[] OutBytes; BufBytes = new byte[NumBytes]; if (hComm != INVALID_HANDLE_VALUE)
{
COMSTAT ComStat = new COMSTAT(); int dwErrorFlags = 0;
ClearCommError(hComm, ref dwErrorFlags, ref ComStat); if (dwErrorFlags != 0)
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT); OVERLAPPED ovlCommPort = new OVERLAPPED(); int BytesRead = 0; ReadFile(hComm, BufBytes, NumBytes, ref BytesRead, ref ovlCommPort); OutBytes = new byte[BytesRead]; Array.Copy(BufBytes, OutBytes, BytesRead); //if (ComStat.cbInQue > 0)
//{ //}
//else
//{
// throw (new ApplicationException("Comm Port Not Open"));
//}
}
else
{
throw (new ApplicationException("Comm Port Not Open"));
} return OutBytes;
}
}
c#下的serialport不会用, 读取返回值有问题,用readbyte去读只返回byte[3],应该返回byte[9]
createfile
setcommdcb
settimeout
setcommevent
createListenThread->监听->读取,这里一样也会是3个。
看下面这组数据
01 03 04 00 B7 03 03 0A 3F E4
01 03 04 00 B7 03 03 0A 3F E4
01 03 04 00 B7 03 03 0A 3F E4
01 03 04 00 B7 03 03 0A 3F E4
01 03 04 00 B7 03 03 0A 3F E4
01 03 04 00 B7 03 04 4B 26
01 03 04 00 B7 03 04 4B 26
01 03 04 00 B7 03 04 4B 26
01 03 04 00 B7 03 04 4B 3F A6 为什么会多个字节