Serial Port Library pure LUA

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
BruceTheGoose
Citizen
Posts: 76
Joined: Sat Sep 20, 2014 2:54 pm

Serial Port Library pure LUA

Post by BruceTheGoose »

No need for any Java running in the background. Here it is:

Code: Select all

local serialib = {
  message = "";
  port = "";
  mode = "";
}

function serialib.openPort(self,port,mode) -- Name of PORT and a mode "r" for reading and "w" for writing
  local m = mode or "r"
  local p = assert(io.open(port,m),"The port "..port.." could not be found.");
  self.port = p;
  self.mode = m;
end

function serialib.closePort() -- Closes the port
  self.port:close();
end

function serialib.read(self,length) -- Read from the port, length specifies the length of the message 
  local l = length or 1
  self.message = self.port:read(l);
  self.port:flush();
end

function serialib.write(self,message) -- Writes to the port, message is the message being sent
  local m = message or "Hello World!"
  self.port:write(m.."\n");
  self.port:flush();
end

function serialib.getMessage(self)-- If the port is in reading mode, this function returns the message it received from the port
  if(self.mode == "r") then return self.message; else return "Not reading!" end
end

return serialib;
Here is an example of it being interfaced with an Arduino Uno:

Lua code

Code: Select all

function love.load()
  s = require("Serialib");
  s:openPort("/dev/cu.wchusbserial1410","r");
end

function love.update(dt)
  s:read(5);
end

function love.draw()
 love.graphics.print(s:getMessage());
end

function love.keypressed(key)
  if(key == "escape") then
    love.quit();
  end
end
C Code (Arduino IDE):

Code: Select all

void setup() {
  Serial.begin(9600);
}

String message = "1:1:2";
boolean tix = false;

void loop() {
  delay(1000);
  Serial.print(message);
  if(tix){
    message = "1:1:2";
    tix = !tix;
  }else{
    message = "2:1:1";
    tix = !tix;
  }
}
Please post any suggested improvements in terms of code!

Happy coding!
"I don't know."
User avatar
Positive07
Party member
Posts: 1009
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Serial Port Library pure LUA

Post by Positive07 »

Linux? Because that won't work on Windows, that I'm sure of
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Serial Port Library pure LUA

Post by raidho36 »

Well yeah. Doing it in Windows is a mess.
User avatar
Positive07
Party member
Posts: 1009
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Serial Port Library pure LUA

Post by Positive07 »

I have a binary serial library that should work both on Windows and POSIX systems... I should look for it
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests