使用Visual Studio 2008和C进行串行通信(适用于Arduino)
我想使用Visual Studio和C将数据发送到我的Arduino板。我特别需要使用C,因为我使用ARToolKit获取标记并相应地发送数据。
我正在尝试代码
#include #include void main() { system( "MODE COM9: BAUD=9600 PARITY=n DATA=8 STOP=1" ) ; FILE port = fopen( "COM9:", "wb" ) ; printf("hello"); fprintf( port, "s" ) ; fclose( port ) ; }
但它没有得到编译。
我只需要发送数据。
我从你的post中了解到你需要使用visual studio发送数据,而不是将程序写入flash或任何其他东西。 这是我在我的机器上为您制作的示例,它可以发送文本测试。
#include "stdafx.h" #include #include #include int _tmain(int argc, _TCHAR* argv[]) { char test[] = "Hello"; HANDLE hDevice = CreateFile(L"COM2",GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,0); if (hDevice !=INVALID_HANDLE_VALUE) { printf("Port opened! \n"); DCB lpTest; GetCommState(hDevice,&lpTest); lpTest.BaudRate = CBR_9600; lpTest.ByteSize = 8; lpTest.Parity = NOPARITY; lpTest.StopBits = ONESTOPBIT; SetCommState(hDevice,&lpTest); DWORD btsIO; WriteFile(hDevice,test,strlen(test),&btsIO,NULL); CloseHandle(hDevice); } _getch(); return 0; }
首先要检查的是你可以与arduino串口进行通信吗? 如果你有XP或更早版本它带有hyperterm,windows vista + 7不需要像http://ttssh2.sourceforge.jp/这样的东西
ps – 我不确定在系统调用中创建的模式参数是否“粘贴”到调用环境中,或者在shell退出时重置。 有关如何正确执行操作的信息,请参阅Windows串口编程示例
CPPWindows可以工作但它也在C ++中。 对于想要使用OPENCV / ARTOOLKIT发送串行数据的所有人或者用C(而不是C ++)编写的东西。 我发布的代码现在正在运行。 它需要一些改变。 最终代码是:(不是COM端口的更改)
#include #include void main() { FILE* port; system( "MODE COM1: BAUD=9600 PARITY=n DATA=8 STOP=1" ) ; port = fopen( "COM1:", "wb" ) ; printf("s"); fprintf( port, "s" ) ; fclose( port ) ; }