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

Wired Modem Stops working after world re-log.

Started by Zeoic, 02 February 2014 - 12:35 PM
Zeoic #1
Posted 02 February 2014 - 01:35 PM
So, after I leave my world, then go back in, my programs seem to get stuck at anything related to modems. If I break my modem and replace it, the program works again. I have absolutely no idea why this is happening. Does anyone know of this bug (if it is a bug) and any way to fix it?

Thanks, Zeoic


ATM CODE
Spoiler

local m = peripheral.wrap("monitor_0")
local modem = peripheral.wrap("left")
local accbal = 0
local straccbal = "0"
function back()
m.setBackgroundColor(colors.white)
m.clear()
m.setTextColor(colors.black)
end
function swipe()
m.setCursorPos(2,2)
m.write("Swipe an")
m.setCursorPos(2,3)
m.write("Interact Card")
m.setCursorPos(2,4)
m.write("To Continue")
m.setCursorPos(14,5)
m.write("VVV")
end
function balance()
modem = peripheral.wrap("left")
print("balance")
acc = string.sub(cardnumber, 10, 12)
print(acc)
modem.transmit(1000,1000,acc)
sleep(0.01)
modem.open(1000)
balancetest = true
while balancetest do
e,p1,p2,p3,p4,p5 = os.pullEvent()
if e == "modem_message" then
accbal = tonumber(p4)
straccbal = p4
balancetest = false
end
sleep(0.01)
end
end
function withdraw()
balance()
modem = peripheral.wrap("left")
print("withdraw")
acc = string.sub(cardnumber, 10, 12)
print(acc)
modem.transmit(1000,1001,acc..wamount)
sleep(0.1)
modem.open(1000)
withdrawtest = true
while withdrawtest do
e,p1,p2,p3,p4,p5 = os.pullEvent()
if e == "modem_message" and p4 == "true" then
sleep(0.1)
back()
m.setBackgroundColor(colors.white)
m.setCursorPos(3,2)
m.write("Depositing Gold")
m.setCursorPos(3,5)
m.write("VVV")
for i=1, wamount, 1 do
  rs.setOutput("front",true)
  sleep(0.1)
  rs.setOutput("front", false)
  sleep(0.1)
end
os.reboot()
withdrawtest = false
elseif e == "modem_message" and p4 == "funds" then
--Insufficient Funds
sleep(0.1)
back()
basicpage()
m.setBackgroundColor(colors.white)
m.setCursorPos(1,2)
m.write("Insufficient Funds")
while true do
e,p1,p2,p3,p4,p5 = os.pullEvent()
if e == "monitor_touch" and p2 >= 18 and p3 == 1 then
os.reboot()
end
sleep(0.01)
end
else
-- unknown error
os.reboot()
end
sleep(0.01)
end
end
function basicpage()
m.setBackgroundColor(colors.white)
m.setCursorPos(2,5)
m.write("#: *** **** "..string.sub(cardnumber,10,12))
m.setBackgroundColor(colors.red)
m.setCursorPos(18,1)
m.write("X")
end
back()
swipe()
while true do
e,p1,p2,p3,p4,p5 = os.pullEvent()
if e == "mag_swipe" and string.match(p1, "ICard") then
var1, var2 = string.find(p1, "ICard")
cardnumber = string.sub(p1,var2+1,var2+12)
back()
m.setCursorPos(2,2)
m.setBackgroundColor(colors.lightGray)
m.write(" ")
m.setBackgroundColor(colors.white)
m.write(" Withdraw")
m.setCursorPos(2,3)
m.setBackgroundColor(colors.gray)
m.write(" ")
m.setBackgroundColor(colors.white)
m.write(" Check Balance")
print(cardnumber)
basicpage()
while true do
e, p1, p2, p3, p4 ,p5 = os.pullEvent()
if e == "monitor_touch" and p2 == 18 and p3 == 1 then
os.reboot()
elseif e== "monitor_touch" and p2 == 2 and p3 == 2 then
-- withdraw
back()
basicpage()
m.setBackgroundColor(colors.white)
m.setCursorPos(6,1)
m.write("Max:$100")
m.setBackgroundColor(colors.white)
m.setCursorPos(2,2)
m.write("Withdraw Amount:")
m.setBackgroundColor(colors.lime)
m.setCursorPos(8,4)
m.write("DONE")
withtrue = true
witham = 0
while withtrue do
  m.setBackgroundColor(colors.white)
  m.setCursorPos(8,3)
  m.clearLine()
  m.write("$"..witham)
  m.setCursorPos(1,3)
  m.setBackgroundColor(colors.red)
  m.write("-10")
  m.setCursorPos(5,3)
  m.setBackgroundColor(colors.red)
  m.write("-")
  m.setCursorPos(14,3)
  m.setBackgroundColor(colors.green)
  m.write("+")
  m.setCursorPos(16,3)
  m.setBackgroundColor(colors.green)
  m.write("+10")
  e,p1,p2,p3,p4,p5 = os.pullEvent()
  if e == "monitor_touch" and p2 == 5 and p3 == 3 then
   print("-")
   if witham <= 0 then
   else
    witham = witham-1
   end
  elseif e == "monitor_touch" and p2 == 14 and p3 == 3 then
   print("+")
   if witham >= 100 then
   else
    witham = witham+1
   end
  elseif e == "monitor_touch" and p2 >= 16 and p2 <= 18 and p3 == 3 then
   print("+10")
   if witham >= 91 then
   else
    witham = witham+10
   end
  elseif e == "monitor_touch" and p2 >= 1 and p2 <= 3 and p3 == 3 then
   print("-10")
   if witham <= 9 then
   else
    witham = witham-10
   end
  elseif e == "monitor_touch" and p2 >= 18 and p3 == 1 then
   os.reboot()
  elseif e == "monitor_touch" and p2 >= 8 and p2 <= 11 and p3 == 4 then
   --DONE
   sleep(0.5)
   m.clear()
   back()
   basicpage()
   m.setBackgroundColor(colors.white)
   m.setCursorPos(3,2)
   m.write("Withdraw $"..witham.."?")
   m.setCursorPos(4,3)
   m.setBackgroundColor(colors.red)
   m.write("No")
   m.setCursorPos(13,3)
   m.setBackgroundColor(colors.green)
   m.write("Yes")
   e,p1,p2,p3,p4,p5 = os.pullEvent()
   if e == "monitor_touch" and p2 >= 4 and p2 <= 5 and p3 == 3 then
    os.reboot()
   elseif e == "monitor_touch" and p2 >= 13 and p2 <= 15 and p3 == 3 then
    --ACTUAL WITHDRAWL
    wamount = witham
    withdraw()
   
   elseif e == "monitor_touch" and p2 >= 18 and p3 == 1 then
    os.reboot()
   end
  end
