Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require "component"
- local event = require "event"
- local octcp = {}
- octcp.modem = component.modem
- octcp.address = ""
- octcp.port = 0
- octcp.buffer = ""
- octcp.psize = 8192
- function octcp.listen(_,_,from,port,_,message)
- if from == octcp.address and port == octcp.port then
- octcp.buffer = octcp.buffer..message
- end
- end
- function octcp.read(n)
- local s = octcp.buffer:sub(1,n)
- if octcp.buffer:len() >=n then
- octcp.buffer:sub(n+1)
- else
- octcp.buffer=""
- end
- return s
- end
- function octcp.write(s)
- local t = {}
- if s:len() > octcp.psize then
- repeat
- octcp.modem.send(octcp.address,octcp.port,s:sub(1,octcp.psize))
- s=s:sub(octcp.psize+1)
- until s:len() <= octcp.psize
- else
- octcp.modem.send(octcp.address,octcp.port,s)
- end
- end
- function octcp.isData()
- if octcp.buffer~="" then return true else return false end
- end
- function octcp.init(port,address)
- octcp.port=port
- octcp.address=address
- event.listen("modem_message",octcp.listen)
- end
- return octcp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement