Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int EtherByte
- int i
- // We really ought to wait until the Ethernet port is active, it takes half a second or so from power-up
- // But if we don't wait it still works
- repeat
- sleep(10)
- until getsysvar(svEtherIsActive)
- // Tell the system to disable the internal debugging... this lets received characters on the debug port get to this code
- // If we don't do this characters arriving on the debug serial port are used for "Top Secret" debugging commands
- // And you can't talk to the server
- setsysvar(svDebug,false)
- println
- println "And now, a working Telnet server..."
- println "Try connecting to it with a telnet client and typing characters at it... You can type characters back now too..."
- println "You might have to change the 192.168.0.199 thang to suit your network, it's not supporting DHCP yet."
- println "You can do that with a setsysvar(svEtherAddr,dqtoint(\"1.2.3.4\")) before the devicesetup command..."
- println
- // Set the required IP address
- //setsysvar(svEtherAddr,dqtoint("192.168.0.99"))
- repeat
- // Setting up an Ethernet device using devTCPServer makes us a server, waiting for a connection from a client
- // 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.
- devicesetup(devETHER1,devTCPServer,23)
- println
- 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.
- println
- // And then read/write them exactly like serial ports while the device is connected
- // Notice this check is now easier.
- while devicestatus(devETHER1,statusConnected)
- {
- // Print any characters that arrive on port 80
- // We'll read it one byte at a time so we can translate CR/LF to CR, and catch Ctrl-C
- while rxwait(devETHER1) // True if not zero, ie, there are characters waiting
- {
- // Read the input one byte at a time so we can merge LF/CR and detect Ctrl-C
- EtherByte = deviceread(devETHER1, u8)
- // Examine it and decide what to do...
- select EtherByte
- {
- case $03 devicecmd(devETHER1,cmdClose) // Ctrl-C closes the device
- case $0A,$0D println // CR or LF print a new line
- default print chr$(EtherByte) // Anything else gets printed
- }
- }
- // Send any received characters from the debug port to the TELNET client
- // We'll read it one byte at a time so we can translate CR/LF to LF
- while rxwait(devDBG) // True if not zero, ie, there are characters waiting
- {
- // Read the input one byte at a time so we can merge LF/CR and detect Ctrl-C
- EtherByte = deviceread(devDBG, u8)
- // Examine it and decide what to do...
- select EtherByte
- {
- case $0A,$0D devicewrite(devETHER1, u16 $0A0D) // CR or LF send a CR+LF
- default devicewrite(devETHER1, u8 EtherByte) // Anything else gets sent
- }
- }
- sleep(10) // Sleep if there's nothing there
- }
- until false // And when it finishes, just open the port again and listen for new clients.
- // If we didn't need to translate we could transfer strings like this
- // while rxwait(devDBG) // True if not zero, ie, there are characters waiting
- // devicewrite(devETHER1, str deviceread(devDBG, str -1)) // Read all the waiting characters into a string, and send them as a lump
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement