Check for SYS/GL during library init. Reason is that
[AROS.git] / test / serial / serialtest.c
blob9a0b8764cda581a52c09c365bd4e0e03a2846637
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <stdio.h>
7 #include <string.h>
9 #include <proto/alib.h>
10 #include <proto/exec.h>
11 #include <proto/dos.h>
13 #include <devices/serial.h>
14 #include <exec/execbase.h>
16 #include <stdlib.h>
18 void doall(void);
19 VOID do_auto(struct MsgPort * prt, ULONG unitnum, ULONG baudrate, ULONG delay);
21 struct IOExtSer * IORequests[10];
22 struct MsgPort * SerPort;
25 #define ARG_TEMPLATE "AUTO/S,BAUD/K,UNIT/K,PAUSE/K"
27 enum
29 ARG_AUTO = 0,
30 ARG_BAUD,
31 ARG_UNIT,
32 ARG_PAUSE,
33 NOOFARGS
36 int main(int argc, char **argv)
38 int i = 0;
39 IPTR args[NOOFARGS] = {(IPTR)FALSE,
40 (IPTR)NULL,
41 (IPTR)NULL,
42 (IPTR)NULL};
43 struct RDArgs * rda;
44 rda = ReadArgs(ARG_TEMPLATE, args, NULL);
46 if (NULL != rda) {
47 while (i < 10) {
48 IORequests[i++] = NULL;
50 SerPort = CreatePort("mySerial",0);
51 if (TRUE == args[ARG_AUTO]) {
52 ULONG unitnum = 0;
53 ULONG baudrate = 9600;
54 ULONG delay = 5;
55 if (NULL != (APTR) args[ARG_UNIT])
56 unitnum = atoi((CONST_STRPTR)args[ARG_UNIT]);
57 if (NULL != (APTR) args[ARG_BAUD])
58 baudrate = atoi((CONST_STRPTR)args[ARG_BAUD]);
59 if (NULL != (APTR) args[ARG_PAUSE])
60 delay = atoi((CONST_STRPTR)args[ARG_PAUSE]);
61 do_auto(SerPort,
62 unitnum,
63 baudrate,
64 delay);
65 } else {
66 doall();
68 DeletePort(SerPort);
69 FreeArgs(rda);
71 return 1;
74 VOID do_auto(struct MsgPort * prt, ULONG unitnum, ULONG baudrate, ULONG delay)
76 ULONG err;
77 IORequests[0] = (struct IOExtSer *)
78 CreateExtIO(SerPort, sizeof(struct IOExtSer));
80 printf("Opening unit %ld. Using baudrate %ld baud.\n",
81 (long)unitnum,
82 (long)baudrate);
84 err = OpenDevice("serial.device",unitnum,(struct IORequest *)IORequests[0],0);
86 if (0 != err) {
87 printf("Failed to open unit %ld of serial device.\n",(long)unitnum);
88 DeleteExtIO((struct IORequest *)IORequests[0]);
89 IORequests[0]=NULL;
90 } else {
91 printf("Opened device. Now waiting for %ld seconds.\n",(long)delay);
92 if (0 != delay) {
93 Delay(50*delay);
95 IORequests[0]->IOSer.io_Command = SDCMD_SETPARAMS;
97 IORequests[0]->io_Baud = baudrate;
99 DoIO((struct IORequest *)IORequests[0]);
100 if (0 != ((struct IORequest *)IORequests[0])->io_Error) {
101 printf("An error occured while setting the baudrate!\n");
102 } else {
103 ULONG len;
104 char buffer[] = "Hello, this is AROS's serial device.\n";
105 printf("Writing to serial device.\n");
106 IORequests[0]->IOSer.io_Command = CMD_WRITE;
107 IORequests[0]->IOSer.io_Flags = 0;
108 IORequests[0]->IOSer.io_Length = -1;
109 IORequests[0]->IOSer.io_Data = buffer;
111 DoIO((struct IORequest *)IORequests[0]);
112 printf("Now please enter something! Waiting for %ld seconds!\n",(long)delay);
113 if (0 != delay) {
114 Delay(50 * delay);
117 IORequests[0]->IOSer.io_Command = SDCMD_QUERY;
119 DoIO((struct IORequest *)IORequests[0]);
120 printf("Status bits: 0x%x\n",IORequests[0]->io_Status);
121 printf("Number of bytes in buffer: %d\n",(int)IORequests[0]->IOSer.io_Actual);
123 if (0 != (len = (int)IORequests[0]->IOSer.io_Actual)) {
124 len = (len < sizeof(buffer) - 1)
125 ? len
126 : sizeof(buffer)-1;
127 IORequests[0]->IOSer.io_Command = CMD_READ;
128 IORequests[0]->IOSer.io_Flags = IOF_QUICK;
129 IORequests[0]->IOSer.io_Length = len;
130 IORequests[0]->IOSer.io_Data = buffer;
132 DoIO((struct IORequest *)IORequests[0]);
133 buffer[len] = 0;
134 printf("Received the following string: %s\n",buffer);
136 printf("Ending now.\n");
138 CloseDevice((struct IORequest *)IORequests[0]);
142 int getFreeIORequest()
144 int i = 0;
145 while (i <= 9)
147 if (NULL == IORequests[i])
148 return i;
149 i++;
151 return -1;
154 void closedevices(void)
156 int i = 0;
157 while (i <= 9)
159 if (NULL != IORequests[i])
160 CloseDevice((struct IORequest *)IORequests[i]);
161 i++;
165 void open_device(void)
167 unsigned long unitnum;
168 char sevenwire, shared;
169 int index = getFreeIORequest();
170 ULONG flags = 0;
171 BYTE err;
173 if (-1 == index)
175 printf("No more device to open.\n");
176 return;
179 IORequests[index] = (struct IOExtSer *)
180 CreateExtIO(SerPort, sizeof(struct IOExtSer));
182 printf("Open serial device.\n");
183 printf("Unitnumber: ");
184 scanf("%lu", &unitnum);
185 printf("shared access (y/n):");
186 scanf("%c", &shared);
187 printf("seven wire (y/n):");
188 scanf("%c", &sevenwire);
190 if (shared == 'y' ||
191 shared == 'Y')
192 IORequests[index] -> io_SerFlags |= SERF_SHARED;
194 if (sevenwire == 'y' ||
195 sevenwire == 'Y')
196 IORequests[index] -> io_SerFlags |= SERF_7WIRE;
198 err = OpenDevice("serial.device",unitnum,(struct IORequest *)IORequests[index],flags);
200 if (0 != err)
202 printf("Failed to open unit %ld of serial device.\n",unitnum);
203 DeleteExtIO((struct IORequest *)IORequests[index]);
204 IORequests[index]=NULL;
206 else
208 printf("Created unit %ld of serial device. Refer to it with number %d\n",unitnum,index);
214 void close_device(void)
216 int index;
217 printf("Close a serial device.\n");
218 printf("Referencenumber: ");
219 scanf("%d", &index);
221 if (index >= 0 && index <= 9 && NULL != IORequests[index])
223 printf("Closing device!\n");
224 CloseDevice((struct IORequest *)IORequests[index]);
225 DeleteExtIO((struct IORequest *)IORequests[index]);
226 IORequests[index]=NULL;
228 else
230 printf("No such refence.\n");
234 void write_to_device(void)
236 int index;
237 printf("Write to serial device.\n");
238 printf("Referncenumber: ");
239 scanf("%d", &index);
241 if (index >= 0 && index <= 9 && NULL != IORequests[index])
243 char buffer[100];
244 memset(buffer,0,100);
245 IORequests[index]->IOSer.io_Command = CMD_WRITE;
246 IORequests[index]->IOSer.io_Flags = 0;
247 IORequests[index]->IOSer.io_Length = -1;
248 IORequests[index]->IOSer.io_Data = buffer;
250 printf("Enter string to transmit!\n");
251 scanf("%s",buffer);
252 DoIO((struct IORequest *)IORequests[index]);
254 else
256 printf("No such refence.\n");
260 void read_from_device(void)
262 int index;
263 printf("Read from serial device.\n");
264 printf("Referncenumber: ");
265 scanf("%d", &index);
267 if (index >= 0 && index <= 9 && NULL != IORequests[index])
269 int len;
270 char buffer[100];
271 memset(buffer,0,100);
272 printf("Read how many bytes [1-99]: ");
273 scanf("%d",&len);
274 if (len <= 0) len = 1;
275 if (len > 99) len = 99;
276 printf("Reading %d bytes from device!\n",len);
277 IORequests[index]->IOSer.io_Command = CMD_READ;
278 IORequests[index]->IOSer.io_Flags = IOF_QUICK;
279 IORequests[index]->IOSer.io_Length = len;
280 IORequests[index]->IOSer.io_Data = buffer;
282 DoIO((struct IORequest *)IORequests[index]);
283 printf("Received the following string: %s\n",buffer);
285 else
287 printf("No such refence.\n");
292 void query(void)
294 int index;
295 printf("Query a serial device.\n");
296 printf("Referncenumber: ");
297 scanf("%d", &index);
299 if (index >= 0 && index <= 9 && NULL != IORequests[index])
301 IORequests[index]->IOSer.io_Command = SDCMD_QUERY;
303 DoIO((struct IORequest *)IORequests[index]);
304 printf("Status bits: 0x%x\n",IORequests[index]->io_Status);
305 printf("Number of bytes in buffer: %d\n",(int)IORequests[index]->IOSer.io_Actual);
307 else
309 printf("No such refence.\n");
313 void stop(void)
315 int index;
316 printf("Stop/pause IO on a serial device.\n");
317 printf("Referncenumber: ");
318 scanf("%d", &index);
320 if (index >= 0 && index <= 9 && NULL != IORequests[index])
322 IORequests[index]->IOSer.io_Command = CMD_STOP;
324 DoIO((struct IORequest *)IORequests[index]);
325 printf("IO has been stopped!\n");
327 else
329 printf("No such refence.\n");
333 void start(void)
335 int index;
336 printf("Start/resume IO on a serial device.\n");
337 printf("Referncenumber: ");
338 scanf("%d", &index);
340 if (index >= 0 && index <= 9 && NULL != IORequests[index])
342 IORequests[index]->IOSer.io_Command = CMD_START;
344 DoIO((struct IORequest *)IORequests[index]);
345 printf("IO has started!\n");
347 else
349 printf("No such refence.\n");
354 void set_parameters(void)
356 int index;
357 printf("Set parameters on a serial device.\n");
358 printf("Referncenumber: ");
359 scanf("%d", &index);
361 if (index >= 0 && index <= 9 && NULL != IORequests[index])
363 int baudrate;
364 IORequests[index]->IOSer.io_Command = SDCMD_SETPARAMS;
366 printf("New baudrate: ");
367 scanf("%d",&baudrate);
368 IORequests[index]->io_Baud = baudrate;
370 DoIO((struct IORequest *)IORequests[index]);
371 if (((struct IORequest *)IORequests[index])->io_Error != 0)
373 printf("An error occured while setting the parameters!\n");
376 else
378 printf("No such refence.\n");
383 void doall(void)
385 char buf[80];
387 for (;;)
389 printf("> ");
390 fflush(stdout);
391 scanf("%s", buf);
393 if (!strcmp(buf,"quit"))
395 closedevices();
396 return;
398 else if (!strcmp(buf, "help"))
400 printf("quit help open_device [od] close_device [cd] write_to_device [wd]\n");
401 printf("read_from_device [rd] query [q] set_parameters [sp] stop [pa]\nstart [st]\n");
403 else if (!strcmp(buf, "open_device") || !strcmp(buf, "od"))
405 open_device();
407 else if (!strcmp(buf, "close_device") || !strcmp(buf, "cd"))
409 close_device();
411 else if (!strcmp(buf, "write_to_device") || !strcmp(buf, "wd"))
413 write_to_device();
415 else if (!strcmp(buf, "read_from_device") || !strcmp(buf, "rd"))
417 read_from_device();
419 else if (!strcmp(buf, "query") || !strcmp(buf, "q"))
421 query();
423 else if (!strcmp(buf, "set_parameters") || !strcmp(buf, "sp"))
425 set_parameters();
427 else if (!strcmp(buf, "stop") || !strcmp(buf, "pa"))
429 stop();
431 else if (!strcmp(buf, "start") || !strcmp(buf, "st"))
433 start();