MFC, threads, and serial ports
Hi folks, I'm at a loss as to my problem here. Multithreading is new to me, but examples I've seen make me suspect this should work. I've been writing code to interface with a scientific instrument connected to the computer via serial. The nature of this device and our application structure suggested multithreading would be the way to go for solid support.
I am using Visual C++ 6.0 and MFC, as are the other engineers where I work. I realize the popularity of this platform is decreasing, but it's what I've got for now.
The problem I am having is my ReadFile on the serial port is returning a true value, yet the bytes read parameter is zero. I am using non-overlapped IO. After some reading I'd tried using SetCommMask and WaitCommEvent, to no avail. MFC documentation suggests that I'd see a true return value yet zero bytes read if I was using a named pipe, but I am not.
Here's the other part of the scenario, and it could likely be what's tripping things up. I have a decent serial port class I wrote that encapsulates what you'd expect, opening, closing, reading, writing. My main application thread creates an instance of the class, opens the serial port, and launches the writing/reading thread. It also stashes the serial instance in a global index so I can get at the object from the thread. In the thread, when there's a command for the serial device, I do my locks, then I send the command off to the serial port via my serial object's write function, and then get all set up to block on a read(also a member of the serial object). This is where ReadFile returns true and zero for the number of bytes read.
Any ideas? I can think of a couple of things that might be the case that I'm working on, but I'm a little stumped as most of the things I see suggest that this approach should work, though people aren't using objects like I have been. Thanks for your time.
I am using Visual C++ 6.0 and MFC, as are the other engineers where I work. I realize the popularity of this platform is decreasing, but it's what I've got for now.
The problem I am having is my ReadFile on the serial port is returning a true value, yet the bytes read parameter is zero. I am using non-overlapped IO. After some reading I'd tried using SetCommMask and WaitCommEvent, to no avail. MFC documentation suggests that I'd see a true return value yet zero bytes read if I was using a named pipe, but I am not.
Here's the other part of the scenario, and it could likely be what's tripping things up. I have a decent serial port class I wrote that encapsulates what you'd expect, opening, closing, reading, writing. My main application thread creates an instance of the class, opens the serial port, and launches the writing/reading thread. It also stashes the serial instance in a global index so I can get at the object from the thread. In the thread, when there's a command for the serial device, I do my locks, then I send the command off to the serial port via my serial object's write function, and then get all set up to block on a read(also a member of the serial object). This is where ReadFile returns true and zero for the number of bytes read.
Any ideas? I can think of a couple of things that might be the case that I'm working on, but I'm a little stumped as most of the things I see suggest that this approach should work, though people aren't using objects like I have been. Thanks for your time.
