我正在开发一个程序,使用 Arduino UNO 从接近传感器读取读数。虽然我可以使用 Arduino 的内置串行监视器获得读数,但不知何故我无法从 MS VC++ 打开相同的端口。
程序(部分)如下:
int main(void)
{
/*used for port"COM13"*/
HANDLE hCom=INVALID_HANDLE_VALUE;
char input[30];
string ss,ss1,ss2,ss3,ss4;
/*Open "COM13"*/
hCom=CreateFile("COM13",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if(hCom==INVALID_HANDLE_VALUE)
{
printf("can't open file");
}
/*Communication Setting*/
DCB dcb;
memset(&dcb,0,sizeof (DCB));
dcb.DCBlength=sizeof (DCB);
dcb.BaudRate=CBR_9600;
dcb.ByteSize=8;
dcb.Parity=NOPARITY;
dcb.StopBits=ONESTOPBIT;
SetCommState(hCom,&dcb);
while(1)
{
//using the data string inputs, printout the readings, process it etc...
}
}
当我尝试调试它时,我会得到这个错误:
我从调试窗口中得到无法打开文件
,显示在打开端口时出现问题。
一些附加信息:
- 为什么选择 VC++?我还在同一个程序中使用 OpenCV 和一些数学计算,因此我可以更轻松地使用 VC++
- 我还使用 TeraTerm 测试了我的 UNO 程序的数据读取没有问题(=我的 UNO 没有问题)
- 我已经用另一个微 Controller (非 Arduino)测试了上面的程序没有问题。
如果需要,我会上传我的 UNO 程序。
提前致谢!
最佳答案
来自 CreateFile 的 MSDN 页面:
To specify a COM port number greater than 9, use the following syntax: "\.\COM10". This syntax works for all port numbers and hardware that allows COM port numbers to be specified.
关于c++ - 打开 COM 端口时调试断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31848607/