dos.library: Correct the usage of the AROS_CALL4() macro
[AROS.git] / test / serialtest.c
blob1f381a8ad1c266344c573e4704665e9e3362300f
1 #include <stdio.h>
2 #include <string.h>
4 #include <proto/alib.h>
5 #include <proto/exec.h>
6 #include <proto/dos.h>
8 #include <devices/serial.h>
9 #include <exec/execbase.h>
11 #include <stdlib.h>
13 void doall(void);
14 VOID do_auto(struct MsgPort * prt, ULONG unitnum, ULONG baudrate, ULONG delay);
16 struct IOExtSer * IORequests[10];
17 struct MsgPort * SerPort;
20 #define ARG_TEMPLATE "AUTO/S,BAUD/K,UNIT/K,PAUSE/K"
22 enum
24 ARG_AUTO = 0,
25 ARG_BAUD,
26 ARG_UNIT,
27 ARG_PAUSE,
28 NOOFARGS
31 int main(int argc, char **argv)
33 int i = 0;
34 IPTR args[NOOFARGS] = {(IPTR)FALSE,
35 (IPTR)NULL,
36 (IPTR)NULL,
37 (IPTR)NULL};
38 struct RDArgs * rda;
39 rda = ReadArgs(ARG_TEMPLATE, args, NULL);
41 if (NULL != rda) {
42 while (i < 10) {
43 IORequests[i++] = NULL;
45 SerPort = CreatePort("mySerial",0);
46 if (TRUE == args[ARG_AUTO]) {
47 ULONG unitnum = 0;
48 ULONG baudrate = 9600;
49 ULONG delay = 5;
50 if (NULL != (APTR) args[ARG_UNIT])
51 unitnum = atoi((CONST_STRPTR)args[ARG_UNIT]);
52 if (NULL != (APTR) args[ARG_BAUD])
53 baudrate = atoi((CONST_STRPTR)args[ARG_BAUD]);
54 if (NULL != (APTR) args[ARG_PAUSE])
55 delay = atoi((CONST_STRPTR)args[ARG_PAUSE]);
56 do_auto(SerPort,
57 unitnum,
58 baudrate,
59 delay);
60 } else {
61 doall();
63 DeletePort(SerPort);
64 FreeArgs(rda);
66 return 1;
69 VOID do_auto(struct MsgPort * prt, ULONG unitnum, ULONG baudrate, ULONG delay)
71 ULONG err;
72 IORequests[0] = (struct IOExtSer *)
73 CreateExtIO(SerPort, sizeof(struct IOExtSer));
75 printf("Opening unit %ld. Using baudrate %ld baud.\n",
76 (long)unitnum,
77 (long)baudrate);
79 err = OpenDevice("serial.device",unitnum,(struct IORequest *)IORequests[0],0);
81 if (0 != err) {
82 printf("Failed to open unit %ld of serial device.\n",(long)unitnum);
83 DeleteExtIO((struct IORequest *)IORequests[0]);
84 IORequests[0]=NULL;
85 } else {
86 printf("Opened device. Now waiting for %ld seconds.\n",(long)delay);
87 if (0 != delay) {
88 Delay(50*delay);
90 IORequests[0]->IOSer.io_Command = SDCMD_SETPARAMS;
92 IORequests[0]->io_Baud = baudrate;
94 DoIO((struct IORequest *)IORequests[0]);
95 if (0 != ((struct IORequest *)IORequests[0])->io_Error) {
96 printf("An error occured while setting the baudrate!\n");
97 } else {
98 ULONG len;
99 char buffer[] = "Hello, this is AROS's serial device.\n";
100 printf("Writing to serial device.\n");
101 IORequests[0]->IOSer.io_Command = CMD_WRITE;
102 IORequests[0]->IOSer.io_Flags = 0;
103 IORequests[0]->IOSer.io_Length = -1;
104 IORequests[0]->IOSer.io_Data = buffer;
106 DoIO((struct IORequest *)IORequests[0]);
107 printf("Now please enter something! Waiting for %ld seconds!\n",(long)delay);
108 if (0 != delay) {
109 Delay(50 * delay);
112 IORequests[0]->IOSer.io_Command = SDCMD_QUERY;
114 DoIO((struct IORequest *)IORequests[0]);
115 printf("Status bits: 0x%x\n",IORequests[0]->io_Status);
116 printf("Number of bytes in buffer: %d\n",(int)IORequests[0]->IOSer.io_Actual);
118 if (0 != (len = (int)IORequests[0]->IOSer.io_Actual)) {
119 len = (len < sizeof(buffer) - 1)
120 ? len
121 : sizeof(buffer)-1;
122 IORequests[0]->IOSer.io_Command = CMD_READ;
123 IORequests[0]->IOSer.io_Flags = IOF_QUICK;
124 IORequests[0]->IOSer.io_Length = len;
125 IORequests[0]->IOSer.io_Data = buffer;
127 DoIO((struct IORequest *)IORequests[0]);
128 buffer[len] = 0;
129 printf("Received the following string: %s\n",buffer);
131 printf("Ending now.\n");
133 CloseDevice((struct IORequest *)IORequests[0]);
137 int getFreeIORequest()
139 int i = 0;
140 while (i <= 9)
142 if (NULL == IORequests[i])
143 return i;
144 i++;
146 return -1;
149 void closedevices(void)
151 int i = 0;
152 while (i <= 9)
154 if (NULL != IORequests[i])
155 CloseDevice((struct IORequest *)IORequests[i]);
156 i++;
160 void open_device(void)
162 unsigned long unitnum;
163 char sevenwire, shared;
164 int index = getFreeIORequest();
165 ULONG flags = 0;
166 BYTE err;
168 if (-1 == index)
170 printf("No more device to open.\n");
171 return;
174 IORequests[index] = (struct IOExtSer *)
175 CreateExtIO(SerPort, sizeof(struct IOExtSer));
177 printf("Open serial device.\n");
178 printf("Unitnumber: ");
179 scanf("%lu", &unitnum);
180 printf("shared access (y/n):");
181 scanf("%c", &shared);
182 printf("seven wire (y/n):");
183 scanf("%c", &sevenwire);
185 if (shared == 'y' ||
186 shared == 'Y')
187 IORequests[index] -> io_SerFlags |= SERF_SHARED;
189 if (sevenwire == 'y' ||
190 sevenwire == 'Y')
191 IORequests[index] -> io_SerFlags |= SERF_7WIRE;
193 err = OpenDevice("serial.device",unitnum,(struct IORequest *)IORequests[index],flags);
195 if (0 != err)
197 printf("Failed to open unit %ld of serial device.\n",unitnum);
198 DeleteExtIO((struct IORequest *)IORequests[index]);
199 IORequests[index]=NULL;
201 else
203 printf("Created unit %ld of serial device. Refer to it with number %d\n",unitnum,index);
209 void close_device(void)
211 int index;
212 printf("Close a serial device.\n");
213 printf("Referencenumber: ");
214 scanf("%d", &index);
216 if (index >= 0 && index <= 9 && NULL != IORequests[index])
218 printf("Closing device!\n");
219 CloseDevice((struct IORequest *)IORequests[index]);
220 DeleteExtIO((struct IORequest *)IORequests[index]);
221 IORequests[index]=NULL;
223 else
225 printf("No such refence.\n");
229 void write_to_device(void)
231 int index;
232 printf("Write to serial device.\n");
233 printf("Referncenumber: ");
234 scanf("%d", &index);
236 if (index >= 0 && index <= 9 && NULL != IORequests[index])
238 char buffer[100];
239 memset(buffer,0,100);
240 IORequests[index]->IOSer.io_Command = CMD_WRITE;
241 IORequests[index]->IOSer.io_Flags = 0;
242 IORequests[index]->IOSer.io_Length = -1;
243 IORequests[index]->IOSer.io_Data = buffer;
245 printf("Enter string to transmit!\n");
246 scanf("%s",buffer);
247 DoIO((struct IORequest *)IORequests[index]);
249 else
251 printf("No such refence.\n");
255 void read_from_device(void)
257 int index;
258 printf("Read from serial device.\n");
259 printf("Referncenumber: ");
260 scanf("%d", &index);
262 if (index >= 0 && index <= 9 && NULL != IORequests[index])
264 int len;
265 char buffer[100];
266 memset(buffer,0,100);
267 printf("Read how many bytes [1-99]: ");
268 scanf("%d",&len);
269 if (len <= 0) len = 1;
270 if (len > 99) len = 99;
271 printf("Reading %d bytes from device!\n",len);
272 IORequests[index]->IOSer.io_Command = CMD_READ;
273 IORequests[index]->IOSer.io_Flags = IOF_QUICK;
274 IORequests[index]->IOSer.io_Length = len;
275 IORequests[index]->IOSer.io_Data = buffer;
277 DoIO((struct IORequest *)IORequests[index]);
278 printf("Received the following string: %s\n",buffer);
280 else
282 printf("No such refence.\n");
287 void query(void)
289 int index;
290 printf("Query a serial device.\n");
291 printf("Referncenumber: ");
292 scanf("%d", &index);
294 if (index >= 0 && index <= 9 && NULL != IORequests[index])
296 IORequests[index]->IOSer.io_Command = SDCMD_QUERY;
298 DoIO((struct IORequest *)IORequests[index]);
299 printf("Status bits: 0x%x\n",IORequests[index]->io_Status);
300 printf("Number of bytes in buffer: %d\n",(int)IORequests[index]->IOSer.io_Actual);
302 else
304 printf("No such refence.\n");
308 void stop(void)
310 int index;
311 printf("Stop/pause IO on a serial device.\n");
312 printf("Referncenumber: ");
313 scanf("%d", &index);
315 if (index >= 0 && index <= 9 && NULL != IORequests[index])
317 IORequests[index]->IOSer.io_Command = CMD_STOP;
319 DoIO((struct IORequest *)IORequests[index]);
320 printf("IO has been stopped!\n");
322 else
324 printf("No such refence.\n");
328 void start(void)
330 int index;
331 printf("Start/resume IO on a serial device.\n");
332 printf("Referncenumber: ");
333 scanf("%d", &index);
335 if (index >= 0 && index <= 9 && NULL != IORequests[index])
337 IORequests[index]->IOSer.io_Command = CMD_START;
339 DoIO((struct IORequest *)IORequests[index]);
340 printf("IO has started!\n");
342 else
344 printf("No such refence.\n");
349 void set_parameters(void)
351 int index;
352 printf("Set parameters on a serial device.\n");
353 printf("Referncenumber: ");
354 scanf("%d", &index);
356 if (index >= 0 && index <= 9 && NULL != IORequests[index])
358 int baudrate;
359 IORequests[index]->IOSer.io_Command = SDCMD_SETPARAMS;
361 printf("New baudrate: ");
362 scanf("%d",&baudrate);
363 IORequests[index]->io_Baud = baudrate;
365 DoIO((struct IORequest *)IORequests[index]);
366 if (((struct IORequest *)IORequests[index])->io_Error != 0)
368 printf("An error occured while setting the parameters!\n");
371 else
373 printf("No such refence.\n");
378 void doall(void)
380 char buf[80];
382 for (;;)
384 printf("> ");
385 fflush(stdout);
386 scanf("%s", buf);
388 if (!strcmp(buf,"quit"))
390 closedevices();
391 return;
393 else if (!strcmp(buf, "help"))
395 printf("quit help open_device [od] close_device [cd] write_to_device [wd]\n");
396 printf("read_from_device [rd] query [q] set_parameters [sp] stop [pa]\nstart [st]\n");
398 else if (!strcmp(buf, "open_device") || !strcmp(buf, "od"))
400 open_device();
402 else if (!strcmp(buf, "close_device") || !strcmp(buf, "cd"))
404 close_device();
406 else if (!strcmp(buf, "write_to_device") || !strcmp(buf, "wd"))
408 write_to_device();
410 else if (!strcmp(buf, "read_from_device") || !strcmp(buf, "rd"))
412 read_from_device();
414 else if (!strcmp(buf, "query") || !strcmp(buf, "q"))
416 query();
418 else if (!strcmp(buf, "set_parameters") || !strcmp(buf, "sp"))
420 set_parameters();
422 else if (!strcmp(buf, "stop") || !strcmp(buf, "pa"))
424 stop();
426 else if (!strcmp(buf, "start") || !strcmp(buf, "st"))
428 start();