Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to open a modem on any side
- local function openModem()
- local sides = {"left", "right", "top", "bottom", "front", "back"}
- for _, side in ipairs(sides) do
- if peripheral.getType(side) == "modem" then
- rednet.open(side)
- return true
- end
- end
- return false
- end
- -- CLI function to send commands to the turtle
- local function turtleCLI(turtleId)
- if not openModem() then
- print("No modem found. Please attach a modem to the computer.")
- return
- end
- print("Connected to turtle " .. turtleId .. ". Type 'exit' to quit.")
- while true do
- io.write("> ")
- local command = read()
- if command == "exit" then
- break
- end
- rednet.send(turtleId, command)
- local senderId, response = rednet.receive(5) -- Wait for up to 5 seconds for a response
- if senderId == turtleId then
- print("Response: " .. response)
- else
- print("No response from turtle " .. turtleId)
- end
- end
- rednet.close()
- end
- -- Main function to start the CLI
- local args = { ... }
- if #args < 1 then
- print("Usage: remote <turtle-id>")
- else
- local turtleId = tonumber(args[1])
- if turtleId then
- turtleCLI(turtleId)
- else
- print("Invalid turtle ID.")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement