4 #include <proto/alib.h>
5 #include <proto/exec.h>
8 #include <devices/serial.h>
9 #include <exec/execbase.h>
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"
31 int main(int argc
, char **argv
)
34 IPTR args
[NOOFARGS
] = {(IPTR
)FALSE
,
39 rda
= ReadArgs(ARG_TEMPLATE
, args
, NULL
);
43 IORequests
[i
++] = NULL
;
45 SerPort
= CreatePort("mySerial",0);
46 if (TRUE
== args
[ARG_AUTO
]) {
48 ULONG baudrate
= 9600;
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
]);
69 VOID
do_auto(struct MsgPort
* prt
, ULONG unitnum
, ULONG baudrate
, ULONG delay
)
72 IORequests
[0] = (struct IOExtSer
*)
73 CreateExtIO(SerPort
, sizeof(struct IOExtSer
));
75 printf("Opening unit %ld. Using baudrate %ld baud.\n",
79 err
= OpenDevice("serial.device",unitnum
,(struct IORequest
*)IORequests
[0],0);
82 printf("Failed to open unit %ld of serial device.\n",(long)unitnum
);
83 DeleteExtIO((struct IORequest
*)IORequests
[0]);
86 printf("Opened device. Now waiting for %ld seconds.\n",(long)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");
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
);
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)
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]);
129 printf("Received the following string: %s\n",buffer
);
131 printf("Ending now.\n");
133 CloseDevice((struct IORequest
*)IORequests
[0]);
137 int getFreeIORequest()
142 if (NULL
== IORequests
[i
])
149 void closedevices(void)
154 if (NULL
!= IORequests
[i
])
155 CloseDevice((struct IORequest
*)IORequests
[i
]);
160 void open_device(void)
162 unsigned long unitnum
;
163 char sevenwire
, shared
;
164 int index
= getFreeIORequest();
170 printf("No more device to open.\n");
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
);
187 IORequests
[index
] -> io_SerFlags
|= SERF_SHARED
;
189 if (sevenwire
== 'y' ||
191 IORequests
[index
] -> io_SerFlags
|= SERF_7WIRE
;
193 err
= OpenDevice("serial.device",unitnum
,(struct IORequest
*)IORequests
[index
],flags
);
197 printf("Failed to open unit %ld of serial device.\n",unitnum
);
198 DeleteExtIO((struct IORequest
*)IORequests
[index
]);
199 IORequests
[index
]=NULL
;
203 printf("Created unit %ld of serial device. Refer to it with number %d\n",unitnum
,index
);
209 void close_device(void)
212 printf("Close a serial device.\n");
213 printf("Referencenumber: ");
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
;
225 printf("No such refence.\n");
229 void write_to_device(void)
232 printf("Write to serial device.\n");
233 printf("Referncenumber: ");
236 if (index
>= 0 && index
<= 9 && NULL
!= IORequests
[index
])
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");
247 DoIO((struct IORequest
*)IORequests
[index
]);
251 printf("No such refence.\n");
255 void read_from_device(void)
258 printf("Read from serial device.\n");
259 printf("Referncenumber: ");
262 if (index
>= 0 && index
<= 9 && NULL
!= IORequests
[index
])
266 memset(buffer
,0,100);
267 printf("Read how many bytes [1-99]: ");
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
);
282 printf("No such refence.\n");
290 printf("Query a serial device.\n");
291 printf("Referncenumber: ");
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
);
304 printf("No such refence.\n");
311 printf("Stop/pause IO on a serial device.\n");
312 printf("Referncenumber: ");
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");
324 printf("No such refence.\n");
331 printf("Start/resume IO on a serial device.\n");
332 printf("Referncenumber: ");
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");
344 printf("No such refence.\n");
349 void set_parameters(void)
352 printf("Set parameters on a serial device.\n");
353 printf("Referncenumber: ");
356 if (index
>= 0 && index
<= 9 && NULL
!= IORequests
[index
])
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");
373 printf("No such refence.\n");
388 if (!strcmp(buf
,"quit"))
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"))
402 else if (!strcmp(buf
, "close_device") || !strcmp(buf
, "cd"))
406 else if (!strcmp(buf
, "write_to_device") || !strcmp(buf
, "wd"))
410 else if (!strcmp(buf
, "read_from_device") || !strcmp(buf
, "rd"))
414 else if (!strcmp(buf
, "query") || !strcmp(buf
, "q"))
418 else if (!strcmp(buf
, "set_parameters") || !strcmp(buf
, "sp"))
422 else if (!strcmp(buf
, "stop") || !strcmp(buf
, "pa"))
426 else if (!strcmp(buf
, "start") || !strcmp(buf
, "st"))