45#include "BufferedAsyncSerial.h"
46#include <scolMemoryHelper.hpp>
58int SERIALSCOL_NB_CB = 1;
61int SCOL_SERIAL_READ_CB = 0;
83 int baudRate, parity, byteSize, stopBits;
85 int pUser = MMpull(m);
87 int pSettings = MMpull(m);
88 int pName = MMpull(m);
94 MMechostr (MSKDEBUG,
"\n_openSIO error : IO device name is nil...\n");
99 std::string portnum((
char*)MMstartstr(m, MTOP(pName)));
100 if (pSettings == NIL)
109 pSettings = MTOP(pSettings);
110 baudRate = MTOI(MMfetch (m, pSettings, 0));
111 parity = MTOI(MMfetch (m, pSettings, 1));
112 byteSize = MTOI(MMfetch (m, pSettings, 2));
113 stopBits = MTOI(MMfetch (m, pSettings, 3));
119 MMechostr (0,
"\n_openSIO error : reading callback is nil...\n");
124 MMechostr(0,
"_openSIO : new SerialIO\n");
127 boost::asio::serial_port_base::character_size bsize(byteSize);
128 boost::asio::serial_port_base::parity bparity = boost::asio::serial_port_base::parity((parity == 1) ? boost::asio::serial_port_base::parity::odd : (parity == 2) ? boost::asio::serial_port_base::parity::even : boost::asio::serial_port_base::parity::none);
129 boost::asio::serial_port_base::stop_bits bstop = boost::asio::serial_port_base::stop_bits((stopBits == 3) ? boost::asio::serial_port_base::stop_bits::onepointfive : (parity == 2) ? boost::asio::serial_port_base::stop_bits::two : boost::asio::serial_port_base::stop_bits::one);
131 BufferedAsyncSerial* serialObj =
new BufferedAsyncSerial(portnum, baudRate, bparity, bsize, boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::none), bstop);
145 if ((MMpushPointer(m, serialObj) != 0))
147 SAFE_DELETE(serialObj);
152 if ((k = OBJcreate(m, OBJSERIALSCOL, SCOL_PTR serialObj, NIL, 0)))
return k;
155 if ((k = MMpush(m, pCbk)))
return k;
156 if ((k = MMpush(m, pUser)))
return k;
157 if ((k = OBJaddreflex(m, OBJSERIALSCOL, SCOL_SERIAL_READ_CB)))
return k;
175 MMechostr(MSKDEBUG,
"SCcloseSIO\n");
178 int objtab = MMget(m, 0);
181 MMechostr(MSKDEBUG,
"_closeSIO : SerialIO NIL\n");
186 OBJdelTM(m, OBJSERIALSCOL, objtab);
187 MMset(m, 0, ITOM(0));
190 MMechostr(MSKDEBUG,
"ok\n");
208 int objtab = MMget(m, 0);
209 if ((s == NIL) || (objtab == NIL))
222 std::string data = (
char*)MMstart(m, (MTOP(s))+1);
225 MMset(m, 0, ITOM(0));
230int getSerialReadCb(mmachine m, SCOL_PTR_TYPE
id, SCOL_PTR_TYPE param)
234 std::string* data = (std::string*)param;
243 if (OBJbeginreflex(m, OBJSERIALSCOL, SCOL_PTR serialObj, SCOL_SERIAL_READ_CB))
249 int res = MMmalloc (m,((data->size()+4)>>2)+1, TYPEBUF);
250 *(
int*) MMstart(m, res) = data->size();
251 char* BS = (
char*)MMstart(m, res + 1);
252 memcpy(BS, data->data(), data->size());
254 if ((k = MMpush(m, (res<<1) + 1)))
return k;
255 if ((k = MMpush(m, ITOM(data->size()))))
return k;
257 k=OBJcallreflex(m, 2);
262static NativeDefinition sSerialDef[] =
264 {
"_openSIO", 5,
"fun [Chn S [I I I I] fun [SerialIO u0 S I] I u0] SerialIO",
_openSIO },
265 {
"_closeSIO", 1,
"fun [SerialIO] I",
_closeSIO },
266 {
"_writeSIO", 2,
"fun [SerialIO S] I",
_writeSIO }
270int destroySerialIO(mmachine m, SCOL_PTR_TYPE handsys,
int objtab)
273 SAFE_DELETE(serialObj);
274 MMsetPointer<BufferedAsyncSerial*>(m, MTOP(objtab), 0);
286int LoadSerialIO(mmachine m)
289 OBJSERIALSCOL = OBJregister(SERIALSCOL_NB_CB, 0, destroySerialIO,
"SerialIO");
294 SERIAL_READ_CB = OBJgetUserEvent();
295 OBJdefEvent(SERIAL_READ_CB, getSerialReadCb);
298 int k = PKhardpak2(m,
"serialIO.pkg-1.0",
sizeof(sSerialDef) /
sizeof(sSerialDef[0]), sSerialDef);
300 MMechostr(MSKDEBUG,
"\n" );
309extern "C" SCOL_EXPORT
int ScolLoadPlugin(mmachine m, cbmachine w)
321extern "C" SCOL_EXPORT
int ScolUnloadPlugin()
329extern "C" SCOL_EXPORT
int ScolSerialIOLoadPlugin(mmachine m, cbmachine w)
341extern "C" SCOL_EXPORT
int ScolSerialIOUnloadPlugin()
void writeString(const std::string &s)
int _closeSIO(mmachine m)
_closeSIO : This function destroy a serial object and close the connection
int _writeSIO(mmachine m)
_writeSIO : This function send data on serial device
int _openSIO(mmachine m)
_openSIO : This function open a serial port