#include "Windows.h" #include "stdio.h" int main() { WCHAR PortName[] = L"COM1"; // 仮想COMポートを指定 // ポートオープン HANDLE handle; handle = CreateFile(PortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (handle == INVALID_HANDLE_VALUE) { printf("CreateFile() error (%d)\n", GetLastError()); CloseHandle(handle); return -1; } // コマンド送信 char SndStr[] = "FREQ1G\r\n"; // 周波数:1G に設定 int nLen = strlen(SndStr); DWORD nSend; BOOL ret = WriteFile(handle, SndStr, nLen, &nSend, 0); if (ret == FALSE) { printf("WriteFile() error (%d)\n", GetLastError()); CloseHandle(handle); return -2; } // 応答受信 char RevStr[256] = { 0 }; DWORD nRecv; int i = 0; while (i < 255) { ReadFile(handle, &RevStr[i], 1, &nRecv, 0); if (nRecv == 1) { if (RevStr[i] == 0x0a) // 文字:LF を受信したら終了 break; i++; } } printf(RevStr); // ポートクローズ CloseHandle(handle); return 0; }