-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerialIO.tm
325 lines (287 loc) · 6.59 KB
/
SerialIO.tm
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* SerialIO.tm
*
* Copyright 2004-2009 Rob Raguet-Schofield. All rights reserved.
*
*/
/*
* Implementation
*/
#include "SerialPort.h"
#include "SerialPort.h"
#include <stdio.h>
#if UNIX_MATHLINK
#include <errno.h>
#include <string.h>
#define SNPRINTF snprintf
#elif WINDOWS_MATHLINK
#define SNPRINTF _snprintf
#endif
static void message(const char* theSymbol, const char* theTag, const char* theParam1 = NULL, const char* theParam2 = NULL)
{
MLPutFunction(stdlink, "EvaluatePacket", 1);
int theParamCount = 0;
if(theParam1 != NULL)
{
theParamCount++;
if(theParam2 != NULL)
{
theParamCount++;
}
}
MLPutFunction(stdlink, "Message", 1 + theParamCount);
MLPutFunction(stdlink, "MessageName", 2);
MLPutSymbol(stdlink, theSymbol);
MLPutString(stdlink, theTag);
if(theParam1 != NULL)
{
MLPutString(stdlink, theParam1);
if(theParam2 != NULL)
{
MLPutString(stdlink, theParam2);
}
}
MLNextPacket(stdlink);
}
class Node
{
public:
Node(SerialPort* thePort)
: port(thePort), next(NULL), prev(NULL)
{
}
SerialPort* port;
Node* next;
Node* prev;
};
static Node* FirstNode = NULL;
static Node* LastNode = NULL;
static Node* getPortNode(int thePort, bool messageOnFailure)
{
Node* theNode = FirstNode;
while(theNode != NULL)
{
if(thePort == theNode->port->getIdentifier())
break;
theNode = theNode->next;
}
if(theNode == NULL && messageOnFailure)
{
char buffer[256];
SNPRINTF(buffer, 256, "%d", thePort);
message("SerialPort", "inval", buffer);
}
return theNode;
}
static SerialPort* getPortObject(int thePort, bool messageOnFailure)
{
Node* theNode = getPortNode(thePort, messageOnFailure);
return (theNode != NULL ? theNode->port : NULL);
}
static int getPortNumber(SerialPort* thePort)
{
return thePort->getIdentifier();
}
void serialOpen(kcharp_ct thePathName)
{
SerialPort* thePort = new SerialPort(thePathName);
if(!thePort->openPort())
{
delete thePort;
message("SerialOpen", "failed", strerror(errno));
MLPutSymbol(stdlink, "$Failed");
return;
}
Node* theNode = new Node(thePort);
if(LastNode != NULL)
{
LastNode->next = theNode;
theNode->prev = LastNode;
LastNode = theNode;
}
else
FirstNode = LastNode = theNode;
int thePortNumber = getPortNumber(thePort);
MLPutInteger(stdlink, thePortNumber);
}
char* serialClose(int thePort)
{
Node* theNode = getPortNode(thePort, true);
if(theNode != NULL)
{
if(theNode->prev != NULL)
theNode->prev->next = theNode->next;
else
FirstNode = theNode->next;
if(theNode->next != NULL)
theNode->next->prev = theNode->prev;
else
LastNode = theNode->prev;
SerialPort* port = theNode->port;
delete theNode;
delete port;
return "Null";
}
else
return "$Failed";
}
void serialSetOptions()
{
long argc;
if(MLCheckFunction(stdlink, "List", &argc))
{
int thePort;
if((argc == 2) && MLGetInteger(stdlink, &thePort))
{
SerialPort* port = getPortObject(thePort, true);
if(port != NULL)
{
if(MLCheckFunction(stdlink, "List", &argc))
{
for(long i = 0; i < argc; i++)
{
long argc2;
if(MLCheckFunction(stdlink, "Rule", &argc2) && (argc2 == 2))
{
const char* lhs;
if(MLGetString(stdlink, &lhs))
{
int rhs;
if(MLGetInteger(stdlink, &rhs))
{
if(strcmp(lhs, "BaudRate") == 0)
port->setSpeed(rhs);
else if(strcmp(lhs, "DataBits") == 0)
port->setCharSize((SerialPort::CharSize) rhs);
else if(strcmp(lhs, "StopBits") == 0)
port->setDoubleStopBits(rhs == 2);
else if(strcmp(lhs, "Parity") == 0)
port->setParity((SerialPort::Parity) rhs);
}
MLDisownString(stdlink, lhs);
}
}
}
MLPutFunction(stdlink, "List", 4);
MLPutFunction(stdlink, "Rule", 2);
MLPutString(stdlink, "BaudRate");
MLPutInteger(stdlink, port->getSpeed());
MLPutFunction(stdlink, "Rule", 2);
MLPutString(stdlink, "DataBits");
MLPutInteger(stdlink, port->getCharSize());
MLPutFunction(stdlink, "Rule", 2);
MLPutString(stdlink, "StopBits");
MLPutInteger(stdlink, port->getDoubleStopBits() ? 2 : 1);
MLPutFunction(stdlink, "Rule", 2);
MLPutString(stdlink, "Parity");
MLPutInteger(stdlink, port->getParity());
return;
}
}
}
}
MLPutSymbol(stdlink, "$Failed");
}
char* serialReadyQ(int thePort, double theTimeout)
{
SerialPort* port = getPortObject(thePort, true);
if(port != NULL)
{
if(port->isDataAvailable(theTimeout))
return "True";
else
return "False";
}
else
return "$Failed";
}
#define kBufferSize 1024
void serialRead(int thePort, double theTimeout)
{
SerialPort* port = getPortObject(thePort, true);
if(port != NULL)
{
char buffer[kBufferSize];
size_t res = port->readData(buffer, kBufferSize, theTimeout);
MLPutByteString(stdlink, (kucharp_ct) buffer, res);
}
else
MLPutSymbol(stdlink, "$Failed");
}
void serialWrite(int thePort, kucharp_ct theData, int len)
{
SerialPort* port = getPortObject(thePort, true);
if(port != NULL)
{
len = port->writeData((char*) theData, len);
MLPutByteString(stdlink, theData, len);
}
else
MLPutSymbol(stdlink, "$Failed");
}
/*
* MathLink template
*/
:Evaluate: Begin["SerialIO`Private`"]
:Begin:
:Function: serialOpen
:Pattern: serialOpen[pathName_String]
:Arguments: { pathName }
:ArgumentTypes: { String }
:ReturnType: Manual
:End:
:Begin:
:Function: serialClose
:Pattern: serialClose[port_Integer]
:Arguments: { port }
:ArgumentTypes: { Integer }
:ReturnType: Symbol
:End:
:Begin:
:Function: serialSetOptions
:Pattern: serialSetOptions[port_Integer, opts_List]
:Arguments: { port, opts }
:ArgumentTypes: Manual
:ReturnType: Manual
:End:
:Begin:
:Function: serialReadyQ
:Pattern: serialReadyQ[port_Integer, timeout_Real]
:Arguments: { port, timeout }
:ArgumentTypes: { Integer, Real }
:ReturnType: Symbol
:End:
:Begin:
:Function: serialRead
:Pattern: serialRead[port_Integer, timeout_Real]
:Arguments: { port, timeout }
:ArgumentTypes: { Integer, Real }
:ReturnType: Manual
:End:
:Begin:
:Function: serialWrite
:Pattern: serialWrite[port_Integer, data_String]
:Arguments: { port, data }
:ArgumentTypes: { Integer, ByteString }
:ReturnType: Manual
:End:
:Evaluate: End[]
/*
* main()
*/
#if WINDOWS_MATHLINK
int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR lpszCmdLine, int)
{
char buff[512];
char FAR * buff_start = buff;
char FAR * argv[32];
char FAR * FAR * argv_end = argv + 32;
MLScanString( argv, &argv_end, &lpszCmdLine, &buff_start);
return MLMain( argv_end - argv, argv);
}
#else
int main(int argc, charpp_ct argv)
{
return MLMain(argc, argv);
}
#endif // WINDOWS_MATHLINK