embedded dev. question - how to break free from a faulty serial port opening?
Under WindowsCE, C++ project, I'm trying to work with devices connected via both "real" serial ports and/or serial-to-usb wrappers ("virtual" serial ports); my problem is - when I try to open the port, if something goes wrong, the function never returns and the system goes into some non-responsive state and has to be eventually rebooted. I need to open the ports from the main thread. The question is - how can I make it happen in a controlled way?? this is the opening code snippet:
std::ostringstream device_name;
device_name << "\\\\.\\COM" << port;
m_port = ::CreateFile(device_name.str().c_str(),
GENERIC_READ | GENERIC开发者_Python百科_WRITE,
0, // exclusive access
NULL, // no security
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, // overlapped I/O
NULL); // null template
any suggestions would be greatly appreciated
thanks!
You cannot protect yourself against crummy USB device drivers. Shop around for a better one.
Why not perform the open in another thread? There's no reason to do anything else in that thread - just open the port and you can use the handle for the opened port in any other thread in your process.
However, I'm not sure what's so screwed up that your CreateFile()
call seems to hang - I wonder if this happens even on another thread whether things will still be stable in your application.
Disclaimer: I've never done WinCE, and I personally would have reservations about doing an embedded system using an operating system from the company that made "Blue Screen of Death" a household term.
Silly question number one: Have you verified that no one else has already opened that particular COM port?
You're asking for exclusive access. If someone else already has it, I'd expect the system to hang your task until the other guy releases the port.
精彩评论