#include "stdafx.h" #include "windows.h" #include using namespace std; HANDLE port; void init() { port = CreateFile("COM2",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); SetupComm(port, 128, 128); DCB dcb; GetCommState(port, &dcb); dcb.BaudRate = 2400; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = TWOSTOPBITS; dcb.fAbortOnError = TRUE; SetCommState(port, &dcb); } void close(HANDLE h) { CloseHandle(h); } unsigned char read() { unsigned char data[2]; DWORD junk; BOOL returnval = ReadFile(port, &data, 1, &junk, NULL); return data[0]; } void write(unsigned char c) { unsigned char data[2]; DWORD junk; data[0] = c; WriteFile(port,&data,1,&junk,NULL); } int _tmain(int argc, _TCHAR* argv[]) { int command; init(); cin >> command; if (command == 0) { cout << "Turning off LED0" << endl; write(0); } else if (command == 1) { cout << "Turning on LED0" << endl; write(1); } else if (command == 2) { cout << "Turning off LED1" << endl; write(2); } else if (command == 3) { cout << "Turning on LED1" << endl; write(3); } else if (command == 4) { cout << "Turning off LED2" << endl; write(4); } else if (command == 5) { cout << "Turning on LED2" << endl; write(5); } else if (command == 6) { cout << "Turning off LED3" << endl; write(6); } else if (command == 7) { cout << "Turning on LED3" << endl; write(7); } else if (command == 8) { cout << "Checking the status of SW0" << endl; write(8); unsigned long data = read(); if (data == 0) { cout << "SW0 is on" << endl; } else { cout << "SW0 is off" << endl; } } else if (command == 9) { cout << "Checking the status of SW0" << endl; write(9); unsigned long data = read(); if (data == 0) { cout << "SW1 is on" << endl; } else { cout << "SW1 is off" << endl; } } else if (command == 10) { cout << "Checking the status of SW0" << endl; write(10); unsigned long data = read(); if (data == 0) { cout << "SW2 is on" << endl; } else { cout << "SW2 is off" << endl; } } else if (command == 11) { cout << "Checking the status of SW0" << endl; write(11); unsigned long data = read(); if (data == 0) { cout << "SW3 is on" << endl; } else { cout << "SW3 is off" << endl; } } else { cout << "Unknown command." << endl; } return 0; }