Writing Code

Below is your first project in its entirety. You can just copy/paste it into the main.tbs file.

The only line that will need your attention is the line that starts with net.ip. You have to assign the device a suitable IP address, which must be "compatible" with your network segment. In practical terms, this means that the first three numbers of the IP address match your PC's IP address, while the fourth is different. For example, if your PC's IP address is 192.168.1.40, you can try setting your Tibbo device's IP address to 192.168.1.41. Of course, you should first make sure that this IP address is not in use; try pinging it — if there's no response, it is likely free.

You might be wondering about DHCP: Isn't it a safe and convenient way to assign a valid and unoccupied IP address? Isn't it normal — if not outright common — for a network device to procure an IP address while booting?

Tibbo devices are different, as they boot with a default IP address of 1.0.0.1. Your program can directly configure this IP address through the net.ip property. Should your application need to use DHCP, we offer a DHCP library that is easily integrated into your projects.

Note that a correct IP address is not required for cross-debugging to work. Even if you leave the default IP address of 1.0.0.1 unchanged, TIDE will be able to access your Tibbo device. However, if you are using the TiOS Simulator, you must choose a suitable IP address independent of the host PC's IP address.

** Tibbo Basic **

include "global.tbh"

sub on_sys_init()
   net.ip="192.168.1.222" '<—- set the correct IP

   sock.rxbuffrq(1)
   sock.txbuffrq(1)
   sys.buffalloc

   sock.protocol=PL_SOCK_PROTOCOL_TCP
   sock.localportlist="1000"
   sock.inconmode=PL_SOCK_INCONMODE_ANY_IP_ANY_PORT
   sock.reconmode=PL_SOCK_RECONMODE_3
end sub

sub on_sock_data_arrival()
   dim s as string

   s=sock.getdata(sock.txfree)
   sock.setdata(s)
   sock.send
end sub

sub on_sock_event(newstate as enum pl_sock_state,newstatesimple as enum pl_sock_state_simple)
   if newstatesimple=PL_SSTS_EST then
      sock.setdata("HELLO WORLD!")
      sock.send
   end if
end sub

Notice how small your first Tibbo BASIC program is! This is the power of Tibbo BASIC (and C) — you can accomplish complex things in very few lines of code.

A few comments on this code:

1.The on_sys_init event handler prepares a single listening TCP socket.

2.The on_sock_data_arrival event handler takes whatever is received by the TCP socket and sends it back.

3.The on_sock_event event handler is unnecessary for the echo application; it was added because we wanted to have the proverbial "HELLO WORLD!" somewhere. The string is sent whenever a TCP connection is established.