|
COM4我也很疑惑,不过是可以打开的。内核占用了COM2(这个占用已经不是硬件上的串口占用)。cpu的三个串口不是对应于COM1(跟内核调试共用的)、COM3和COM4吗?不过串口注册表上我确实没有看到有COM4的注册表,但是有COM3的,这个也不正常呀。以下是注册表,各位看看:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; UART0 (physical COM1 connector P1) (Serial)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
IF BSP_NOSERIAL !
[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\SER2440]
"DeviceArrayIndex"=dword:0
"Irq"=dword:03
"IoBase"=dword:50000000
"IoLen"=dword:2C
"Prefix"="COM"
"Dll"="SER2440.Dll"
"Order"=dword:1
"Priority"=dword:0
"Port"="COM1:"
"DeviceType"=dword:0 ; null modem
"FriendlyName"="S2440 COM1"
; "Index"=dword:2
"IClass"="{A32942B7-920C-486b-B0E6-92A702A99B35}"
ENDIF BSP_NOSERIAL !
[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\SER2440\Unimodem]
"Tsp"="Unimodem.dll"
"DeviceType"=dword:0
"FriendlyName"="SER2440 UNIMODEM"
"DevConfig"=hex: 10,00, 00,00, 05,00,00,00, 10,01,00,00, 00,4B,00,00, 00,00, 08, 00, 00, 00,00,00,00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; UART1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
IF BSP_NOSERIAL !
[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\SER2440_1]
"DeviceArrayIndex"=dword:1
"Irq"=dword:17
"IoBase"=dword:50004000
"IoLen"=dword:2C
"Prefix"="COM"
"Dll"="SER2440.Dll"
"Order"=dword:3
"Priority"=dword:0
"Port"="COM2:"
"DeviceType"=dword:0 ; null modem
"FriendlyName"="S2440 COM2"
; "Index"=dword:2
"IClass"="{A32942B7-920C-486b-B0E6-92A702A99B35}"
ENDIF BSP_NOSERIAL !
[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\SER2440_1\Unimodem]
"Tsp"="Unimodem.dll"
"DeviceType"=dword:0
"FriendlyName"="SER2440 UNIMODEM"
"DevConfig"=hex: 10,00, 00,00, 05,00,00,00, 10,01,00,00, 00,4B,00,00, 00,00, 08, 00, 00, 00,00,00,00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; UART2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
IF BSP_NOSERIAL !
[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\SER2440_2]
"DeviceArrayIndex"=dword:2
"Irq"=dword:18
"IoBase"=dword:50008000
"IoLen"=dword:2C
"Prefix"="COM"
"Dll"="SER2440.Dll"
"Order"=dword:4
"Priority"=dword:0
"Port"="COM3:"
"DeviceType"=dword:0 ; null modem
"FriendlyName"="S2440 COM3"
; "Index"=dword:2
"IClass"="{A32942B7-920C-486b-B0E6-92A702A99B35}"
ENDIF BSP_NOSERIAL !
[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\SER2440_2\Unimodem]
"Tsp"="Unimodem.dll"
"DeviceType"=dword:0
"FriendlyName"="SER2440 UNIMODEM"
"DevConfig"=hex: 10,00, 00,00, 05,00,00,00, 10,01,00,00, 00,4B,00,00, 00,00, 08, 00, 00, 00,00,00,00
应用程序打开代码如下:
打开串口
m_hPort = CreateFile(s2w(sPortName).c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if(INVALID_HANDLE_VALUE==m_hPort)
{
return false;
}
DCB PortDCB;
PortDCB.DCBlength = sizeof(DCB);
if(!GetCommState(m_hPort, &PortDCB))
{
Close();
return false;
}
PortDCB.BaudRate = nBaundRate;
PortDCB.ByteSize = nDataBits;
switch(cParity)
{
case 'N':
PortDCB.Parity = NOPARITY;
break;
case 'E':
PortDCB.Parity = ODDPARITY;
break;
case 'O':
PortDCB.Parity = EVENPARITY;
break;
}
switch(nStopBits)
{
case 1:
PortDCB.StopBits = ONESTOPBIT;
break;
case 2:
PortDCB.StopBits = TWOSTOPBITS;
break;
}
//if("COM3:"==sPortName || "COM4:"==sPortName)
//{
//PortDCB.fOutxCtsFlow = TRUE;
//PortDCB.fRtsControl = RTS_CONTROL_HANDSHAKE;
//PortDCB.fOutxDsrFlow = FALSE;
//PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
//PortDCB.fRtsControl = RTS_CONTROL_DISABLE;
//PortDCB.fDtrControl = DTR_CONTROL_DISABLE;
//}
//else
{
//PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
}
if(!SetCommState(m_hPort, &PortDCB))
{
Close();
return false;
}
COMMTIMEOUTS CommTimeouts;
CommTimeouts.ReadIntervalTimeout = 100;
CommTimeouts.ReadTotalTimeoutMultiplier = 100;
CommTimeouts.ReadTotalTimeoutConstant = 500;
CommTimeouts.WriteTotalTimeoutMultiplier = 100;
CommTimeouts.WriteTotalTimeoutConstant = 500;
/*
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 0;
*/
if(!SetCommTimeouts(m_hPort, &CommTimeouts))
{
Close();
return false;
}
if(!PurgeComm(m_hPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR))
{
Close();
return false;
}
发送数据:
bool CSerialPort::Send(const char * pData, size_t uDataSize)
{
if(INVALID_HANDLE_VALUE==m_hPort || pData == NULL)
return false;
size_t uPos = 0;
DWORD dwNumberOfBytesWritten = 0;
while(uPos < uDataSize)
{
if(!WriteFile(m_hPort, pData + uPos, uDataSize - uPos, &dwNumberOfBytesWritten, NULL))
{
break;
}
if (dwNumberOfBytesWritten == 0) //一个数据都没有写进去,也跳出了
{
break;
}
uPos += dwNumberOfBytesWritten;
}
return uPos==uDataSize;
}
|
|