C++ 向串列埠傳輸資料
#include <iostream>
#include <Windows.h>
#include<tchar.h>
using namespace std;
int main()
{
std::cout << "Start Test . . .\n";
HANDLE hcom;
hcom = CreateFile(L"COM4", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL, NULL);
if (hcom == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "開啟串列埠失敗!\n");
exit(0);
}
SetupComm(hcom, 1024, 1024);
DCB dcb;
GetCommState(hcom, &dcb);
dcb.BaudRate = 115200;
dcb.ByteSize = 8;
dcb.Parity = 0;
dcb.StopBits = 1;
SetCommState(hcom, &dcb);
string data[] = { "*IDN?\n", "RFO 1\n", "RFO 2\n", "RFO 3\n", "RFO 4\n", "RFO 1\n", "RFO 2\n", "RFO 3\n" };
DWORD dwWrittenLen = 0;
WriteFile(hcom, "*RST \n", strlen("*RST \n"), &dwWrittenLen, NULL);
Sleep(2 * 1000);
WriteFile(hcom, "RFO 2\n", strlen("RFO 2\n"), &dwWrittenLen, NULL);
Sleep(2 * 1000);
WriteFile(hcom, "RFO 1\n", strlen("RFO 1\n"), &dwWrittenLen, NULL);
Sleep(2 * 1000);
WriteFile(hcom, "RFO 3\n", strlen("RFO 3\n"), &dwWrittenLen, NULL);
Sleep(2 * 1000);
WriteFile(hcom, "RFO 4\n", strlen("RFO 4\n"), &dwWrittenLen, NULL);
return 0;