Help with intercept code.

Trying to wrap my head around luup.io.intercept() can someone help with the following: -

  1. Send command in raw mode.
  2. Wait for 3 byte reply.
  3. Send command again.

My understanding is that the intercept should go in before the send and it will receive on byte then reset as per the following code: -

[code]function DIDIOTransmit(Command, DALICommand, commandType)

local byteCount = 0
local commandRepeat = false
local byte1 = string.byte(Command, 5)
local byte2 = string.byte(Command, 6)

log("(DIDIOTransmit) commands:" .. string.format("%02X",byte1) .. ":" .. string.format("%02X",byte2) .. ":",3)
if (tonumber(commandType) == 1) then
	if (DALICommand == DALI_INIT or DALICommand == DALI_RAND) then
		commandRepeat = true
	else
		commandRepeat = false
	end
end

if(luup.io.write(Command) == false) then
	log("(DIDIOTransmit) fail:" .. string.format("%02X",byte1) .. ":" .. string.format("%02X",byte2) .. ":",2)
	return false
end

if(commandRepeat == true)then

--loop goes here to detect 3 bytes
	while(byteCount < 3) do
		incoming_data = luup.io.read();
			if (incoming_data ~= nil) then
			log("DIDIOTransmit:incoming data:" .. string.format("%02X",tonumber(string.byte(incoming_data))))
				byteCount = (byteCount + 1)
				luup.io.intercept()
			end
	end

	if(luup.io.write(Command) == fail) then
		log("(DIDIOTransmit) fail:" .. string.format("%02X",byte1) .. ":" .. string.format("%02X",byte2) .. ":",2)
		return false
	end
	return true
end

return true

end[/code]

I seem to be getting inconsistent results so was hoping someone could point me in the right direction.

The code above is causing restarts?

Thanks

John

Oops fixed missed the initial intercept before the write.

[code]function DIDIOTransmit(Command, DALICommand, commandType)

local byteCount = 0
local commandRepeat = false
local byte1 = string.byte(Command, 5)
local byte2 = string.byte(Command, 6)

log("(DIDIOTransmit) commands:" .. string.format("%02X",byte1) .. ":" .. string.format("%02X",byte2) .. ":",3)
if (tonumber(commandType) == 1) then
	if (DALICommand == DALI_INIT or DALICommand == DALI_RAND) then
		commandRepeat = true
		luup.io.intercept()
	else
		commandRepeat = false
	end
end

if(luup.io.write(Command) == false) then
	log("(DIDIOTransmit) fail:" .. string.format("%02X",byte1) .. ":" .. string.format("%02X",byte2) .. ":",2)
	return false
end

if(commandRepeat == true)then

--loop goes here to detect 3 bytes
	while(byteCount < 3) do
		incoming_data = luup.io.read()
			if (incoming_data ~= nil) then
			log(":DIDIOTransmit:incoming data:" .. string.format("%02X",tonumber(string.byte(incoming_data))))
				byteCount = (byteCount + 1)
				luup.io.intercept()
			end
	end
	if(luup.io.write(Command) == fail) then
		log("(:DIDIOTransmit:fail:" .. string.format("%02X",byte1) .. ":" .. string.format("%02X",byte2) .. ":",2)
		return false
	end
	return true
end
return true

end[/code]

Cheers

John