我无法使用Visual C++在我的 Arduino 上写入字符串。我以某种方式能够打开 COM 端口,但无法向 Arduino 发送数据。在我的情况下,问题实际上是什么?
int main()
{
HANDLE hComm;
hComm = CreateFileA("\\\\.\\COM11",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
if (hComm == INVALID_HANDLE_VALUE)
{
printf("com not opened");
}
else
{
printf("COM OPENED");
}
COMMTIMEOUTS cto = { 1, 100, 1000, 0, 0 };
DCB dcb;
memset(&dcb,0,sizeof(dcb));
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = 38400;
dcb.fBinary = 1;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = 8;
if(!SetCommState(hComm,&dcb))
{
printf("HI");
}
while(1)
{
char bag[]="L";
DWORD read=0 ;
DWORD write=1; // Number of bytes to write to serial port
// Decmial value to write to serial port
WriteFile(hComm, bag,write,&write, NULL);
}
}