-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample1.cpp
More file actions
executable file
·67 lines (45 loc) · 2.25 KB
/
Copy pathExample1.cpp
File metadata and controls
executable file
·67 lines (45 loc) · 2.25 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*!
\file Example1.cpp
\brief Example code source for class serialib.
This example open the device on ttyACM0.
(USB to RS232 converter under linux).
If the opening is successful, it sends the AT command
and waits for a string being received from the device.
After 5 seconds, if no valid data are received from the
device, reception is giving up.
\author Philippe Lucidarme (University of Angers) <serialib@googlegroups.com>
\version 1.2
\date 05/01/2011
*/
#include <stdio.h>
#include "serialib.h"
#if defined (_WIN32) || defined( _WIN64)
#define DEVICE_PORT "COM1" // COM1 for windows
#endif
#ifdef __linux__
#define DEVICE_PORT "/dev/tty01" // tty01 for linux
#endif
int main()
{
serialib LS; // Object of the serialib class
int Ret; // Used for return values
char Buffer[128];
char buf;
// Open serial port
Ret=LS.Open(DEVICE_PORT,115200); // Open serial link at 115200 bauds
if (Ret!=1) { // If an error occured...
printf ("Error while opening port. Permission problem ?\n"); // ... display a message ...
return Ret; // ... quit the application
}
printf ("Serial port opened successfully !\n");
// Read a char from the serial device
Ret=LS.ReadChar(buf,5000); // read a char to buf, timeout of 5 seconds
// The final character of the string must be a line feed ('\n')
if (Ret>0) // If a string has been read from, print the string
printf ("String read from serial port : %c",buf);
else
printf ("TimeOut reached. No data received !\n"); // If not, print a message.
// Close the connection with the device
LS.Close();
return 0;
}