Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include "windows.h"
- using namespace std;
- int main() {
- //create handle to COM-Port
- const char *port = "COM3";
- HANDLE handle = CreateFile(port, GENERIC_ALL, 0, NULL, OPEN_EXISTING, 0, NULL);
- if (handle == INVALID_HANDLE_VALUE) {
- return -1;
- }
- //set baud rate to 9600
- DCB configuration;
- DWORD baudrate = CBR_9600;
- if (!GetCommState(handle, &configuration)) {
- return -1;
- }
- configuration.BaudRate = baudrate;
- if (!SetCommState(handle, &configuration)) {
- return -1;
- }
- //read as long as data is available
- char buffer;
- DWORD bytesRead = 0;
- DWORD bytesWritten = 0;
- while (ReadFile(handle, &buffer, 1, &bytesRead, 0)) {
- cout << buffer;
- //write every character back to the device
- if (!WriteFile(handle, &buffer, 1, &bytesWritten, 0)) {
- return -1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement