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

Delay with wired modems or OpenPeripheral

Started by Bmandk, 30 May 2014 - 03:33 PM
Bmandk #1
Posted 30 May 2014 - 05:33 PM
Hello

I have recently started using ComputerCraft to control my base. I have OpenPeripherals and thermal expansion on it, with more, but these are the mods I'm using together right now.

So I have 14 Resonant Energy Cells from Thermal Expansion to store my energy, and they're all hooked up to wired modems. I am pulling off their energy stored from each cell, so I can monitor my energy network. The problem I have is that with each request, there's a small delay. I remade a very simple code in my test world, which is here:


cells = {"cofh_thermalexpansion_energycell_0", "cofh_thermalexpansion_energycell_1", "cofh_thermalexpansion_energycell_2", "cofh_thermalexpansion_energycell_3", "cofh_thermalexpansion_energycell_4", "cofh_thermalexpansion_energycell_5", "cofh_thermalexpansion_energycell_6", "cofh_thermalexpansion_energycell_7", "cofh_thermalexpansion_energycell_8"}

for i, v in ipairs(cells) do
energy = peripheral.call("back", "callRemote", v, "getEnergyStored", "direction")
print(i..": "..energy)
end

Basically, the script iterates through all the cells and calls the function getEnergyStored from each cell with the peripheral.call() function. Here is a low-FPS gif, which shows the resulting delay:


I tried googling this, and couldn't find anything.

I would like to know if there's a way to not have this delay, as it stops all other interaction with the computer, like keypresses and such. If it's just how it is with the function or modems, a solution I see is to set up another computer, and just have it read the energy, and send it over modems, but if there's a delay there, then it wouldn't matter. I would also like to have it all on one computer.
CometWolf #2
Posted 30 May 2014 - 05:48 PM
This is an issue with OpenP, based on my own experiences this seems to happen whenever an openP function is used. Idk why though, ask theoriginalbit :P/>
Edited on 30 May 2014 - 03:49 PM
Bmandk #3
Posted 30 May 2014 - 05:49 PM
Alright, thanks. Do you know where I can get a hold of him?
CometWolf #4
Posted 30 May 2014 - 06:00 PM
He should show up in this topic eventually. Otherwise i suppose you could send him a pm or something, but im rather curious as to wether there is any way to remedy this myself. It's quite annoying, as even events are missed during that time.
Bmandk #5
Posted 30 May 2014 - 06:04 PM
Well, as I stated in the first post, a possible workaround for this would be to have a second computer use the peripheral.call function, and then send that to the main computer via modems. Then the OpenPeripheral isn't even used there, and you can use the os.pullEvent to listen for the modem there.
CometWolf #6
Posted 30 May 2014 - 06:28 PM
Seeing as all CC comps run on the same thread, i can't actually guarantee that'll work either. Worth a try though i suppose.
Lyqyd #7
Posted 30 May 2014 - 07:38 PM
All you'd need to do is use a second coroutine if the OpenP peripheral calls are yielding internally.
apemanzilla #8
Posted 30 May 2014 - 07:59 PM
All you'd need to do is use a second coroutine if the OpenP peripheral calls are yielding internally.
They're not; I've checked the basic code. It looks like they're running synchronously and just being delayed for some reason.
Bmandk #9
Posted 30 May 2014 - 08:25 PM
Yeah, I just set up my system with a 2nd computer, wasn't too hard, but still a hassle. It works just as I wanted without a delay, and I can use the os.pullEvent without any delay, as there is only delay on the computer pulling the energy levels.
apemanzilla #10
Posted 30 May 2014 - 08:31 PM
Yeah, I just set up my system with a 2nd computer, wasn't too hard, but still a hassle. It works just as I wanted without a delay, and I can use the os.pullEvent without any delay, as there is only delay on the computer pulling the energy levels.
Alright. If it becomes an issue again submit an issue report on their GitHub where they'll see it and be able to address the issue.
Mr_Programmer #11
Posted 31 May 2014 - 02:03 PM
please could you explane what this code is doing as im a noob and what to know what your doing here?


