Advertisement
Cremulus

SLOG Ethernet client

Aug 4th, 2021
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. str EtherIn
  2. int EtherByte
  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, though the debugging reports are untidy on the first attempt.
  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. // Now, the actual client example
  16. repeat
  17.  
  18. // The Ethernet device has powered up
  19.  
  20. // Setting up an Ethernet device using devTCPClient makes us look for a server
  21. // This tries to open a remote port as a client - NOTE! The order of these has changed
  22. devicesetup(devETHER1,devTCPClient,dqtoint("192.168.0.60"),8080) // The server's IP address and port
  23.  
  24. // Tell the user what we're trying to do
  25. println "I'm trying to connect to a server at",inttodq(devicestatus(devETHER1,statusDestAddr)),"port",devicestatus(devETHER1,statusDestPort)
  26. println "From ",inttodq(devicestatus(devETHER1,statusSrcAddr)),"port",devicestatus(devETHER1,statusSrcPort)
  27. println
  28.  
  29. println "Started connecting"
  30. println
  31.  
  32. // Wait while it's connecting - this is true ONLY while it's looking for the other end.
  33. // devicestatus(devETHER1,statusConnecting) stops being true when we connect or give up
  34. while devicestatus(devETHER1,statusConnecting)
  35. sleep(10)
  36.  
  37. println "Stopped connecting"
  38. println
  39.  
  40. // Are now active?
  41. // devicestatus(devETHER1,statusReady) is true while the port is talking and we can read/write data
  42. if devicestatus(devETHER1,statusReady)
  43. {
  44. println "Became Active"
  45.  
  46. // Now loop while we remain active.
  47. while devicestatus(devETHER1,statusReady)
  48. {
  49. // Transfer any characters that have arrived from the server to the debug port
  50. while rxwait(devETHER1) > 0
  51. {
  52. EtherIn = deviceread(devETHER1, str -1)
  53. devicewrite(devDBG,str EtherIn)
  54. }
  55.  
  56. // Transfer characters from the debug port to the server
  57. // Now, we could just read them as a string, but we use bytes as we're going to detect CR and convert it to LF, and detect Ctrl-C
  58. while rxwait(devDBG) > 0
  59. {
  60. EtherByte = deviceread(devDBG, u8)
  61.  
  62. // Showoff the swanky select statement
  63. select EtherByte
  64. {
  65. case $03 devicecmd(devETHER1,cmdClose) // Ctrl-C closes the port
  66. case $0D,$0A devicewrite(devETHER1,u8 $0A) // Either CR or LF send an LF
  67. default devicewrite(devETHER1,u8 EtherByte) // Anything else gets sent
  68. }
  69. }
  70.  
  71. sleep(20) // Sleep when there's nothing better to do
  72. }
  73.  
  74. // We've fallen out of the active loop
  75. println "\r\nStopped being ready"
  76. }
  77. // Transaction over, but just go back and try again
  78. until false // And when it finishes, just try again...
  79.  
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement