File: serial.c

package info (click to toggle)
libcdk5 5.0.20250116-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,584 kB
  • sloc: ansic: 32,706; sh: 5,364; makefile: 1,198; sed: 48; cpp: 41
file content (264 lines) | stat: -rw-r--r-- 5,701 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* $Id: serial.c,v 1.9 2021/01/09 22:41:57 tom Exp $ */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/termios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <time.h>

#include <cdk_test.h>

#ifdef HAVE_XCURSES
char *XCursesProgramName = "serial";
#endif

/*
 * Create global definitions.
 */
#define DEFAULT_PORT			"/dev/ttya"
#define DEFAULT_POLL_INTERVAL		1	/* milliseconds */

/*
 * This is the working function which probes the serial port.
 */
boolean probeModem (void);

/*
 * Define some global variables.
 */
CDKLABEL *label = 0;
int LLastState = 0;
int LCurrentState = 0;
extern char *optarg;
char port[256];
int LFD;

/*
 *
 */
int main (int argc, char **argv)
{
   CDKSCREEN *cdkScreen = 0;
   int lines = 0;
   char *info[256], temp[256];
   struct termios termInfo;

   /* Set the deault values. */
   strcpy (port, DEFAULT_PORT);

   /* Parse up the command line. */
   while (1)
   {
      int ret;

      if ((ret = getopt (argc, argv, "p:h")) == -1)
      {
	 break;
      }

      switch (ret)
      {
      case 'p':
	 strcpy (port, optarg);
	 break;

      case 'h':
	 printf
		 ("Usage: %s [-p Port] [-i Poll Interval] [-c Poll Count] [-v] [-h]\n",
		 argv[0]);
	 ExitProgram (EXIT_SUCCESS);
	 break;
      }
   }

   cdkScreen = initCDKScreen (NULL);

   /* Start CDK color. */
   initCDKColor ();

   /*
    * Set the title of the main window.
    */
   sprintf (temp, "<C>Serial Port Monitor (%s)", port);
   info[lines++] = copyChar (temp);
   info[lines++] = copyChar ("<C><#HL(30)>");
   info[lines++] = copyChar ("");
   info[lines++] = copyChar ("Line Enabled       : -");
   info[lines++] = copyChar ("Data Terminal Ready: -");
   info[lines++] = copyChar ("Carrier Detect     : -");
   info[lines++] = copyChar ("Request To Send    : -");
   info[lines++] = copyChar ("Clear To Send      : -");
   info[lines++] = copyChar ("Secondary Transmit : -");
   info[lines++] = copyChar ("Secondary Receive  : -");
   info[lines++] = copyChar ("");

   /* Create the label widget. */
   label = newCDKLabel (cdkScreen, CENTER, CENTER, info, lines, TRUE, FALSE);
   drawCDKLabel (label, TRUE);

   /*
    * Open the serial port read only.
    */
   if ((LFD = open (port, O_RDONLY | O_NDELAY, 0)) == -1)
   {
      /* Create a pop-up dialog box... */
      printf ("Error: Open of <%s> failed.\n", port);
      ExitProgram (EXIT_FAILURE);
   }

   termInfo.c_cflag = CRTSCTS | CLOCAL;
   if (tcgetattr (LFD, &termInfo) != 0)
   {
      /* Really should create a pop-up dialog box... */
      printf ("Error: Could not get port attributes. Closing the port.\n");
      close (LFD);
      ExitProgram (EXIT_FAILURE);
   }

   for (;;)
   {
      /* Probe the modem. */
      probeModem ();

      /*
       * Sleep for the given amount of time. We do this first so no
       * weird refresh things happen.
       */
      napms (DEFAULT_POLL_INTERVAL);
   }
}

/*
 * This probes the modem and determines if we need to update
 * the display.
 */
boolean probeModem (void)
{
   int lines = 0;
   char *info[256], temp[256];

   /* Start building the label. */
   sprintf (temp, "<C>Serial Port Monitor (%s)", port);
   info[lines++] = copyChar (temp);
   info[lines++] = copyChar ("<C><#HL(30)>");
   info[lines++] = copyChar ("");

   /*
    * Get the serial port info.
    */
   ioctl (LFD, TIOCMGET, &LCurrentState);

   /*
    * If the states are different, change the display.
    */
   if (LLastState != LCurrentState)
   {
      /*
       * Check for line enabled.
       */
      if (LCurrentState & TIOCM_LE)
      {
	 info[lines++] = copyChar ("Line Enabled       : <#DI>");
      }
      else
      {
	 info[lines++] = copyChar ("Line Enabled       :  ");
      }

      /*
       * Check for data terminal ready.
       */
      if (LCurrentState & TIOCM_DTR)
      {
	 info[lines++] = copyChar ("Data Terminal Ready: <#DI>");
      }
      else
      {
	 info[lines++] = copyChar ("Data Terminal Ready:  ");
      }

      /*
       * Check for carrier detect.
       */
      if (LCurrentState & TIOCM_CAR)
      {
	 info[lines++] = copyChar ("Carrier Detect     : <#DI>");
      }
      else
      {
	 info[lines++] = copyChar ("Carrier Detect     :  ");
      }

      /*
       * Check for request to send.
       */
      if (LCurrentState & TIOCM_RTS)
      {
	 info[lines++] = copyChar ("Request To Send    : <#DI>");
      }
      else
      {
	 info[lines++] = copyChar ("Request To Send    :  ");
      }

      /*
       * Check for clear to send.
       */
      if (LCurrentState & TIOCM_CTS)
      {
	 info[lines++] = copyChar ("Clear To Send      : <#DI>");
      }
      else
      {
	 info[lines++] = copyChar ("Clear To Send      :  ");
      }

      /*
       * Check for secondary transmit.
       */
      if (LCurrentState & TIOCM_ST)
      {
	 info[lines++] = copyChar ("Secondary Transmit : <#DI>");
      }
      else
      {
	 info[lines++] = copyChar ("Secondary Transmit :  ");
      }

      /*
       * Check for secondary receive.
       */
      if (LCurrentState & TIOCM_SR)
      {
	 info[lines++] = copyChar ("Secondary Receive  : <#DI>");
      }
      else
      {
	 info[lines++] = copyChar ("Secondary Receive  :  ");
      }
   }
   info[lines++] = copyChar ("");

   /* Only do this if things have changed. */
   if (LLastState != LCurrentState)
   {
      eraseCDKLabel (label);
      setCDKLabel (label, info, lines, TRUE);
      drawCDKLabel (label, TRUE);
   }

   /*
    * Keep the current state.
    */
   LLastState = LCurrentState;

   /*
    * Return False to tell X that we want this function to be
    * run again.
    */
   return FALSE;
}