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

Sending bits over modem

Started by cmdpwnd, 24 June 2015 - 04:41 AM
cmdpwnd #1
Posted 24 June 2015 - 06:41 AM
When sending data, it is sent as a string in rednet. Firstly, does this not apply when sending soley from the modem instead of via rednet? Second if no, is it still possible to send bits of data without it being interpreted as a byte?
Bomb Bloke #2
Posted 24 June 2015 - 07:09 AM
Lua offers a limited selection of types compared to most other languages; strings, numbers, booleans, tables, functions, coroutines, and nil. Per the wiki, you can send any of these types directly via modems, with the exception of functions and coroutines (which get translated to nil). (Though to be fair, that was only documented there a bit over a week ago.)

So yeah, if you specifically want to send bools, just send bools.

modem.transmit(targetChannel, suggestedReplyChannel, true)

Edit:

It's important to note that modem messages arrive as events on the eligible destination systems. Events are pulled from the front of the event queue (each time a system yields by eg calling os.pullEvent()), and new events are added to the end. Each such system can only handle up to 256 events in their queue at a time. That means that if you're sending data one bit per message, there is an absolute upper limit of just 32 bytes before you either allow the target system to start pulling events, or before you start losing data - and odds are the "in practise" figure will be lower, because the modem_message events aren't going to be the only ones populating the queue.

Likewise, if you eg transmit two messages for every one that's getting pulled, the queue will quickly overflow and you'll start losing information.
Edited on 24 June 2015 - 05:19 AM
Lyqyd #3
Posted 24 June 2015 - 07:18 AM
And you can send all of those types through the rednet API functions just as well as you can via the raw modem peripheral function.
cmdpwnd #4
Posted 24 June 2015 - 07:37 PM
ok, but the bool value `true` while specifies a state is still 4bytes long, so in the event that I send `true` is it sent as 4bytes or if not, what is it sent as and how long is it?
Lyqyd #5
Posted 24 June 2015 - 07:56 PM
It doesn't matter. All of those details are abstracted away on the Java side of things. A Boolean will send and be received just as fast as a ten kilobyte string, or a large table.