cells = {"cofh_thermalexpansion_energycell_0", "cofh_thermalexpansion_energycell_1", "cofh_thermalexpansion_energycell_2", "cofh_thermalexpansion_energycell_3", "cofh_thermalexpansion_energycell_4", "cofh_thermalexpansion_energycell_5", "cofh_thermalexpansion_energycell_6", "cofh_thermalexpansion_energycell_7", "cofh_thermalexpansion_energycell_8"}

for i, v in ipairs(cells) do
energy = peripheral.call("back", "callRemote", v, "getEnergyStored", "direction")
print(i..": "..energy)
end
Bomb Bloke #12
Posted 31 May 2014 - 02:22 PM
The first line rigs a table such that cells[1] is "cofh_thermalexpansion_energycell_0", cells[2] is "cofh_thermalexpansion_energycell_1", and so on.

A loop is then started based on the contents of the table. On the first iteration "i" will be 1 and "v" will be "cofh_thermalexpansion_energycell_0", on the next iteration "i" will be 2 and "v" will be "cofh_thermalexpansion_energycell_1", and so on.

Whichever peripheral is currently specified by "v" gets its "getEnergyStored" function called each time the loop repeats, and the result goes into a (redundant) "energy" variable which then gets printed.
Mr_Programmer #13
Posted 31 May 2014 - 02:25 PM
The first line rigs a table such that cells[1] is "cofh_thermalexpansion_energycell_0", cells[2] is "cofh_thermalexpansion_energycell_1", and so on.

A loop is then started based on the contents of the table. On the first iteration "i" will be 1 and "v" will be "cofh_thermalexpansion_energycell_0", on the next iteration "i" will be 2 and "v" will be "cofh_thermalexpansion_energycell_1", and so on.

Whichever peripheral is currently specified by "v" gets its "getEnergyStored" function called each time the loop repeats, and the result goes into a (redundant) "energy" variable which then gets printed.
ok i understand abit now, but if i wanted this part of code then how would i get it to print on a monitor each cells energy on a new line so like cell 1 will be on line 1, cell 2 will be on line 2 etc…
KingofGamesYami #14
Posted 01 June 2014 - 03:15 PM
The first line rigs a table such that cells[1] is "cofh_thermalexpansion_energycell_0", cells[2] is "cofh_thermalexpansion_energycell_1", and so on.

A loop is then started based on the contents of the table. On the first iteration "i" will be 1 and "v" will be "cofh_thermalexpansion_energycell_0", on the next iteration "i" will be 2 and "v" will be "cofh_thermalexpansion_energycell_1", and so on.

Whichever peripheral is currently specified by "v" gets its "getEnergyStored" function called each time the loop repeats, and the result goes into a (redundant) "energy" variable which then gets printed.
ok i understand abit now, but if i wanted this part of code then how would i get it to print on a monitor each cells energy on a new line so like cell 1 will be on line 1, cell 2 will be on line 2 etc…
add this to the top of his code.

mon = peripheral.wrap( "yoursidehere" )
term.redirect( mon )
theoriginalbit #15
Posted 02 June 2014 - 12:58 PM
This is an issue with OpenP, based on my own experiences this seems to happen whenever an openP function is used. Idk why though, ask theoriginalbit :P/>
Alright, thanks. Do you know where I can get a hold of him?
He should show up in this topic eventually. Otherwise i suppose you could send him a pm or something, but im rather curious as to wether there is any way to remedy this myself. It's quite annoying, as even events are missed during that time.
*sigh* lol. sometimes we do need the yield, sometimes info isn't immediately available and such, but we should only be yielding when needed (assuming people code the adaptors correctly)
btw the OpenMods IRC is also quite a handy place.

Well, as I stated in the first post, a possible workaround for this would be to have a second computer use the peripheral.call function, and then send that to the main computer via modems. Then the OpenPeripheral isn't even used there, and you can use the os.pullEvent to listen for the modem there.
Seeing as all CC comps run on the same thread, i can't actually guarantee that'll work either. Worth a try though i suppose.
that'd be perfectly fine, the CC computers running in the same thread doesn't effect this. however I do suggest as Lyqyd stated and make use of coroutines, that'd be the best method.

All you'd need to do is use a second coroutine if the OpenP peripheral calls are yielding internally.
They're not; I've checked the basic code. It looks like they're running synchronously and just being delayed for some reason.
no we definitely do yield internally when needed.