File: testport.cc

package info (click to toggle)
picprog 1.9.0-1.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 296 kB
  • ctags: 181
  • sloc: cpp: 2,962; makefile: 83; sh: 79
file content (207 lines) | stat: -rw-r--r-- 5,014 bytes parent folder | download
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* -*- c++ -*-

This is Testport, simple serial port testing program.
Copyright © 2007 Jaakko Hyvätti

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

The author may be contacted at:

Email: Jaakko.Hyvatti@iki.fi
URL:   http://www.iki.fi/hyvatti/
Phone: +358 40 5011222

Please send any suggestions, bug reports, success stories etc. to the
Email address above.

*/

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

#include <sys/ioctl.h>
#include <sys/io.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <sysexits.h>
#include <string.h>
#include <sched.h>

using namespace std;

#ifndef O_NONBLOCK
#define O_NONBLOCK O_NDELAY
#endif

int modem_signals[] = {
  TIOCM_CD,
  TIOCM_RI,
  TIOCM_CTS,
  TIOCM_DSR,
  TIOCM_RTS,
  TIOCM_DTR
};
const char * modem_signal_names[] = {
  "CD",
  "RI",
  "CTS",
  "DSR",
  "RTS",
  "DTR"
};
const int num_signals = sizeof(modem_signals) / sizeof(modem_signals[0]);

int
main (int argc, char **argv)
{
  int fd;
  struct termios saved, termstate, ttysaved, ttystate;
  int modembits;
  const char *portname;
  bool break_on = false;
  int signal_mask = 0;
  for (int s = 0; s < num_signals; ++s)
    signal_mask |= modem_signals[s];

  if (argc != 2) {
    cerr << "Usage: " << argv[0] << " /dev/ttyS0" << endl;
    exit (1);
  }

  portname = argv[1];

  cout << "Keys:" << endl
       << "q\tquit" << endl
       << "b\ttoggle break" << endl
       << "r\ttoggle rts" << endl
       << "d\ttoggle dtr" << endl
       << "\tother keys transmit that char." << endl
    ;

  if (0 > (fd = open (portname, O_RDWR|O_NOCTTY|O_NONBLOCK))) {
    int e = errno;
    cerr << "Unable to open tty " << portname << ":" << strerror (e) << endl;
    exit (EX_IOERR);
  }
  tcgetattr (fd, &saved);
  termstate = saved;
  termstate.c_iflag = IGNBRK | IGNPAR;
  termstate.c_oflag = 0;
  termstate.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
  termstate.c_lflag = 0;
  termstate.c_cc[VMIN] = 0;
  termstate.c_cc[VTIME] = 0;
  tcsetattr (fd, TCSANOW, &termstate);

  ioctl (fd, TIOCMGET, &modembits);

  if (isatty (0)) {
    tcgetattr (0, &ttysaved);
    ttystate = ttysaved;
    ttystate.c_iflag = IGNBRK | IGNPAR;
    ttystate.c_oflag = OPOST | ONLCR;
    ttystate.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
    ttystate.c_lflag = 0;
    ttystate.c_cc[VMIN] = 0;
    ttystate.c_cc[VTIME] = 0;
    tcsetattr (0, TCSANOW, &ttystate);
    cout << "Terminal set up." << endl;
  }

  for (;;) {
    int n;
    char instr[2];
    n = read (0, instr, 1);
    if (n < 0) {
      int e = errno;
      cerr << "Read error on tty " << strerror (e) << endl;
      break;
    }
    if (n == 1) {
      if (instr[0] == 'q' || instr[0] == 3)
	break;
      if (instr[0] == 'b') {
	break_on = !break_on;
	if (0 > ioctl (fd, break_on ? TIOCSBRK : TIOCCBRK, 0)) {
	  int e = errno;
	  cerr << "Unable to set break on tty "
	       << portname << ":" << strerror (e) << endl;
	  break;
	}
	continue;
      }
      if (instr[0] == 'r') {
	int newmodem = modembits;
	newmodem ^= TIOCM_RTS;
	ioctl (fd, TIOCMSET, &newmodem);
	continue;
      }
      if (instr[0] == 'd') {
	int newmodem = modembits;
	newmodem ^= TIOCM_DTR;
	ioctl (fd, TIOCMSET, &newmodem);
	continue;
      }
      cout << "OUT: 0x" << hex << int(instr[0]) << dec << endl;
      if (1 != write (fd, instr, 1)) {
	cerr << "Write err" << strerror(errno) << endl;
	break;
      }
      continue;
    } // read 1 char from keyboard

    n = read (fd, instr, 1);
    if (n < 0) {
      int e = errno;
      cerr << "Read error on serial port " << strerror (e) << endl;
      break;
    }
    if (n == 1) {
      cout << "IN: 0x" << hex << int(instr[0]) << dec << endl;
    }

    int newmodem;
    ioctl (fd, TIOCMGET, &newmodem);
    if ((newmodem & signal_mask) != (modembits & signal_mask)) {
      string message;
      for (int s = 0; s < num_signals; ++s) {
	string sstate
	  = ((newmodem & modem_signals[s]) ? string("+") : string("-"))
	  + string(modem_signal_names[s]);
	cout << sstate << ' ';
	if ((newmodem & modem_signals[s]) != (modembits & modem_signals[s])) {
	  message += " " + sstate;
	}
      }
      cout << ':' << message << endl;
      modembits = newmodem;
    }

  } // for ever;

  if (isatty (0)) {
    tcsetattr (0, TCSANOW, &ttysaved);
  }
  tcsetattr (fd, TCSANOW, &saved);
  close (fd);
  cout << "Quit." << endl;
  return 0;
}