arudinoとPC間でデータのやり取りをおこなうVC++プログラムを作ろうとしています。
いろいろなサイトを参考にPCからarudinoへの送信はできましたがarudinoからPCへ送信する部分がができていません。
(ReadFile()がFalseになりNG5が表示されます)
ReadFile()の使い方が間違っているのでしょうか?
c++
1#include <windows.h> 2#include <stdio.h> 3#include <stdlib.h> 4 5HANDLE Portarduino; 6bool ready; 7BYTE Rxdata = '0'; 8 9void main(void) { 10 Portarduino = CreateFile(L"COM4", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);//ポート設定 11 if (Portarduino == INVALID_HANDLE_VALUE) { 12 printf("NG1\n"); 13 system("PAUSE"); 14 exit(0); 15 } 16 17 //バッファ初期化 18 ready = SetupComm(Portarduino, 1024, 1024); 19 if (!ready) { 20 printf("NG2\n"); 21 CloseHandle(Portarduino); 22 system("PAUSE"); 23 exit(0); 24 } 25 26 ready = PurgeComm(Portarduino, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR); 27 if (!ready) { 28 printf("NG3\n"); 29 CloseHandle(Portarduino); 30 exit(0); 31 } 32 33 DCB dcb; 34 GetCommState(Portarduino, &dcb); 35 dcb.DCBlength = sizeof(DCB); 36 dcb.BaudRate = 9600; 37 dcb.fBinary = TRUE; 38 dcb.ByteSize = 8; 39 dcb.fParity = NOPARITY; 40 dcb.StopBits = ONESTOPBIT; 41 42 ready = SetCommState(Portarduino, &dcb); 43 if (!ready) { 44 printf("NG4\n"); 45 CloseHandle(Portarduino); 46 system("PAUSE"); 47 exit(0); 48 } 49 50 51 //受信待機 52 DWORD dwRead; 53 while (1) { 54 ready = ReadFile(Portarduino, &Rxdata, sizeof(Rxdata), &dwRead, NULL); 55 if (!ready) { 56 printf("NG5\n"); 57 } 58 printf("%c\n", Rxdata); 59 } 60 61 CloseHandle(Portarduino); 62 system("PAUSE"); 63} 64
arudino
1 2 3void setup() { 4 Serial.begin(9600); // opens serial port, sets data rate to 9600 bps 5} 6 7void loop() { 8 //uint8_t data = 0x00; 9 Serial.write('1'); 10 //delay(1000); 11}
回答2件
あなたの回答
tips
プレビュー