Writing Code

Top  Previous  Next

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 net.ip=" " line. You have to choose a suitable IP address for your device. (more)!

The address your choose must be "compatible" with your network segment. In practical terms this means that the first three numbers of the IP must match those of your PC's IP. The fourth number will necessarily be different.

For example, if your PC's IP is 192.168.1.40 you can try setting your Tibbo device's IP to 192.168.1.41. Of course, you need to make sure that this IP is unoccupied. Try pinging it — if there is no response then this IP must be free.

At this point you are probably wondering about the DHCP. Isn't this the safe and convenient way to assign a valid and unoccupied IP address? Isn't it normal (common) for a network device to procure an IP while booting?

Tibbo devices are different! They boot with the default IP of 1.0.0.1. Your program can directly manipulate this IP — through the .ip property of the net. object. Should your application need to use the DHCP, we offer a DHCP library. You can easily integrate it into your next project.

Note 1: The correct IP is not required for cross-debugging to work. Even if you leave the default IP of 1.0.0.1 unchanged TIDE will be able to access your Tibbo device!

Note 2: You have to choose a suitable IP address even if you are using TiOS Simulator. The IP address of the Simulator is independent from the IP address of the PC it is running on.

 

Few code comments:

The sys_on_sys_init event handler prepares a single listening TCP socket.

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

The sock_on_sock_event event handler is not actually necessary for the echo application. We added this because we wanted to have the proverbial "HELLO WORLD" somewhere. The string is sent whenever a TCP connection is established.

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

 

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