-
Notifications
You must be signed in to change notification settings - Fork 1
/
serialDriver.cpp
executable file
·146 lines (137 loc) · 5.25 KB
/
serialDriver.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/******************************************************************************************
* The purpose of this class is to handle interactions with the serial ports. It has nothing
* special in it. Is is simply a serail port driver. It allows other code to read and
* write to the serial port
*
* Author: Daniel Peterson
* Version 1.0 11/23/09
********************************************************************************************/
#include "serialDriver.h"
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <errno.h>
using namespace std;
/********************************************************************************************
* Function Name: serialDriver
* Purpose: constructor for this class
* Arguments: none
* Returns: nothing
*********************************************************************************************/
serialDriver::serialDriver(void)
{
m_open = false;
}
/********************************************************************************************
* Function Name: ~serialDriver
* Purpose: destructor for this class
* Arguments: none
* Returns: nothing
*********************************************************************************************/
serialDriver::~serialDriver(void)
{
m_open = false; //pointless, but does get the point accross
close(fd_serial); //close the file discriptor when we are destroyed
}
/********************************************************************************************
* Function Name: initialize
* Purpose: does the actual initialization of the port
* Arguments: port - the port to initialize
* Returns: 1 on success
*********************************************************************************************/
int serialDriver::initializeSerialPort(char port[])
{
struct termios tty;
struct termios oldtty;
try{
cout << "Creating Serial Port\n" << flush;
const char* p = port;
fd_serial = open(p, O_RDWR | O_NOCTTY | O_NONBLOCK); //try to open the serial port
cout << "Got File Descriptor\n" << flush;
if (fd_serial < 0) { //if opening the port was unsuccessful, print the error to the console line and exit
printf("Error opening the port\n"); //say where the error occured
perror(port); //print the error
throw port;
}
tcgetattr(fd_serial, &oldtty); //save the previous port settings so the computer doesn't complain
bzero(&tty, sizeof(tty)); //make sure the new structure is actually clear
//lets set the new port settings
/**
* B19200 = Baud 19200
* CS8 = 8N1
* CLOCAL = no modem control. We control the port directly
* CREAD = enable recieving characters
**/
tty.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
/**
* ICRNL = map a carriage return to the new line character so when a return is sent, a new line is too
**/
tty.c_iflag = ICRNL;
tty.c_oflag = 0; //set output to raw only
tty.c_lflag = ICANON; //disable echo
int err = tcflush(fd_serial, TCIFLUSH); //flush the old settings
if (err < 0) cout << "Error flushing old settings" << flush;
err = tcsetattr(fd_serial, TCSANOW, &tty); //apply the new settings to the port
if (err < 0) cout << "Error applying new serial port settings" << flush;
m_open = true;
cout << "Serial Port Ready: " << fd_serial << "\n" << flush;
} //end try
catch(...) {
cout << "There was an error initializing the port\n" << flush;
return -1;
}// end catch
}//=====================================================
/********************************************************************************************
* Function Name: readSerial
* Purpose: reads a byte from the serail port
* Arguments: data - a pointer to a character array to write to in memory
* Returns: nothing
*********************************************************************************************/
int serialDriver::readSerial(char *data)
{
try
{
int t = read(fd_serial, data, 1);
if (t < 0) return -1;
return 1;
}
catch(...){cout <<"Error reading port\n" << flush;}
}//======================================================
/********************************************************************************************
* Function Name: writeSerial
* Purpose: writes a byte
* Arguments: data - the byte of data to write
* Returns: 1 on success
*********************************************************************************************/
int serialDriver::writeSerial(char *data, unsigned int size )
{
try
{
int err = write(fd_serial, data, size);
if (err < 0) {cout << "Error writing to the port"; return -1;}
return 1;
}
catch(...){cout << "Error writing: " << strerror(errno) << "\n" << flush; return -1;}
}//=======================================================
/********************************************************************************************
* Function Name: writeSerial
* Purpose: writes a byte
* Arguments: data - the byte of data to write
* Returns: 1 on success
*********************************************************************************************/
int serialDriver::writeSerial(char *data[], unsigned int size )
{
try
{
for (int i = 0; i < size; i++)
{
int err = write(fd_serial, data[i],1);
if (err < 0) {cout << "Error writing to the port"; return -1;}
}
return 1;
}
catch(...){cout << "Error writing: " << strerror(errno) << "\n" << flush; return -1;}
}//=======================================================