views:

85

answers:

3

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_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!

A: 

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.

John R. Strohm
No, that produces ERROR_ACCESS_DENIED.
Hans Passant
well, sometimes you don't have a luxury to choose the OSi second Hans - if it's opened by somebody else - it gives the error, but the function returns;
Oleg Ivanov
what I'm trying to see if i can break free from the CreateFile function call if it's taking too long - but being in the same thread, i don't know how to do that. And if, let's say, I pass this m_port variable into a special "opening" thread that i could terminate if it's too long - that would be nice, but - if it's opened successfully from that special thread, which then exit, will it be the same for me in the main thread - I mean, the port's handle - will it be operational to the fullest as if I opened it from the main thread? I'm not very comfortable with the threading...
Oleg Ivanov
@John: CE and the desktop share no common code, so the reliability cannot be compared. Microsoft's desktop OSes have always had trouble (but then pretty much every desktop OS does due to the need to be able to handle nearly infinite hardware configurations). I've run CE systems for months without touching them and if it weren't for things like power failures, I wouldn't be surprised to see one run indefinitely.
ctacke
@ctacke: I stand by my reservations. This might help explain why: http://fourquestions.us/personal/fun/bluescreen.php It is admittedly a fake, but the prospect of seeing even ONE such on a mission- or safety-critical system is too terrifying to me.
John R. Strohm
I second ctackle - it does seem that CE is more stable and predictable than MS's desktop OSs, at least from my company's [combined] experience.
Oleg Ivanov
+2  A: 

You cannot protect yourself against crummy USB device drivers. Shop around for a better one.

Hans Passant
in my case - it's also flaky cables/connectors, unstable devices on the other end of the cable and the like;i'd love to be able to handle this through the code
Oleg Ivanov
Is your code snippet in fact accurate? Or are you hanging on the ReadFile call? Use SetCommTimeouts().
Hans Passant
no, it's the CreateFile that gives me grief; I have the SetCommTimeouts later down the road
Oleg Ivanov
Then my original answer is accurate.
Hans Passant
I have to agree with Hans. If it's hanging in CreateFile it's becasue the driver (which is provided by the OEM, not Microsoft) is crap. CreateFile is a blocking system call, so even putting it on a separate thread is likely to do you no good. Nothing on the bus should cause simply opening the UART to hang.
ctacke
I'm opening the port to connect to a camera, and I already got some statistics showing that when I have bad cables/connectors and/or the particular camera is flaky - I'm getting this problem. My set up is complicated (to say the least) - a serial camera is connected to a serial/USB (FTDI) device, which in turn is represented by the FTDI driver as a COM port
Oleg Ivanov
A camera on a serial port? That's special. No wonder you are having trouble.
Hans Passant
well, it's for control messages, not for the video stream
Oleg Ivanov
+1  A: 

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.

Michael Burr
I will certainly try opening the port in another thread - in the near future - from what I get here, nobody was shocked by this idea, so it might just work out
Oleg Ivanov