forked from yan9a/serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conserial.cpp
45 lines (38 loc) · 1.07 KB
/
conserial.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// File: conserial.cpp
// Description: Serial communication console program for Windows and Linux
// WebSite: http://cool-emerald.blogspot.sg/2017/05/serial-port-programming-in-c-with.html
// MIT License (https://opensource.org/licenses/MIT)
// Copyright (c) 2018 Yan Naing Aye
#include <stdio.h>
#include "ceSerial.h"
using namespace std;
using namespace ce;
int main()
{
#ifdef ceWINDOWS
ceSerial com("\\\\.\\COM1",9600,8,'N',1); // Windows
#else
ceSerial com("/dev/ttyS0",9600,8,'N',1); // Linux
#endif
printf("Opening port %s.\n",com.GetPort().c_str());
if (com.Open() == 0) {
printf("OK.\n");
}
else {
printf("Error.\n");
return 1;
}
bool successFlag;
printf("Writing.\n");
char s[]="Hello";
successFlag=com.Write(s); // write string
successFlag=com.WriteChar('!'); // write a character
printf("Waiting 3 seconds.\n");
ceSerial::Delay(3000); // delay to wait for a character
printf("Reading.\n");
char c=com.ReadChar(successFlag); // read a char
if(successFlag) printf("Rx: %c\n",c);
printf("Closing port %s.\n",com.GetPort().c_str());
com.Close();
return 0;
}