3,245
社区成员
发帖
与我相关
我的任务
分享
com1 = CreateFileW(_T("COM2"),GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if(com1 == INVALID_HANDLE_VALUE)
{
m_Recbox.SetWindowTextW(_T("Create init error!!!"));
}
else
{
m_Recbox.SetWindowTextW(_T("Create init sucessed!!!"));
}
dcb1.DCBlength = sizeof (DCB);
GetCommState(com1,&dcb1);
dcb1.BaudRate=CBR_38400;
dcb1.fBinary=TRUE;//win32必须为true
dcb1.fParity=TRUE;
dcb1.fOutxCtsFlow=FALSE;
dcb1.fOutxDsrFlow=FALSE;
dcb1.fDtrControl=DTR_CONTROL_ENABLE;
//dcb1.fDsrSensitivity=FALSE;
//dcb1.fOutX=FALSE;
//dcb1.fInX=FALSE;
//dcb1.fErrorChar=FALSE;
//dcb1.fNull=FALSE;
dcb1.fRtsControl=RTS_CONTROL_ENABLE;
dcb1.fAbortOnError=FALSE;
dcb1.ByteSize=8;
dcb1.Parity=NOPARITY;
dcb1.StopBits=ONESTOPBIT;
if (SetCommState(com1,&dcb1) ==0)
{
CString string_error;
DWORD serial_error;
m_Recbox.SetWindowTextW(_T("Set init error!!!"));
string_error.FormatMessage("%d",GetLastError());
MessageBoxW((LPCTSTR)string_error,_T("Error"), MB_OK);
}
else
{
m_Recbox.SetWindowTextW(_T("Set init sucessed!!!"));
}
BYTE n=TEXT('A');
if (!WriteFile(com1,&n,0,NULL))
{
m_Recbox.SetWindowTextW(_T("write error!!!"));
}