-
Notifications
You must be signed in to change notification settings - Fork 0
/
amiga_serial_read_async.c
executable file
·148 lines (116 loc) · 3.76 KB
/
amiga_serial_read_async.c
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
/* -*-*-*-*-*-*-*-*-*-*-*-*-*-*
* Amiga_Serial_Read_Async.c
* by Marek Hac on 20/04/2023
* https://github.com/marekhac
* -*-*-*-*-*-*-*-*-*-*-*-*-*-*
* Example of async reading of data from serial port on Amiga.
* Read data in the loop, until CTRL-D is pressed
*
* based on complex tricky example of serial.device usage
*
* Compile with GCC
* Run from CLI only
*/
#include <exec/types.h>
#include <exec/memory.h>
#include <exec/io.h>
#include <exec/devices.h>
#include <exec/libraries.h>
#include <dos/dos.h>
#include <devices/serial.h>
#include <stdio.h>
// serial port params
#define READ_BUFFER_SIZE 32
struct IOExtSer *SerialIO; /* pointer to I/O request */
struct MsgPort *SerialMP; /* pointer to Message Port */
struct IOTArray Terminators =
{
0x00 /* EOF character */
};
UBYTE serialReadBuffer[READ_BUFFER_SIZE]; /* reserve 32 bytes storage */
void sendWriteCommand()
{
SerialIO->IOSer.io_Command = CMD_WRITE;
SerialIO->IOSer.io_Length = -1;
SerialIO->IOSer.io_Data = (APTR)"WakeUp";
if (DoIO(SerialIO))
{
printf("Error while sending CMD_WRITE command\n");
}
}
void setupReadCommand()
{
SerialIO->IOSer.io_Command = CMD_READ;
SerialIO->IOSer.io_Length = READ_BUFFER_SIZE;
SerialIO->IOSer.io_Data = &serialReadBuffer;
}
void setupCustomSerialParams()
{
// setup transmition parameters
SerialIO->io_RBufLen = 512;
SerialIO->io_Baud = 9600;
SerialIO->io_ReadLen = 8;
SerialIO->io_WriteLen = 8;
SerialIO->io_StopBits = 1;
// stop receving data when termination character appear in stream
SerialIO->io_SerFlags = SERF_EOFMODE;
SerialIO->io_TermArray = Terminators;
// update serial parameters using SDCMD_SETPARAMS command
SerialIO->IOSer.io_Command = SDCMD_SETPARAMS;
if (DoIO(SerialIO))
{
printf("Error while setting serial parameters!\n");
}
}
int main(void)
{
ULONG signal;
/* create message port */
if (SerialMP = (struct MsgPort *) CreatePort(0,0) )
{
/* create I/O request */
if (SerialIO=(struct IOExtSer *)
CreateExtIO(SerialMP,sizeof(struct IOExtSer)) )
{
if (OpenDevice(SERIALNAME,0L,SerialIO,0) )
printf("%s did not open\n",SERIALNAME);
else
{
/* device is open */
setupCustomSerialParams();
sendWriteCommand();
setupReadCommand();
/* Initiate I/O command and not wait for it to complete */
SendIO(SerialIO);
printf("Sleeping until CTRL-D or serial input\n");
do
{
signal = Wait(1L << SerialMP -> mp_SigBit | SIGBREAKF_CTRL_D);
if (CheckIO(SerialIO) ) /* If request is complete... */
{
/* wait for specific I/O request */
WaitIO(SerialIO);
printf("%ld bytes received. ",SerialIO->IOSer.io_Actual);
printf("Data: %s\n", serialReadBuffer);
SendIO(SerialIO); /* restart I/O request */
}
}
while (!(signal & SIGBREAKF_CTRL_D));
AbortIO(SerialIO); /* ask device to abort request, if pending */
WaitIO(SerialIO); /* wait for abort, then clean up */
CloseDevice(SerialIO); /* close serial device */
DeletePort(SerialMP);
}
DeleteExtIO(SerialIO); /* delete I/O request */
}
else
{ printf("Unable to create IORequest\n");
DeletePort(SerialMP); /* delete message port */
}
}
else
{
printf("Unable to create message port\n");
}
return 0;
}