Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
squall2044

Define ports at begining of program

Question

Description:

  I am writing a program to automatically control an iris from sgcraft and as such I am opening and closing the same port alot within the same program.

 

Function:

  I would like to be able to define ports at the top of the program and have it populate through.

For example instead of:

local c = require("component")

local e = reuire("event")

local m = c.modem

m.open(123)

m.broadcast(123, "HI!")

m.close(123)

 

It would be great if we could go like this:

local c = require("component")

local e = reuire("event")

local m = c.modem

local p = "123"

m.open(p)

m.broadcast(p, "HI!")

m.close(p)

 

It would also allow for easy changeing of the port in large programs.

Deadline:

  There is no deadline, I'm just tossing you guys an idea.

 

Additional Information:

  Simply ask me if you have other questions

 

Have Fun!!!

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Why do you need to open and close the port every time you send a message over the port? You can open the port at the beginning of the program, and close the port at the end.

Also, you were almost right with your "if we could" code. You just didn't need to make the port a string.

local com = require("component")

local modem = com.modem

local port = 123

modem.open(port)

modem.broadcast(port, "Hello")
modem.broadcast(port, "world")
modem.broadcast(port, "Another message")

modem.close(port)
Link to post
Share on other sites
  • 0

It's not each and every time I send a message it is at certain points in the code.

Here is the program I have:

 

local h ="Hello"

local c = require("component")

local e = require("event")

local t = require("term")

local m = c.modem

local p = "12345\n"

local s = c.getPrimary("stargate")


 
t.clear()


while true do

print(e.pull("sgDialIn"))
print("Incoming Wormhole")

print("Closing Iris")

s.closeIris()

m.open(420) --- Choose whatever port you want

print(m.isOpen(420))
print("Channel Open")

e.pull("sgStargateStateChange")

os.sleep(5)

m.broadcast(420, h)

local _, _, from, port, _, handshake = e.pull("modem_message")

 if handshake == h then

  os.sleep(1)
 
   print("Handshake Succeeded. Awaiting Signal")
  
    m.send(from, 420, "Awaiting Signal. Please enter IDC now.")

     local _, _, from, port, _, message = e.pull("modem_message")

      if message == p then

       print("Code: " ..(message).. " Accpeted, Opening Iris")

        s.openIris()

         os.sleep(3)

          m.send(from, 420, "Iris Open. Welcome Home") --- port must be the same as the one you chose earlier

           print("Welcome Home")

            os.sleep(10)

             print("Closing Gate")

              s.disconnect()

               m.close(420)

 else if message ~= p then ---password must be the same as the one you chose earlier

  os.sleep(2)
   print("Code: " ..(message).." Not Accepted, Disconnecting Gate")

     m.send(from, 420, "IDC Rejected")

      os.sleep(4)

       s.disconnect()

        e.pull("sgStargateStateChange")

         os.sleep(1)

          print("Gate Disconnected, Opening Iris")

           s.openIris()

            print("Good-Bye")

             m.close(420)

  end

 end

else if handshake ~= h then

 print("Handshake Failed!")

  os.sleep(1)

   m.send(from, 420, "Go Away")

    os.sleep(1)

     s.disconnect()

      s.openIris()

       m.close(420)

  end

 end

end

 

As you can see I am opening the port when there is an incoming wormhole and then closing it again at the end of each section also there is alot of sending instead of broadcasting for security's sake. Thank you so much I made the minor change in the code and it works perfectly now.

Edited by squall2044
Fixing a formatting error
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.