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

Asynchron communication with Multiple Computers

Question

Explenation:

Main Question: I want to write a centrel OC program, that acts as a central login for severel ComputerCraft controled Doors, similarly to this one, but on my own accord.

I achievd that only those Doors open, where I gave my password, but the problem is, that I can only do it in a specific order. First the first PC, then the second and the may use the first again.

Is there a way to make it possible to open the doors in any order?

Second minor question: I am using a light board from computertronics in my server Rack to see the status of the doors (change in color between green when idle and orange when opened). But because i am waiting for the client to tell me, that the door closed, i can't start a password request from my second pc. Is there a way to allow me to do this at the same time with one single program?

Third minor question: How do i have to right my code in the function, that the while loop makes the counting, so that I wouldn't need an additional if to break my loop.

Code:

CC-Client:

switch = peripheral.wrap("right")

while true do

  term.clear()  -- Read my password
  term.setCursorPos(1,1)
  print("Please enter Password:")
  input = read("*")

  input = textutils.serialise(input) -- Send password to server
  switch.transmit(2001,1,input)
  
  switch.open(2002) -- Get Answer whether or not the password was correct
  local _,_,_,_,message = os.pullEvent("modem_message")

  if message == "true" then -- If password is correct, open door
  
      rs.setAnalogOutput("left",15)
      sleep(2)
      rs.setAnalogOutput("left",0)
      
      switch.transmit(2003,1,textutils.serialise("closed")) -- Tell server that door has closed
          
  end
end  

OP-Server:

component = require("component") 
event = require("event")
term = require("term")
serialization = require("serialization")

gpu = component.gpu
modem = component.modem
light = component.light_board

local password = "12345" -- My test password

function ColorSet (index,color) -- Set the color of the light board in the server rack

  if index == 1 then -- Change color of the light board on the left side
    while (true) do
      light.setColor(index,color)
      light.setActive(index,true)
      index = index +1

      if (index >= 6) then
        break
      end
    end
  else -- Change color on the right side
    index = index +5
    while (true) do
      light.setColor(index,color)
      light.setActive(index,true)
      index = index +1

      if (index >= 11) then
        break
      end
    end
  end
return true
end

ColorSet(1,0x33FF00) -- First initialization of lightboard
ColorSet(2,0x33FF00)
term.clear() -- Clear window and debug info
gpu.set(1,1,"No Requests known.") 
gpu.set(1,4,"No Requests known.")

while true do

  modem.open(2001) -- Get password from first computer
  _,source1,_,_,_,signal1 = event.pull("modem_message")
  modem.close(2001)
  signal1 = serialization.unserialize(signal1)

  gpu.set(1,1,"PC1 did send: "..signal1.."                                       ")
  --debug info about send password
  if signal1 == password then
    ColorSet(1,0xFF6600) -- Change color of light board
    gpu.set(1,2,"Password accepted") -- debug info 
    modem.broadcast(2002,"true") -- tell client to open door
    
    modem.open(2003) -- wait for closed door
    _,_,_,_,_,close = event.pull("modem_message")
    modem.close(2003)
    ColorSet(1,0x33FF00) -- reset light board
  else
    gpu.set(1,2,"Password is wrong") -- debug info and message that password was wrong
    modem.broadcast(2002,"false")
  end

  modem.open(3001)-- repetion from above to other PC (Light board not implemented in this part of the code)
  _,source2,_,_,_,signal2 = event.pull("modem_message")
  modem.close(3001)

  signal2 = serialization.unserialize(signal2)

  gpu.set(1,4,"PC2 did send: "..signal1.."                                       ")

  if signal2 == password then
    gpu.set(1,5,"Password accepted")
    modem.broadcast(3002,"true")
  else
    gpu.set(1,5,"Password is wrong")
    modem.broadcast(3002,"false")
  end
end

If there are any additionel question, I will try to answer them.

Link to post
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Thanks for the help. I will try it als fast as possible.

For the Second question:

 i want  my light Board to be another Color, when the door is Open. To synch it with my Client, I am not using os.sleep, but I wait for a Message in Port 2003.

The Problem  in my Code is, that i have to wait for the door to close, so that i can leave the if statement. So when i want to interact with my Second pc, i have to wait, until I Know that my First door has closed.

Link to post
Share on other sites
  • 0

