the*_*ade 1 serial-port visual-studio-2008 visual-c++
我有一个连接在端口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块中).
我是怎么做的?
bytes_written需要是变量的地址; 编译器不会编译您发布的语句.
同样," OVERLAP"没有意义.
你检查一下是否CreateFile成功了?
什么write_buf时候你打电话strlen给它?
尝试复制并粘贴您正在使用的实际代码.
此外,这似乎不是您正在使用的非常好/信息丰富的样本.尝试Windows串口编程和http://msdn.microsoft.com/en-us/library/ms810467.aspx
还可以从Microsoft站点的示例程序开始,在修改它之前对其进行测试(以检查它是否在您的计算机上运行),然后进行修改.