Skip to content

Instantly share code, notes, and snippets.

@moteus moteus/com2tcp.lua
Last active May 16, 2017

Embed
What would you like to do?
Connect to serial port via tcp
local uv = require "lluv"
uv.rs232 = require "lluv.rs232"
local sockets = {}
local port = uv.rs232('COM4',{
baud = '_9600';
data_bits = '_8';
parity = 'NONE';
stop_bits = '_1';
flow_control = 'OFF';
rts = 'ON';
})
uv.tcp():bind("127.0.0.1", 5555, function(server, err)
if err then return server:close() end
server:listen(function(server, err)
if err then return end
local cli, err = server:accept()
if not cli then return server:close() end
sockets[cli] = true
cli:start_read(function(cli, err, data)
if err then
sockets[cli] = nil
return cli:close()
end
port:write(data)
end)
end)
end)
port:open(function(self, err, info, data)
if err then return self:close() end
self:start_read(function(self, err, data)
if err then return self:close() end
for s in pairs(sockets) do s:write(data) end
end)
end)
uv.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.