This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
nomen_nescio's profile picture

how to handle modem message

Started by nomen_nescio, 02 January 2015 - 12:10 AM
nomen_nescio #1
Posted 02 January 2015 - 01:10 AM
the way messages gotten from the modem(API) is confusing me. when writing it to a monitor it is a table, but when printing it in the terminal it is a random string.











my code:

modem = peripheral.wrap("right")
modem.open(65533)
m = peripheral.wrap("top")
m.clear()
m.setCursorPos(1, 1)

function write(message)
  print(message)
  m.write(message)
  --m.write("melding: "..message[0]..", "..
-- "mottaker:"..tostring(message[2]))

-- unmessage = textutils.unserialize(message)
-- print(unmessage)

  x, y = m.getCursorPos()
  if y  == 1 then
    m.setCursorPos(1, 2)
  
  elseif y  == 2 then
    m.setCursorPos(1, 3)
  
  elseif y  == 3 then
    m.setCursorPos(1, 4)
  
  else
    m.setCursorPos(1, 1)
  end

end

while true do

  event, modemSide, senderChannel,
  senderID, message, distance = os.pullEvent("modem_message")

  write(message)


end

the program is meant to write all rednet communication from the network to the monitor. it kind of does so in it's current state, but i'd like it represented in a different way. could anyone explain why writing and printing the message gives two different results and how to get the message in a string?

message[0] and message[2] returns nil, which is very confusing to me. how can i get information out of "message"?

Answers and suggestions appreciated.
wieselkatze #2
Posted 02 January 2015 - 01:43 AM
message is actually a table. As far as I know, monitors serialize every input they get - integers also get converted to numbers with decimal points.
If you want to see your message in the same form on the computer display, just use

print( textutils.serialize( message ) )

Regarding message[0] and message[2] it is as easy as that: You're wanting to get the value from the table with index 0 or 2 ( lua tables also do start at 1 ), but the index used isn't an integer, but a string.
That means that you either have to use

message["nRecipient"]
or

message.nRecipient

Both of these are valid. This, I think, would cover message[2]. For message[0] just use message["message"] or message.message - it's as easy as that :)/>

Side note: You also may not overwrite the global write() function - that could cause issues. Better use another variable name instead
Edited on 02 January 2015 - 12:45 AM
krzys_h #3
Posted 02 January 2015 - 09:35 AM
You're trying to receive message that was sent using rednet.send via lower level modem api. You probably want to use rednet.receive() or rednet_message event instead.

rednet.receive() version:

rednet.open("right")
-- the rest of your code
while true do
  senderID, message, protocol = rednet.receive()
  write(message)
end
Event version:

rednet.open("right")
-- the rest of your code
while true do
  event, senderID, message, protocol = os.pullEvent("rednet_message")
  write(message)
end
wieselkatze #4
Posted 02 January 2015 - 12:05 PM
You're trying to receive message that was sent using rednet.send via lower level modem api. You probably want to use rednet.receive() or rednet_message event instead.

rednet.receive() version:

rednet.open("right")
-- the rest of your code
while true do
  senderID, message, protocol = rednet.receive()
  write(message)
end
Event version:

rednet.open("right")
-- the rest of your code
while true do
  event, senderID, message, protocol = os.pullEvent("rednet_message")
  write(message)
end

There's nothing wrong with using 'modem_message' - 'rednet_message' is just an event triggered by the rednet API when a modem message comes in; it is just filtering the messages by checking if the channel the message was sent on is equivalent to the computer ID.
Both ways would work fine.
Bomb Bloke #5
Posted 02 January 2015 - 12:28 PM
There's nothing wrong with using 'modem_message' - 'rednet_message' is just an event triggered by the rednet API when a modem message comes in; it is just filtering the messages by checking if the channel the message was sent on is equivalent to the computer ID.
Both ways would work fine.

Prior to ComputerCraft 1.6, this is mostly true; on or after that build, modem_message events contain a table which is unpacked in the respective rednet_message event. krzys_h is correct in that you're better off pulling that rednet_message event directly rather than trying to unpack that table yourself.
nomen_nescio #6
Posted 02 January 2015 - 12:52 PM
You're trying to receive message that was sent using rednet.send via lower level modem api. You probably want to use rednet.receive() or rednet_message event instead.

rednet.receive() version:

rednet.open("right")
-- the rest of your code
while true do
  senderID, message, protocol = rednet.receive()
  write(message)
end
Event version:

rednet.open("right")
-- the rest of your code
while true do
  event, senderID, message, protocol = os.pullEvent("rednet_message")
  write(message)
end

rednet.recive() requires the message to be sent to this computer. i'm monitoring channel 65533 because all message are also sent on that channel.

message is actually a table. As far as I know, monitors serialize every input they get - integers also get converted to numbers with decimal points.
If you want to see your message in the same form on the computer display, just use

print( textutils.serialize( message ) )

Regarding message[0] and message[2] it is as easy as that: You're wanting to get the value from the table with index 0 or 2 ( lua tables also do start at 1 ), but the index used isn't an integer, but a string.
That means that you either have to use

message["nRecipient"]
or

message.nRecipient

Both of these are valid. This, I think, would cover message[2]. For message[0] just use message["message"] or message.message - it's as easy as that :)/>

Side note: You also may not overwrite the global write() function - that could cause issues. Better use another variable name instead

thanks. message["message"] and message["nRecipient"] works perfectly.