在 Visual C 中写入串行 (Com) 端口
我有一个连接在端口 COM4 上的设备(115200 波特,8-N-1)。根据我在此处找到的示例,我将使用以下内容打开端口:
Keyboard_Handle=CreateFile("\\\\.\\COM4",GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL); if(GetLastError() !=0 || Keyboard_Handle == INVALID_HANDLE_VALUE) { AfxMessageBox("Error opening connection to Keyboard"); exit(1); } char buffer[100]; strcpy(buffer,"baud=115200 parity=N data=8 stop=1"); BuildCommDCB((char*)&buffer,&dcb)) if(GetCommState(Keyboard_Handle, &dcb)) { dcb.BaudRate = CBR_115200; dcb.ByteSize = 8; dcb.Parity = 0; dcb.StopBits = 1; SetCommState(Keyboard_Handle, &dcb); }
稍后在我的代码中我在端口上调用 WriteFile 的方法是:
LPDWORD bytes_written; LPDWORD bytes_read; LPOVERLAPPED OVERLAP; char write_buf[10]; write_buf[0] = 's'; write_buf[1] = '\0'; if(Keyboard_Handle != NULL) { WriteFile(Keyboard_Handle, (LPCVOID)write_buf , strlen(write_buf), bytes_written, OVERLAP); }
每次运行代码时,JIT 调试器都会抱怨未处理的异常(尽管 WriteFile 位于 Try/catch 块内)。
我这样做有什么问题吗?
I have a device that is connected on port COM4, (115200 baud, 8-N-1). Based on examples I found here I'm opening the port with:
Keyboard_Handle=CreateFile("\\\\.\\COM4",GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL); if(GetLastError() !=0 || Keyboard_Handle == INVALID_HANDLE_VALUE) { AfxMessageBox("Error opening connection to Keyboard"); exit(1); } char buffer[100]; strcpy(buffer,"baud=115200 parity=N data=8 stop=1"); BuildCommDCB((char*)&buffer,&dcb)) if(GetCommState(Keyboard_Handle, &dcb)) { dcb.BaudRate = CBR_115200; dcb.ByteSize = 8; dcb.Parity = 0; dcb.StopBits = 1; SetCommState(Keyboard_Handle, &dcb); }
Later in my code I call WriteFile on the port with:
LPDWORD bytes_written; LPDWORD bytes_read; LPOVERLAPPED OVERLAP; char write_buf[10]; write_buf[0] = 's'; write_buf[1] = '\0'; if(Keyboard_Handle != NULL) { WriteFile(Keyboard_Handle, (LPCVOID)write_buf , strlen(write_buf), bytes_written, OVERLAP); }
And every time I run the code I get the JIT Debugger complaining about an unhandled exception (though the WriteFile is inside a Try/catch block).
Is there something wrong with how I'm doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
bytes_writing
需要是变量的地址;编译器不会编译您发布的语句。同样,“
OVERLAP
”没有意义。检查
CreateFile
是否成功?当您调用
strlen
时,write_buf
中包含什么?尝试复制并粘贴您正在使用的实际代码。
此外,这似乎不是您正在使用的一个非常好的/信息丰富的示例。尝试Windows串口编程 和 http://msdn.microsoft.com/en-us/library/ ms810467.aspx
还可以从 Microsoft 站点上的示例程序开始,在修改它之前对其进行测试(以检查它是否在您的计算机上运行),然后进行修改。
bytes_written
needs to be an address of a variable; the compiler wouldn't compile the statement you posted.Likewise, "
OVERLAP
" doesn't make sense.Did you check whether
CreateFile
succeeded?What's in
write_buf
when you callstrlen
on it?Try copy-and-pasting the actual code that you're using.
Also that doesn't seem like a very good/informative sample that you're using. Try Windows Serial Port Programming and http://msdn.microsoft.com/en-us/library/ms810467.aspx
Also start with the sample program from the Microsoft site, test it before you modify it (to check that it's working on your machine) and then modify it.
当你调用
SetCommState
时返回值是0吗?它可能会出错,这可能会导致更多问题。另外,您是否逐行检查过以确保是 WriteFile 调用导致了崩溃?
最后,您的应用程序中可能存在一些防病毒软件或其他软件,导致这些问题(查找模块列表中加载的未知 dll)。
When you call
SetCommState
is the return value 0? It could be erroring out which could be causing more problems.Also, have you stepped through line by line to make sure that it is the WriteFile call that crashes it?
Lastly, you could have some antivirus or other software hooking into your app causing these problems (look for unknown dll's loaded in your module list).