end
elseif e== "monitor_touch" and p2 == 2 and p3 == 3 then
-- Check Balance
balance()
print(accbal)
back()
basicpage()
m.setBackgroundColor(colors.white)
m.setCursorPos(2,2)
m.write("Current Balance:")
x = string.len(straccbal)
x = x/2-1
x = 9-x
m.setCursorPos(x,3)
m.write("$"..straccbal)
end
sleep(0.01)
end
end
sleep(0.1)
end

BANK SERVER CODE
Spoiler

local modem = peripheral.wrap("left")
modem.open(1000)
while true do
e,p1,p2,p3,p4,p5 = os.pullEvent()
if e == "modem_message" and p3 == 1000 then
--check balance
print("===================================================")
print("Account: "..p4)
file = fs.open(p4,"r")
bal = file.readAll()
modem = peripheral.wrap("left")
sleep(0.1)
modem.transmit(1000, 1000, bal)
print("Balance: "..bal)
elseif e == "modem_message" and p3 == 1001 then
--withdrawl
print("===================================================")
print("Account: "..string.sub(p4,1,3))
file = fs.open(string.sub(p4,1,3),"r")
bal = file.readAll()
print("Old Balance: "..bal)
print("Withdrawl Amount: "..string.sub(p4,4))
newbal =  tonumber(bal)-tonumber(string.sub(p4,4))
file.close()
if newbal >= 0 then
print("New Balance: "..newbal)
file = fs.open(string.sub(p4,1,3),"w")
file.write(newbal)
file.close()
modem.transmit(1000, 1000, "true")
print("Withdrawl Completed Successfuly")
else
modem.transmit(1000, 1000, "funds")
print("Account had Insufficient Funds!")
end
end
sleep(0.01)
end

Edited on 02 February 2014 - 12:48 PM
CometWolf #2
Posted 02 February 2014 - 01:40 PM
How are we supposed to know, unless you post the program?
Zeoic #3
Posted 02 February 2014 - 01:45 PM
How are we supposed to know, unless you post the program?

I was asking as more of a, "Does any one know if this a bug" type thing, but anyways, I am updating the main post with the code now.
surferpup #4
Posted 02 February 2014 - 03:22 PM
When you say the modem stops working until you break it and replace it, I would like a little more info.

First, do you break and replace both modems – the one on the Bank Server and the one on the ATM?

Second, When you do break and replace the modems, do you restart the Bank Server code? the ATM code? Both?

In looking briefly at your ATM code, I noticed the order of the following lines:

modem.transmit(1000,1000,acc)
sleep(0.01)
modem.open(1000)

Try opening the modem first, then transmitting. Get rid of the sleep all together.

Usually these problems are related to timing. A computer transmits something that the other computer misses because it wasn't in a state where it could receive the event (maybe it was sleeping). I haven't worked through your code deep enough to know if that is going on here. If I get time, I will play some more with this.

I do have a suggestion regarding your code practice which may make your programs easier to code and troubleshoot. Try getting used to writing your code in a more ordered fashion:
  • global and local variables
  • global and local functions
  • main program
In the current code, you have everything somewhat intermixed. Try and avoid that, especially as you take on more complicated programming structures.
Bomb Bloke #5
Posted 02 February 2014 - 07:14 PM
In addition to the answers surferpup asked for, I'd like to know exactly what you mean by "gets stuck"? On what line? Insert extra print statements if you're not sure.

Does it have trouble using just the "modem" calls, or does it also get stuck using the monitor (attached through the modem)?

Are you running SSP or SMP? Are these systems and their attached peripherals chunk loaded?