lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I'm trying to use the trick of wrapping a file descriptor for a serial
port in a dummy socket and then using socket.select to do non-blocking
IO on it.

I can verify that the object for ttyin is on the list of read/write
sockets being passed into select, but I am never getting any events.
Is this the right way to implement getfd()?  getfd = function ()
return ttyin; end; I can't figure out to convert the FILE* from
io.open into a bare fd for getfd().

I'm on Linux where this should work.

ttyin = assert(io.open("/dev/ttyUSB0", "rb"));
ttyin:setvbuf("no");
ttyout = assert(io.open("/dev/ttyUSB0", "wb"));
ttyout:setvbuf("no");


local serial = {
	getfd = function () return ttyin; end;
	dirty = function (self) return false; end;
	settimeout = function () end;
	send = function (_, data, i, j)
		print("serial write\n")
		ttyout:write(byte(i,j))
		-- Send data:sub(i,j) to serial, return bytes written
		return #data, 0; end;
	close = function () end;
	receive = function (_, patt)
		print("in serial receive function\n");
		local data = ttyin:read(1000);
		return data;
	end
};

local function on_incoming(serial, data)
	-- Do something with received serial data
	print("incoming serial ", data)
end

local function on_disconnect(serial, reason)
	-- Serial disconnect
	print("serial disconnect")
end

local function on_connect(serial, reason)
	-- Serial connect
	print("serial connect")
end

-- Registers the fake socket with net.server, and returns the
server.lua conn wrapper object
local serial_conn = require "net.server".wrapclient(serial, "serial", 0, {
	onincoming = on_incoming, onconnect = on_connect, ondisconnect =
on_disconnect }, "*a");

command = "\002\096"
serial_conn:write(command);
ttyout:write(command);ttyout:flush()


-- 
Jon Smirl
jonsmirl@gmail.com