I tried to steamline my network code. This is what i wrote

function server (index)
  index10  = index *10

  modem.open(index10 + 1)
  _,source,_,_,_,signal = event.pull("modem_message")
  modem.close(index10 + 1)
  signal = serialization.unserialize(signal)

  gpu.set(1,1,tostring(source).." did send: "..signal)

  if signal == password then
    ColorSet(1,0xFF6600)
    gpu.set(1,2,"Password accepted")
    modem.send(source,index10 + 2,"true")

	while true do
		modem.open(index10 + 3)
		_,source1,_,_,_,close = event.pull("modem_message")
		modem.close(index10 + 3)

		if source1 == source then
			ColorSet(1,0x33FF00)
			return true
		end
	end
  else
    gpu.set(1,2,"Password is wrong")
    modem.send(source,index10 + 2,"false")
    return false
  end
end

In this function i want to sent the message, whether or not the password was correct, back to my source. My problem is that the CC-Programm only registers a message if i broadcast it, but not when i try to send my answer to him.

Is there another way to directly comunicate between OC and CC?

Link to post
Share on other sites
  • 0

I have finally made it work with this code:

function server (index)
  modem.open(2001)
  modem.open(3001)
  _,_,_,port,_,signal = event.pull("modem_message")
  modem.close(2001)
  modem.close(3001)

  gpu.set(1,10,tostring(port))
  signal = serialization.unserialize(signal)

  gpu.set(1,1,tostring(source).." did send: "..signal)

  if signal == password then
    ColorSet(1,0xFF6600)
    gpu.set(1,2,"Password accepted")
    modem.broadcast(port + 1,"true")
    gpu.set(1,11,"Answer send")

    while true do
      modem.open(port+2)
      _,_,_,port_1,_,signal = event.pull("modem_message")
      modem.open(port+2)

      if (port + 2) == port_1 then
        ColorSet(1,0x33FF00)
        return true
      end
    end
  else
    gpu.set(1,2,"Password is wrong")
    modem.broadcast(port,"false")
    return false
  end
end

 

Link to post
Share on other sites
  • 0

Probably handle the port with the open/shut message as you do the other, keeping all ports needed open at once. For example if you receive a correct password set lights to "open" and when both computers report closed set the light to "closed"

Link to post
Share on other sites
  • 0

Tell me if I understand your question correctly. You have a two door system, kind of like an airlock on a spacecraft, and you want the first door to open when the password is correct and the second door to open only after the first has closed... Is that what you are trying to achieve?

 

Link to post
Share on other sites
  • 0
Quote

Molinko

Tell me if I understand your question correctly. You have a two door system, kind of like an airlock on a spacecraft, and you want the first door to open when the password is correct and the second door to open only after the first has closed... Is that what you are trying to achieve?

Unfortunely qou miss understude. Yes a have two door system, but it is not like a airlock. I wanted it also to be expandebele as much as i want. My problem was that i waitet the first PC to ask at server. Meaning that i could only work with my second pc if the first one was  activated and finished. With this current code iteration I could circumvent all my problems:

local password = "12345"
local client_number = 1
local list = {}
local tempport = 1

function OpenModem (index)
  index = index * 10
  modem.open(index+1)
  modem.open(index+3)
  list[client_number] = index
  client_number = client_number +1
  return true
end

while true do
  
  _,_,_,port,_,signal = event.pull("modem_message")
  
  if port == 1 then
	signal = serialization.unserialize(signal)
	OpenModem(signal)
  else
	for  i = 1, client_number do

		tempport =(port - list[i])

		if tempport == 1 then
			signal = serialization.unserialize(signal)
			gpu.set(1,1,tostring(source).." did send: "..signal)

			if signal == password then
				ColorSet(i,0xFF6600)
				gpu.set(1,2,"Password accepted")
				modem.broadcast(port + 1,"true")
			else
				modem.broadcast(port + 1,"false")
			end
		elseif tempport == 3 then
			ColorSet(i,0x33FF00)
		else
   
		end
	break
	end
  end
end

In this code i leave all my ports open all the time. The last number of every port says which topic it is for.  My server recieves at port ???1 the password, at ???2 it send wheter or not  the password was correct, and port ???3 is wheter the door closes. The literative Port are planned to be for maintence and to register at server.

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.