Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- str EtherIn
- int EtherByte
- // 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, though the debugging reports are untidy on the first attempt.
- 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)
- // Now, the actual client example
- repeat
- // The Ethernet device has powered up
- // Setting up an Ethernet device using devTCPClient makes us look for a server
- // This tries to open a remote port as a client - NOTE! The order of these has changed
- devicesetup(devETHER1,devTCPClient,dqtoint("192.168.0.60"),8080) // The server's IP address and port
- // Tell the user what we're trying to do
- println "I'm trying to connect to a server at",inttodq(devicestatus(devETHER1,statusDestAddr)),"port",devicestatus(devETHER1,statusDestPort)
- println "From ",inttodq(devicestatus(devETHER1,statusSrcAddr)),"port",devicestatus(devETHER1,statusSrcPort)
- println
- println "Started connecting"
- println
- // Wait while it's connecting - this is true ONLY while it's looking for the other end.
- // devicestatus(devETHER1,statusConnecting) stops being true when we connect or give up
- while devicestatus(devETHER1,statusConnecting)
- sleep(10)
- println "Stopped connecting"
- println
- // Are now active?
- // devicestatus(devETHER1,statusReady) is true while the port is talking and we can read/write data
- if devicestatus(devETHER1,statusReady)
- {
- println "Became Active"
- // Now loop while we remain active.
- while devicestatus(devETHER1,statusReady)
- {
- // Transfer any characters that have arrived from the server to the debug port
- while rxwait(devETHER1) > 0
- {
- EtherIn = deviceread(devETHER1, str -1)
- devicewrite(devDBG,str EtherIn)
- }
- // Transfer characters from the debug port to the server
- // 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
- while rxwait(devDBG) > 0
- {
- EtherByte = deviceread(devDBG, u8)
- // Showoff the swanky select statement
- select EtherByte
- {
- case $03 devicecmd(devETHER1,cmdClose) // Ctrl-C closes the port
- case $0D,$0A devicewrite(devETHER1,u8 $0A) // Either CR or LF send an LF
- default devicewrite(devETHER1,u8 EtherByte) // Anything else gets sent
- }
- }
- sleep(20) // Sleep when there's nothing better to do
- }
- // We've fallen out of the active loop
- println "\r\nStopped being ready"
- }
- // Transaction over, but just go back and try again
- until false // And when it finishes, just try again...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement