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 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 into a bare fd for getfd().

I'm on Linux where this should work.

ttyin = assert("/dev/ttyUSB0", "rb"));
ttyout = assert("/dev/ttyUSB0", "wb"));

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")
		-- 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;

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

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

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

-- 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"

Jon Smirl