c – 尝试配置COM端口时设置DCB失败
发布时间:2020-12-16 10:40:19 所属栏目:百科 来源:网络整理
导读:我正在尝试编写一个使用串行端口的C MFC应用程序(例如COM8).每次我尝试设置DCB时都会失败.如果有人可以指出我做错了什么,我真的很感激. DCB dcb = {0};dcb.DCBlength = sizeof(DCB);port.Insert( 0,L"\." );m_hComm = CreateFile( port,// Virtual COM
我正在尝试编写一个使用串行端口的C MFC应用程序(例如COM8).每次我尝试设置DCB时都会失败.如果有人可以指出我做错了什么,我真的很感激.
DCB dcb = {0}; dcb.DCBlength = sizeof(DCB); port.Insert( 0,L"\." ); m_hComm = CreateFile( port,// Virtual COM port GENERIC_READ | GENERIC_WRITE,// Access: Read and write 0,// Share: No sharing NULL,// Security: None OPEN_EXISTING,// The COM port already exists. FILE_FLAG_OVERLAPPED,// Asynchronous I/O. NULL // No template file for COM port. ); if ( m_hComm == INVALID_HANDLE_VALUE ) { TRACE(_T("Unable to open COM port.")); ThrowException(); } if ( !::GetCommState( m_hComm,&dcb ) ) { TRACE(_T("CSerialPort : Failed to get the comm state - Error: %d"),GetLastError()); ThrowException(); } dcb.BaudRate = 38400; // Setup the baud rate. dcb.Parity = NOPARITY; // Setup the parity. dcb.ByteSize = 8; // Setup the data bits. dcb.StopBits = 1; // Setup the stop bits. if ( !::SetCommState( m_hComm,&dcb ) ) // <- Fails here. { TRACE(_T("CSerialPort : Failed to set the comm state - Error: %d"),GetLastError()); ThrowException(); } 谢谢. 附加信息:生成的错误代码为87:“参数不正确.” 解决方法
我的钱是这样的:
dcb.StopBits = 1; The MSDN docs说这个关于StopBits:
所以,你要求1.5个停止位,这是一个非常古老的东西,我甚至不记得它来自哪里.可能是电传打字机. 我猜你的驱动程序/硬件支持这种模式的机会很小,因此错误. 所以,将它改为dcb.StopBits = ONESTOPBIT; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |