func main() global IP as string global Port as integer global SlotConsist(120) as string global SlotSpeedStep(120) as string global SlotStatus(120) as string global SlotAddress(120) as integer global SlotSpeed(120) as integer global SlotDir(120) as string global SlotFunction(120) as string global SlotSnd(120) as string global SlotID1(120) as integer global SlotID2(120) as integer global Index as integer dim Connected, Consist as integer dim i, j as integer Connected = 0 index = 0 print "Loconet Slot Monitor V0.1" print "(c) Michael Mosher 2007" print " " print "Requires Loconet over TCP server" print " " if FileInfo(".\IP.dat",2) = "True" then IP = FileQuickLoad(".\IP.dat") print "load IP " + IP else IP = "127.0.0.1" print "defualt IP " + IP endif if FileInfo(".\Port.dat",2) = "True" then Port = FileQuickLoad(".\Port.dat") print "load Port " + Port else Port = 1234 print "defualt Port " + Port endif ConnectForm() Console(4) LoopMain: if FormAllButtons() = 1 then if FormButton(Button_Quit,0) > 0 then '------ Quit Handler ----- End endif if Connected = 0 then if FormButton(Button_Save,0) > 0 then '------ Save Handler ----- IP = FormTextBox(Text_IP) Port = FormTextBox(Text_Port) FileQuickSave(".\IP.dat", IP) FileQuickSave(".\Port.dat", Port) endif if FormButton(Button_Connect,0) > 0 then '------ Connect Handler ----- IP = FormTextBox(Text_IP) Port = FormTextBox(Text_Port) if ConnectTCP() = 0 then DisplayForm() Connected = 1 endif endif else if FormButton(Button_Up,0) > 0 then '------ Up Handler ----- Index = Index - 20 If Index < 0 then Index = 0 UpdateForm() endif if FormButton(Button_Down,0) > 0 then '------ Down Handler ----- Index = Index + 20 If Index > 100 then Index = 100 UpdateForm() endif if FormButton(Button_Refresh,0) > 0 then '------ Refresh Handler ----- Refresh() UpdateForm() endif endif ' connected endif ' form button pressed if Connected = 1 then ReadLoconet(0) endif 'connected Goto LoopMain endfunc '------------------------------------------------------------------------------- func ReadLoconet(Slot as integer) dim i, j, k, Status as integer dim done = 0 as integer dim Byte(20) as integer dim Line as string dim Buffer as string dim Token as string dim ByteString as string Token = chr(13)+chr(10) TimerClear(1) ReadLoconetLoop1: if SocketBuffer(1) > 0 then Buffer = SocketInput(1) k = 0 ' print Buffer ReadLoconetLoop2: Line = GetWord(Buffer, 1, k+1, Token, Status) if Status <> -1 then if BeginsWith(Line, "RECEIVE") = 1 then i = 0 ReadLoconetLoop3: ByteString = GetWord(Line, 8, i+1, " ", Status) if Status <> -1 then Byte(i) = HexToDec(ByteString) i = i + 1 goto ReadLoconetLoop3 endif if Byte(0) = $A0 then ' speed if Byte(1) < 120 then SlotSpeed(Byte(1)) = Byte(2) UpdateForm() endif 'slot < 120 endif if Byte(0) = $A1 then ' DirF if Byte(1) < 120 then DecodeDirF(Byte(1),Byte(2)) UpdateForm() endif 'slot < 120 endif if Byte(0) = $A2 then ' Sound func if Byte(1) < 120 then DecodeSnd(Byte(1), Byte(2)) UpdateForm() endif 'slot < 120 endif if Byte(0) = $B5 then ' Stat if Byte(1) < 120 then DecodeStat(Byte(1), Byte(2)) UpdateForm() endif 'slot < 120 endif if Byte(0) = $B6 then 'dir F if Byte(1) < 120 then DecodeDirF(Byte(1),Byte(2)) UpdateForm() endif 'slot < 120 endif if Byte(0) = $E7 then ' slot info if Byte(2) < 120 then SlotAddress(Byte(2)) = Byte(9) * 128 + Byte(4) SlotSpeed(Byte(2)) = Byte(5) SlotID1(Byte(2)) = Byte(11) SlotID2(Byte(2)) = Byte(12) DecodeDirF(Byte(2), Byte(6) ) DecodeSnd (Byte(2), Byte(10)) DecodeStat(Byte(2), Byte(3) ) ' print "St "+Byte(2)+" Add "+SlotAddress(Byte(2))+" Sp "+SlotSpeed(Byte(2))+" Dir "+SlotDir(Byte(2))+" Fn "+SlotFunction(Byte(2))+","+SlotSnd(Byte(2)) ' print "SS "+SlotSpeedStep(Byte(2))+" St "+SlotStatus(Byte(2))+" Con "+SlotConsist(Byte(2))+" ID "+SlotID1(Byte(2))+"/"+SlotID2(Byte(2)) if Slot = Byte(2) then Done = 1 UpdateForm() endif 'slot < 120 endif 'E7 endif 'RECEIVE k = k + 1 goto ReadLoconetLoop2 endif 'Get word endif 'socket data if Slot = 0 then Exit() if Done = 1 then Exit() if Timer(1) > 5000 then Exit() goto ReadLoconetLoop1 endfunc '------------------------------------------------------------------------------- func DecodeDirF(Slot as integer, Data as integer) if BitGet(Data,4) > 0 then SlotFunction(Slot) = "0" else SlotFunction(Slot) = "-" endif if BitGet(Data,0) > 0 then SlotFunction(Slot) = SlotFunction(Slot) + "1" else SlotFunction(Slot) = SlotFunction(Slot) + "-" endif if BitGet(Data,1) > 0 then SlotFunction(Slot) = SlotFunction(Slot) + "2" else SlotFunction(Slot) = SlotFunction(Slot) + "-" endif if BitGet(Data,2) > 0 then SlotFunction(Slot) = SlotFunction(Slot) + "3" else SlotFunction(Slot) = SlotFunction(Slot) + "-" endif if BitGet(Data,3) > 0 then SlotFunction(Slot) = SlotFunction(Slot) + "4" else SlotFunction(Slot) = SlotFunction(Slot) + "-" endif if BitGet(Data,5) > 0 then SlotDir(Slot) = "R" else SlotDir(Slot) = "F" endif endfunc '------------------------------------------------------------------------------- func DecodeSnd(Slot as integer, Data as integer) if BitGet(Data,0) > 0 then SlotSnd(Slot) = "5" else SlotSnd(Slot) = "-" endif if BitGet(Data,1) > 0 then SlotSnd(Slot) = SlotSnd(Slot) + "6" else SlotSnd(Slot) = SlotSnd(Slot) + "-" endif if BitGet(Data,2) > 0 then SlotSnd(Slot) = SlotSnd(Slot) + "7" else SlotSnd(Slot) = SlotSnd(Slot) + "-" endif if BitGet(Data,3) > 0 then SlotSnd(Slot) = SlotSnd(Slot) + "8" else SlotSnd(Slot) = SlotSnd(Slot) + "-" endif endfunc '------------------------------------------------------------------------------- func DecodeStat(Slot as integer, Data as integer) dim Consist as integer Consist = (BitGet(Data,6) * 2) + BitGet(Data,3) if Consist = 0 then SlotConsist(Slot) = "Free" if Consist = 1 then SlotConsist(Slot) = "Sub " if Consist = 2 then SlotConsist(Slot) = "Top " if Consist = 3 then SlotConsist(Slot) = "Mid " SlotSpeedStep(Slot) = "0x" + convert((Data & $07),"X") ' if other than regular cases if (Data & $07) = 0 then SlotSpeedStep(Slot) = " 28 " if (Data & $07) = 1 then SlotSpeedStep(Slot) = "Tri " if (Data & $07) = 2 then SlotSpeedStep(Slot) = " 14 " if (Data & $07) = 3 then SlotSpeedStep(Slot) = "128 " if (Data & $07) = 4 then SlotSpeedStep(Slot) = " 28*" if (Data & $07) = 7 then SlotSpeedStep(Slot) = "128*" if (Data & $30) = $00 then SlotStatus(Slot) = "Free" if (Data & $30) = $10 then SlotStatus(Slot) = "Com " if (Data & $30) = $20 then SlotStatus(Slot) = "Idle" if (Data & $30) = $30 then SlotStatus(Slot) = "Use " endfunc '------------------------------------------------------------------------------- func Refresh() dim B(4) as integer dim i, j as integer dim Output as string for i = 1 to 119 FormLabel(Label_Refresh,-1,-1,-1,-1,i) B(0) = $BB B(1) = i B(2) = 0 B(3) = 0 FOR j = 0 TO 2 B(3) = B(3) ^ B(j) NEXT B(3) = B(3) ^ 255 Output = "SEND " + Convert(B(0),"X")+" "+Convert(B(1),"X")+" "+Convert(B(2),"X")+" "+Convert(B(3),"X")+chr(13)+chr(10) SocketOutput(1,Output) ReadLoconet(i) if FormButton(Button_Refresh,0) > 0 then FormLabel(Label_Refresh,-1,-1,-1,-1," ") Exit() endif next FormLabel(Label_Refresh,-1,-1,-1,-1," ") endfunc '------------------------------------------------------------------------------- func ConnectTCP() TimerClear(1) SocketConnect(1,IP,Port) WaitLoop: if SocketState(1) = 4 then print "Connected" exit(0) else print "Waiting.... "+SocketData(1,6)+" / "+SocketData(1,5) endif pause(100) if Timer(1) > 30000 then MsgBox("Can't Connect", 0) exit(1) endif Goto WaitLoop endfunc '------------------------------------------------------------------------------- func HexToDec(Hex as string) as integer dim LeftChar as string dim RightChar as string LeftChar = Left(Hex, 1) RightChar = Right(Hex, 1) exit(DecDigit(LeftChar) * 16 + DecDigit(RightChar)) endfunc '------------------------------------------------------------------------------- func DecDigit(Digit as string) as integer StrIf Digit = "0" then exit (0) StrIf Digit = "1" then exit (1) StrIf Digit = "2" then exit (2) StrIf Digit = "3" then exit (3) StrIf Digit = "4" then exit (4) StrIf Digit = "5" then exit (5) StrIf Digit = "6" then exit (6) StrIf Digit = "7" then exit (7) StrIf Digit = "8" then exit (8) StrIf Digit = "9" then exit (9) StrIf Digit = "A" then exit (10) StrIf Digit = "B" then exit (11) StrIf Digit = "C" then exit (12) StrIf Digit = "D" then exit (13) StrIf Digit = "E" then exit (14) StrIf Digit = "F" then exit (15) endfunc '------------------------------------------------------------------------------- func UpdateForm() dim i, j as integer FormSettings(AlignRight) for i = 0 to 19 j = i + Index FormLabel ( 20+i, -1, -1, -1, -1, j) FormLabel ( 40+i, -1, -1, -1, -1, SlotAddress(j)) FormLabel ( 60+i, -1, -1, -1, -1, SlotDir(j)) FormLabel ( 80+i, -1, -1, -1, -1, SlotSpeed(j)) FormLabel (100+i, -1, -1, -1, -1, SlotFunction(j)) FormLabel (120+i, -1, -1, -1, -1, SlotSnd(j)) FormLabel (140+i, -1, -1, -1, -1, SlotSpeedStep(j)) FormLabel (160+i, -1, -1, -1, -1, SlotStatus(j)) FormLabel (180+i, -1, -1, -1, -1, SlotConsist(j)) next endfunc '------------------------------------------------------------------------------- func DisplayForm() dim i, y as integer ' FormRes(240,268) FormNew() FormBGColor(228,228,228) FormCLS() FormTextColor(0,0,0) FormFont("-",10,1) FormSettings(AlignCenter) FormLabel(Label_Heading,0,0,240,20,"Loconet Slot Monitor") FormLabel(Label_Refresh,120,20,20,20," ") FormSettings(AlignRight) FormFont("-",8,0) for i = 0 to 19 y = i * 10 + 40 FormBGColor(255,255,255) FormLabel (20+i, 0, y, 22, 10,i) FormBGColor(255,255,0) FormLabel (40+i, 22, y, 35, 10,SlotAddress(i)) FormBGColor(255,255,255) FormLabel (60+i, 57, y, 10, 10,SlotDir(i)) FormBGColor(255,255,0) FormLabel (80+i, 67, y, 22, 10,SlotSpeed(i)) FormBGColor(255,255,255) FormLabel (100+i, 89, y, 35, 10,SlotFunction(i)) FormBGColor(255,255,0) FormLabel (120+i, 124, y, 29, 10,SlotSnd(i)) FormBGColor(255,255,255) FormLabel (140+i, 153, y, 29, 10,SlotSpeedStep(i)) FormBGColor(255,255,0) FormLabel (160+i, 182, y, 29, 10,SlotStatus(i)) FormBGColor(255,255,255) FormLabel (180+i, 211, y, 29, 10,SlotConsist(i)) next FormBGColor(211,211,211) FormFont("-",9,1) FormButton(Button_Refresh, 160, 20, 80, 20, "Refresh") FormButton(Button_Up, 0, 20, 40, 20, "Up") FormButton(Button_Down, 0, 245, 40, 20, "Down") FormButton(Button_Quit, 180, 245, 60, 20, "Quit") FormSettings(Alignleft) FormBGColor(228,228,228) endfunc '------------------------------------------------------------------------------- func ConnectForm() '--- Buttons --- gconst Button_Quit 0 gconst Button_Connect 1 gconst Button_Save 2 gconst Button_Up 3 gconst Button_Down 4 gconst Button_Refresh 5 '--- Labels --- gconst Label_Heading 0 gconst Label_IP 2 gconst Label_Port 3 gconst Label_Refresh 4 ' Slot 20-39 ' Add 40-59 ' Dir 60-79 ' Speed 80-99 ' Function 100-119 ' Sound 120-139 ' SpeedStep 140-159 ' Status 160-179 ' Consist 180-199 '--- TextBoxes --- gconst Text_IP 1 gconst Text_Port 2 ' FormRes(240,268) FormNew() FormBGColor(228,228,228) FormCLS() FormTextColor(0,0,0) FormFont("-",10,1) FormSettings(AlignCenter) FormLabel(Label_Heading,0,0,240,20,"Loconet over TCP connection") FormFont("-",9,0) FormSettings(AlignRight) FormLabel(Label_IP, 0, 20,60,20,"IP:") FormLabel(Label_Port, 0, 40,60,20,"Port:") FormBGColor(255,255,255) FormSettings(AlignLeft) FormTextBox(Text_IP, 65, 20,100,20,IP) FormTextBox(Text_Port, 65, 40,100,20,Port) FormSettings(Alignleft) FormBGColor(211,211,211) FormFont("-",9,1) FormButton(Button_Quit, 10,245,60,20,"Quit") FormButton(Button_Connect, 80,245,70,20,"Connect") FormButton(Button_Save, 160,245,70,20,"Save") endfunc