Skip to content

Instantly share code, notes, and snippets.

@pabloko
Created April 27, 2021 19:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pabloko/e9c19f6a4b960e383a33833549cc1bb5 to your computer and use it in GitHub Desktop.
Save pabloko/e9c19f6a4b960e383a33833549cc1bb5 to your computer and use it in GitHub Desktop.
Lua 5.1 / luajit - Serial port library (rs232) [win/mac/linux]
//Using rs232 library: https://github.com/mrh1997/rs232/ by Frédéric Meslin, Florent Touchard
#include <Windows.h>
#pragma comment(lib, "kernel32")
#pragma comment(lib, "lua5.1.lib")
#include <lua.hpp>
#include "rs232.h"
char buffer[0xFFF] = { 0 };
BOOL luaopen_serial (lua_State* L)
{
#define LUA_TB(x) x, [] (lua_State *L) -> int
const luaL_reg tab_funcs_serial[] = {
{ LUA_TB ("Enumerate") {
lua_pushnumber (L, comEnumerate ());
return 1;
}},
{ LUA_TB ("GetNoPorts") {
lua_pushnumber (L, comGetNoPorts ());
return 1;
}},
{ LUA_TB ("Terminate") {
comTerminate ();
return 0;
}},
{ LUA_TB ("GetPortName") {
lua_pushstring (L, comGetPortName (lua_tonumber(L, 1)));
return 1;
}},
{ LUA_TB ("GetInternalName") {
lua_pushstring (L, comGetInternalName (lua_tonumber (L, 1)));
return 1;
}},
{ LUA_TB ("FindPort") {
lua_pushnumber (L, comFindPort (lua_tostring (L, 1)));
return 1;
}},
{ LUA_TB ("Open") {
int _baudrate = lua_tonumber (L, 2);
int _flowcontrol = lua_tonumber (L, 3);
lua_pushnumber (L, comOpen (lua_tonumber(L, 1), _baudrate | _flowcontrol));
return 1;
}},
{ LUA_TB ("Close") {
comClose (lua_tonumber (L, 1));
return 0;
}},
{ LUA_TB ("CloseAll") {
comCloseAll ();
return 0;
}},
{ LUA_TB ("Write") {
size_t _size = 0;
const char* _buf = lua_tolstring (L, 2, &_size);
lua_pushnumber (L, comWrite (lua_tonumber (L, 1), _buf, _size));
return 1;
}},
{ LUA_TB ("Read") {
int read = comRead (lua_tonumber (L, 1), buffer, sizeof(buffer));
lua_pushlstring (L, buffer, read);
return 1;
}},
{ LUA_TB ("ReadBlocking") {
int read = comReadBlocking (lua_tonumber (L, 1), buffer, sizeof(buffer), lua_tonumber (L, 2));
lua_pushlstring (L, buffer, read);
return 1;
}},
{ LUA_TB ("SetDtr") {
lua_pushnumber (L, comSetDtr (lua_tonumber (L, 1), lua_tonumber (L, 2)));
return 1;
}},
{ LUA_TB ("SetRts") {
lua_pushnumber (L, comSetRts (lua_tonumber (L, 1), lua_tonumber (L, 2)));
return 1;
}},
{ NULL, NULL }
};
luaL_openlib (L, "Serial", tab_funcs_serial, NULL);
#define LUA_PUSHENUM(x) lua_pushnumber (L, x); lua_setglobal (L, #x)
LUA_PUSHENUM (PARITY_NONE);
LUA_PUSHENUM (PARITY_EVEN);
LUA_PUSHENUM (PARITY_ODD);
LUA_PUSHENUM (PARITY_SPACE);
LUA_PUSHENUM (PARITY_MARK);
return TRUE;
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
INT num = comEnumerate ();
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment