几乎完全相同的代码,在VC下调试发送接收完全正确
在EVC下调试WinCE就不对
我只把 szBaud.Format("baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopbits);
BuildCommDCB(szBaud, &PortDCB);
改成了:
PortDCB.BaudRate = baud; // Current baud
PortDCB.Parity = FALSE;//parity; // 0-4=no,odd,even,mark,space
PortDCB.ByteSize = databits; // Number of bits/bytes, 4-8
PortDCB.StopBits = stopbits; // 0,1,2 = 1, 1.5, 2
这一改VC下就SetCommState (m_hPort, &PortDCB)不了了,但是ECV却可以通过,WriteFile也可以执行,但是示波器测没波形
而在PC上用VC是有波形的,请问应该怎么解决?
请看代码
BOOL CMySerialPort::Open( UINT portnr, // portnumber (1..4)
UINT baud, // baudrate
char parity, // parity
UINT databits, // databits
UINT stopbits, // stopbits
DWORD dwCommEvents, // EV_RXCHAR, EV_CTS etc
UINT writebuffersize, // size to the writebuffer
UINT readbuffersize) // size to the readbuffer
{
DCB PortDCB;
COMMTIMEOUTS CommTimeouts;
TCHAR strPortName[15];
//CString strPortName;
//CString szBaud;
TCHAR szBaud[29];
Close();
// Open the serial port.
//strPortName.Format("COM%d", portnr);
wsprintf(strPortName, L"COM%d:", portnr);
/*
m_hPort = CreateFile (strPortName, // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE, // Access (read/write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING,// How to open the serial port
0, // Port attributes
NULL); // Handle to port with attribute
// to copy
*/
m_hPort = CreateFile(
strPortName,
GENERIC_READ | GENERIC_WRITE, //允许读和写
0, //独占方式(共享模式)
NULL,
OPEN_EXISTING, //打开而不是创建(创建方式)
0,
NULL
);
// If it fails to open the port, return FALSE.
if ( m_hPort == INVALID_HANDLE_VALUE )
{
return FALSE;
}
PortDCB.DCBlength = sizeof (DCB);
// Get the default port setting information.
if (!GetCommState (m_hPort, &PortDCB))
{
return FALSE;
}
//szBaud.Format("baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopbits);
/*
wsprintf(szBaud, L"baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopbits);
if(!BuildCommDCB(szBaud, &PortDCB))
AfxMessageBox(L"sk");
*/
// Change the DCB structure settings.
PortDCB.BaudRate = baud; // Current baud
PortDCB.Parity = FALSE;//parity; // 0-4=no,odd,even,mark,space
PortDCB.ByteSize = databits; // Number of bits/bytes, 4-8
PortDCB.StopBits = stopbits; // 0,1,2 = 1, 1.5, 2
PortDCB.fBinary = TRUE; // Binary mode; no EOF check
PortDCB.fParity = TRUE; // Enable parity checking.
PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
PortDCB.fDtrControl = DTR_CONTROL_ENABLE; // DTR flow control type
PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
PortDCB.fInX = FALSE; // No XON/XOFF in flow control
PortDCB.fErrorChar = FALSE; // Disable error replacement.
PortDCB.fNull = FALSE; // Disable null stripping.
PortDCB.fRtsControl = RTS_CONTROL_ENABLE; // RTS flow control
PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on
// error.
if (!SetCommState (m_hPort, &PortDCB))
{
return FALSE;
}
// Retrieve the time-out parameters for all read and write operations
// on the port.
GetCommTimeouts (m_hPort, &CommTimeouts);
CommTimeouts.ReadIntervalTimeout = 1000;//MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 1000;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 1000;
CommTimeouts.WriteTotalTimeoutConstant = 1000;
if (!SetCommTimeouts (m_hPort, &CommTimeouts))
{
return FALSE;
}
SetCommMask(m_hPort, dwCommEvents);
// Direct the port to perform extended functions SETDTR and SETRTS.
// SETDTR: Sends the DTR (data-terminal-ready) signal.
// SETRTS: Sends the RTS (request-to-send) signal.
EscapeCommFunction (m_hPort, SETDTR);
EscapeCommFunction (m_hPort, SETRTS);
return TRUE;
}