开发者

MFC Serial Port WriteFile() Crash

开发者 https://www.devze.com 2023-04-06 12:12 出处:网络
I am trying to write to a serial port using the WriteFile() function and when I put some breakpoints to check whether it is writing or not, it doesn\'t. I am trying to pass a string of data through my

I am trying to write to a serial port using the WriteFile() function and when I put some breakpoints to check whether it is writing or not, it doesn't. I am trying to pass a string of data through my COMM port.

BOOL bWriteRC = false;
DWORD iBytesWritten = 0;

//String data: "$ABCEFG,00000020,010000003F800000*##\r\n" ==> stored in a CString

CString sStore = "$ABCEFG,00000020,010000003F800000*##\r\n";

bWriteRC = WriteFile(hdl,sStore.GetBuffer(),sStore.GetLength(),&iBytesWritten,NULL); // doesn't go past this...

Somehow whenever I execute it, the program doesn'开发者_开发百科t go past this function and it stops. Any suggestions on what am I doing wrong?

I tried reducing the GetLength(), made it 14 and it works flawlessly but as soon as the number of bytes to write increases beyond 14, WriteFile() stops to go further.

Thanks in advance.


Considering your question, WriteFile() should always succeed as Alex mentioned earlier. Better check the string which you are trying to write is correct or not and is acceptable on the serial port or not. Sometimes, such small things might be overseen and result in big errors. Other than that, in my opinion it should work if it is a simple write operation.


If it hangs in WriteFile it means that COM port driver does not accept your data, it cannot send it to COM port. It depends on such COM port setting as DTR signal state. You need to check flow control settings.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号