Advertisement
Cremulus

SLOG Ethernet server

Aug 4th, 2021
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. int EtherByte
  2. int i
  3.  
  4. // We really ought to wait until the Ethernet port is active, it takes half a second or so from power-up
  5. // But if we don't wait it still works
  6. repeat
  7. sleep(10)
  8. until getsysvar(svEtherIsActive)
  9.  
  10. // Tell the system to disable the internal debugging... this lets received characters on the debug port get to this code
  11. // If we don't do this characters arriving on the debug serial port are used for "Top Secret" debugging commands
  12. // And you can't talk to the server
  13. setsysvar(svDebug,false)
  14.  
  15. println
  16. println "And now, a working Telnet server..."
  17. println "Try connecting to it with a telnet client and typing characters at it... You can type characters back now too..."
  18. println "You might have to change the 192.168.0.199 thang to suit your network, it's not supporting DHCP yet."
  19. println "You can do that with a setsysvar(svEtherAddr,dqtoint(\"1.2.3.4\")) before the devicesetup command..."
  20. println
  21.  
  22. // Set the required IP address
  23. //setsysvar(svEtherAddr,dqtoint("192.168.0.99"))
  24.  
  25. repeat
  26.  
  27. // Setting up an Ethernet device using devTCPServer makes us a server, waiting for a connection from a client
  28. // When we are a server, we can pretty-much just wait for characters to arrive from a client and reply to them, while checking so see if the other end disconnects.
  29. devicesetup(devETHER1,devTCPServer,23)
  30.  
  31. println
  32. println "I'm waiting on",inttodq(devicestatus(devETHER1,statusSrcAddr)),"port",devicestatus(devETHER1,statusSrcPort),"for a telnet client to connect..." // Note it's SrcAddr and SrcPort for talking to a server.
  33. println
  34.  
  35. // And then read/write them exactly like serial ports while the device is connected
  36. // Notice this check is now easier.
  37. while devicestatus(devETHER1,statusConnected)
  38. {
  39. // Print any characters that arrive on port 80
  40. // We'll read it one byte at a time so we can translate CR/LF to CR, and catch Ctrl-C
  41. while rxwait(devETHER1) // True if not zero, ie, there are characters waiting
  42. {
  43. // Read the input one byte at a time so we can merge LF/CR and detect Ctrl-C
  44. EtherByte = deviceread(devETHER1, u8)
  45. // Examine it and decide what to do...
  46. select EtherByte
  47. {
  48. case $03 devicecmd(devETHER1,cmdClose) // Ctrl-C closes the device
  49. case $0A,$0D println // CR or LF print a new line
  50. default print chr$(EtherByte) // Anything else gets printed
  51. }
  52. }
  53.  
  54. // Send any received characters from the debug port to the TELNET client
  55. // We'll read it one byte at a time so we can translate CR/LF to LF
  56. while rxwait(devDBG) // True if not zero, ie, there are characters waiting
  57. {
  58. // Read the input one byte at a time so we can merge LF/CR and detect Ctrl-C
  59. EtherByte = deviceread(devDBG, u8)
  60. // Examine it and decide what to do...
  61. select EtherByte
  62. {
  63. case $0A,$0D devicewrite(devETHER1, u16 $0A0D) // CR or LF send a CR+LF
  64. default devicewrite(devETHER1, u8 EtherByte) // Anything else gets sent
  65. }
  66. }
  67.  
  68. sleep(10) // Sleep if there's nothing there
  69. }
  70.  
  71. until false // And when it finishes, just open the port again and listen for new clients.
  72.  
  73. // If we didn't need to translate we could transfer strings like this
  74. // while rxwait(devDBG) // True if not zero, ie, there are characters waiting
  75. // devicewrite(devETHER1, str deviceread(devDBG, str -1)) // Read all the waiting characters into a string, and send them as a lump
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement