PDA

View Full Version : Convert to Serial plugin to Transport



rickd
January 30th, 2013, 06:52 PM
I have two serial plugins with device manager providers here is my Receiver one.

How do I convert this to a Transport so i can start using a Global
Cache network based serial port.

I have looked at the simple transport code and just need a little help to figure out what needs to be substituted from the serial class for transport class.



eg.

C:\Program Files\Promixis\Girder5\plugins\serial\JVCHD100.lua

--]]

require 'Classes.Publisher'

--command header and command types
local command = '21'
local reference = '3F'
local ref_response = '40'
local header = '8901'
local input = '4950'
local power = '5057'
local gammatable = '4754'
local gammacoeff = '4750'
local remote = '5243'


local device = serial.Classes.Queued:New({ --what changes here

Name = "JVC HD100 Projector",
Description = "JVC HD100",

BaudRate = 19200,
Parity = 0,
--StartBits = 0,
StopBits = 0,
DataBits = 8,
FlowControl = 'N',
IntraCharacterDelay = 0,
CallbackType = serial.CB_TERMINATED,
--SendStartByte = '',
SendTerminator = serial.hextobyte ('0A'),
ReceiveStartByte = serial.hextobyte ('06'),
ReceiveTerminator = serial.hextobyte ('0A'),
IncompleteResponseTimeout = 200,
NoResponseTimeout = 200,
LogLevel = 0,
GlobalName = 'JVCProjector',


Initialize = function (self)
self.Publisher = Classes.Publisher:New()
self.Provider = DeviceManager and DeviceManager.Providers.Start('JVCHD100Provider')
gir.LogMessage(self.Name, 'Communications OK',3)
self.Status = "Communication OK"
return serial.Classes.Queued.Initialize (self)
end,

Event = function(self, ...)
self.Publisher:Event(unpack(arg))
-- print(unpack(arg))
gir.LogMessage(self.Name, table.concat(arg, ' '), 3)
end,

Subscribe = function(self, func)
return self.Publisher:Subscribe(func)
end,

Unsubscribe = function(self, func)
self.Publisher:Unsubscribe(func)
end,

SendCommand = function (self,command,callback,topofque,force)
return serial.Classes.Queued.SendCommand (self,command,callback,topofque,force)
end,




ReceiveResponse = function ( self, data, code ) --is this still valid????

if (math.band (code,serial.RXCHAR) > 0) and data then
print ('Length: '..string.len(data))

if string.len(data)==5 then
local _, _, ack, id, cmdtype = string.find(data, '(.)(..)(..)')
local ackx = string.byte(ack)
local ackbyte = string.format('%02x',ackx)
local ackn=tonumber(ackbyte)
if ackn==6 then
print ('JVC Command Acknowledged : '..cmdtype)
gir.LogMessage(self.Name, cmdtype..':JVC Command Acknowledged',3)
if JVCcommandsent == "PowerOff" and cmdtype == "PW" then
--acknowledgement of command triggers update to provider by checking last command sent and acknowleged command
self:Event('Power','Off')
JVCpower=0
elseif JVCcommandsent == "PowerOn" and cmdtype == "PW" then
self:Event('Power','On')
JVCpower=1
elseif JVCcommandsent == "InputSVideo" and cmdtype == "IP" then
self:Event('Source','SVideo')
--consider put in the JVCinput
elseif JVCcommandsent == "InputVideo" and cmdtype == "IP" then
self:Event('Source','Video')
elseif JVCcommandsent == "InputComponent" and cmdtype == "IP" then
self:Event('Source','Component')
elseif JVCcommandsent == "InputHDMI1" and cmdtype == "IP" then
self:Event('Source','HDMI1')
elseif JVCcommandsent == "InputHDMI2" and cmdtype == "IP" then
self:Event('Source','HDMI2')

elseif JVCcommandsent == "GammaNormal" and cmdtype == "GT" then
self:Event('Gamma','Normal')
elseif JVCcommandsent == "GammaTheater1" and cmdtype == "GT" then
self:Event('Gamma','Theater1')
elseif JVCcommandsent == "GammaTheater2" and cmdtype == "GT" then
self:Event('Gamma','Theater2')
elseif JVCcommandsent == "GammaDynamic" and cmdtype == "GT" then
self:Event('Gamma','Dynamic')
elseif JVCcommandsent == "GammaCustom" and cmdtype == "GT" then
self:Event('Gamma','Custom')

elseif JVCcommandsent == "GammaCoeff_1.8" and cmdtype == "GP" then
self:Event('Coeff','1_8')
elseif JVCcommandsent == "GammaCoeff_1.9" and cmdtype == "GP" then
self:Event('Coeff','1_9')
elseif JVCcommandsent == "GammaCoeff_2.0" and cmdtype == "GP" then
self:Event('Coeff','2_0')
elseif JVCcommandsent == "GammaCoeff_2.1" and cmdtype == "GP" then
self:Event('Coeff','2_1')
elseif JVCcommandsent == "GammaCoeff_2.2" and cmdtype == "GP" then
self:Event('Coeff','2_2')
elseif JVCcommandsent == "GammaCoeff_2.3" and cmdtype == "GP" then
self:Event('Coeff','2_3')
elseif JVCcommandsent == "GammaCoeff_2.4" and cmdtype == "GP" then
self:Event('Coeff','2_4')
elseif JVCcommandsent == "GammaCoeff_2.5" and cmdtype == "GP" then
self:Event('Coeff','2_5')
elseif JVCcommandsent == "GammaCoeff_2.6" and cmdtype == "GP" then
self:Event('Coeff','2_6')
end
end
elseif string.len(data)==6 then
local _, _, cmd, id, query, state = string.find(data, '(.)(..)(..)(.)')
print ('State requested & returned : cmd'..cmd, 'query '..query,'state '..state)
local cmdx = string.byte(cmd)
local cmdbyte = string.format('%02x',cmdx)
print (cmdbyte)
print (state)

if cmd=="@" and query == "PW" then
JVCpower = tonumber(state)
print(state..' :JVC Read Power Processed'..tonumber(JVCpower))
gir.LogMessage(self.Name, ':JVC Read Power Processed',3)
if JVCpower==0 then self:Event('Power','Off')
elseif JVCpower==1 then self:Event('Power','On')
--elseif JVCpower==2 then self:Event('Power','Cool')
--elseif JVCpower==4 then self:Event('Power','Warning')
end
if GetStateAll==1 then self:QueryRest() end --query all was requested and if power is on it will execute and update variables and provider

elseif cmd=="@" and query == "IP" then
JVCinput = tonumber(state)
print(state..' :JVC Read Input Status Processed'..tonumber(JVCinput))
gir.LogMessage(self.Name, ':JVC Read Input State Processed',3)
if JVCinput==0 then self:Event('Source','SVideo')
elseif JVCinput==1 then self:Event('Source','Video')
elseif JVCinput==2 then self:Event('Source','Component')
elseif JVCinput==6 then self:Event('Source','HDMI1')
elseif JVCinput==7 then self:Event('Source','HDMI2')
end

elseif cmd=="@" and query == "GT" then
JVCgamma = tonumber(state)
print(state..' :JVC Read Gamma Status Processed'..tonumber(JVCgamma))
gir.LogMessage(self.Name, ':JVC Read Gamma State Processed',3)
if JVCgamma==0 then self:Event('Gamma','Normal') --publish state back to provider
elseif JVCgamma==1 then self:Event('Gamma','Theater1')
elseif JVCgamma==2 then self:Event('Gamma','Theater2')
elseif JVCgamma==3 then self:Event('Gamma','Dynamic')
elseif JVCgamma==4 then self:Event('Gamma','Custom')
end

elseif cmd=="@" and query == "GP" then
JVCgammacoeff = tonumber(state)
print(state..' :JVC Read Gamma Status Processed'..tonumber(JVCgammacoeff))
gir.LogMessage(self.Name, ':JVC Read GammaCoeff State Processed',3)
if JVCgammacoeff==0 then self:Event('Coeff','1_8')
elseif JVCgammacoeff==1 then self:Event('Coeff','1_9')
elseif JVCgammacoeff==2 then self:Event('Coeff','2_0')
elseif JVCgammacoeff==3 then self:Event('Coeff','2_1')
elseif JVCgammacoeff==4 then self:Event('Coeff','2_2')
elseif JVCgammacoeff==5 then self:Event('Coeff','2_3')
elseif JVCgammacoeff==6 then self:Event('Coeff','2_4')
elseif JVCgammacoeff==7 then self:Event('Coeff','2_5')
elseif JVCgammacoeff==8 then self:Event('Coeff','2_6')
end
end
end
end


if math.band (code,serial.ERR) > 0 then
gir.LogMessage(self.Name, 'Communcication Error',3)
--print ('JVC Error')
end

if math.band (code, serial.NORESPONSETIMEOUT) > 0 then
code = serial.zerobits (code,serial.NORESPONSETIMEOUT)
end -- tells G4 not to report communication errors\timeouts in the Lua Console

serial.Classes.Queued.ReceiveResponse (self,data,code)

end,

--command structure is Command type..Unit ID..Command Type..Data

SendHex = function(self, bytes)
local temp = string.gsub(bytes, '%s', '') -- strip out any spaces
temp = math.hextobyte(temp)
self:SendCommand(temp)
end,

Check = function (self)
return self:SendHex ('21 89 01 00 00')
end,