Avisaro Online Documentation - Wiki
Impressum Kontakt Sitemap

Working with TCP connections

This chapter describes how to open and manage a TCP connection using the BASIC scripting. For example, an external sensor only sends data but can't be modified to send commands to handle a connection. 
 
See (
Mehr here) to get details on how open and manage a TCP connection unsing commands send from an external device such as a micro controller. 

TCP/IP fundamentals

This document assumes you are familiar with TCP/IP fundamentals. For further information on TCP/IP please search the internet or follow the external links:

Open TCP connections

A TCP connection can be established two ways:

1.) Waiting for a incoming connection (Avisaro is TCP server)
Use the command "LISTEN" (Mehr more) to create a 'TCP socket' in listen mode. Specify an internal 'handle' number - this handle number adresses this TCP socket. Also specify a TCP port number.

2.) Connection to a server (Avisaro is TCP client)
Use the text command "CONNECT" (Mehr more) to actively connect to a TCP server. Specify an internal 'handle' number, the other TCP adress and port number.

 

Once the TCP socket with its handle number is declared, you can query the module whether the TCP connection was established successfully:

Control TCP connections

Using the "STATUS" command (Mehr more) it can be verified whether or not a connection was established. A return value of '9' for example means a successfully established connection.

 

Sending and receiving data

Once the connection is established, data can be exchanged using the "PUT" (Mehr more) and "GET" (Mehr more) command. One TCP packet can hold 1452 bytes of data. Ideally, an array with this length should be used for sending and receiving data.

Sending data with PUT

The PUT command is used to send data. The handle number specifies which connection to use since more than one TCP connection can be handled. As data to send, a string variable or an array can be given as a parameter. Remember to check the dummy variable 'LASTERR' after sending since transmitting data can fail. See command describtion for details (Mehr more).

Receiving data with GET

The GET command is used to receive data. It is advised to define an array with 1500 Bytes as the receiving array - otherwise a received tcp/ip packet does not fit. See command details (Mehr more).

Closing TCP/IP connection

When done with sending or receiving data, the connection can be closed. Use the "CLOSE" (Mehr more) command to perform this function.

Monitoring TCP/IP connection

While receiving and transmitting data, the TCP/IP connection can be interrupted. Reasons could be the loss of wireless conenction or the other party was switched of or terminated. The command "STATUS" (Mehr more) allows to monitor the connection and reaction properly to a change in connection status.

Example: HTTP Get to Google

Even embedded systems can connect to a server in the Internet. This example shows how to connect to google using Scripting: 

' HTTP Get Beispiel 
' 2008/08/12
' Avisaro AG
' Enter Port and target here
let p = 80
let t$ = "www.google.de"
outmode -2
let g = 0
dim b(1500)
print "reading "; t$ ; 
print " on port " ;
print p
START:
let g = resolv(t$)
sleep 1000
connect 101, g, p, 5
sleep 1000
let y = status(101)
if y = 9 then
   ' connected
   print "connected"
   let a$ = "GET /search?hl=de"
   let a$ = a$ + "&q=avisaro"
   let a$ = a$ + "&meta= HTTP/1.1"
   let a$ = a$ + chr$(13) + chr$(10) + chr$(13) + chr$(10)
   put 101, a$
   GET_RESPONCE:
   sleep 100
   get 101, b
   close 101
   if (BYTESREAD > 0) then
      ' Ausgabe der Anwort über RS232
      for n = 0 to (BYTESREAD-1)
         print chr$(b(n));
      next n
   end if
 
else
   print "connection failed"
   ' no connect, do something else
end if
close 101
print "end"
Header information


Sets variables to