2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
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>
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"
36 int main(int argc
, char **argv
)
39 IPTR args
[NOOFARGS
] = {(IPTR
)FALSE
,
44 rda
= ReadArgs(ARG_TEMPLATE
, args
, NULL
);
48 IORequests
[i
++] = NULL
;
50 SerPort
= CreatePort("mySerial",0);
51 if (TRUE
== args
[ARG_AUTO
]) {
53 ULONG baudrate
= 9600;
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
]);
74 VOID
do_auto(struct MsgPort
* prt
, ULONG unitnum
, ULONG baudrate
, ULONG delay
)
77 IORequests
[0] = (struct IOExtSer
*)
78 CreateExtIO(SerPort
, sizeof(struct IOExtSer
));
80 printf("Opening unit %ld. Using baudrate %ld baud.\n",
84 err
= OpenDevice("serial.device",unitnum
,(struct IORequest
*)IORequests
[0],0);
87 printf("Failed to open unit %ld of serial device.\n",(long)unitnum
);
88 DeleteExtIO((struct IORequest
*)IORequests
[0]);
91 printf("Opened device. Now waiting for %ld seconds.\n",(long)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");
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
);
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)
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]);
134 printf("Received the following string: %s\n",buffer
);
136 printf("Ending now.\n");
138 CloseDevice((struct IORequest
*)IORequests
[0]);
142 int getFreeIORequest()
147 if (NULL
== IORequests
[i
])
154 void closedevices(void)
159 if (NULL
!= IORequests
[i
])
160 CloseDevice((struct IORequest
*)IORequests
[i
]);
165 void open_device(void)
167 unsigned long unitnum
;
168 char sevenwire
, shared
;
169 int index
= getFreeIORequest();
175 printf("No more device to open.\n");
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
);
192 IORequests
[index
] -> io_SerFlags
|= SERF_SHARED
;
194 if (sevenwire
== 'y' ||
196 IORequests
[index
] -> io_SerFlags
|= SERF_7WIRE
;
198 err
= OpenDevice("serial.device",unitnum
,(struct IORequest
*)IORequests
[index
],flags
);
202 printf("Failed to open unit %ld of serial device.\n",unitnum
);
203 DeleteExtIO((struct IORequest
*)IORequests
[index
]);
204 IORequests
[index
]=NULL
;
208 printf("Created unit %ld of serial device. Refer to it with number %d\n",unitnum
,index
);
214 void close_device(void)
217 printf("Close a serial device.\n");
218 printf("Referencenumber: ");
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
;
230 printf("No such refence.\n");
234 void write_to_device(void)
237 printf("Write to serial device.\n");
238 printf("Referncenumber: ");
241 if (index
>= 0 && index
<= 9 && NULL
!= IORequests
[index
])
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");
252 DoIO((struct IORequest
*)IORequests
[index
]);
256 printf("No such refence.\n");
260 void read_from_device(void)
263 printf("Read from serial device.\n");
264 printf("Referncenumber: ");
267 if (index
>= 0 && index
<= 9 && NULL
!= IORequests
[index
])
271 memset(buffer
,0,100);
272 printf("Read how many bytes [1-99]: ");
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
);
287 printf("No such refence.\n");
295 printf("Query a serial device.\n");
296 printf("Referncenumber: ");
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
);
309 printf("No such refence.\n");
316 printf("Stop/pause IO on a serial device.\n");
317 printf("Referncenumber: ");
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");
329 printf("No such refence.\n");
336 printf("Start/resume IO on a serial device.\n");
337 printf("Referncenumber: ");
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");
349 printf("No such refence.\n");
354 void set_parameters(void)
357 printf("Set parameters on a serial device.\n");
358 printf("Referncenumber: ");
361 if (index
>= 0 && index
<= 9 && NULL
!= IORequests
[index
])
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");
378 printf("No such refence.\n");
393 if (!strcmp(buf
,"quit"))
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"))
407 else if (!strcmp(buf
, "close_device") || !strcmp(buf
, "cd"))
411 else if (!strcmp(buf
, "write_to_device") || !strcmp(buf
, "wd"))
415 else if (!strcmp(buf
, "read_from_device") || !strcmp(buf
, "rd"))
419 else if (!strcmp(buf
, "query") || !strcmp(buf
, "q"))
423 else if (!strcmp(buf
, "set_parameters") || !strcmp(buf
, "sp"))
427 else if (!strcmp(buf
, "stop") || !strcmp(buf
, "pa"))
431 else if (!strcmp(buf
, "start") || !strcmp(buf
, "st"))