Tech Note 16 CommunicationsMay 01, 2008Copyright 2008 NS BASIC Corporation |
If you have notes or tips to add, please send them to support@nsbasic.com.
1. Serial Communications - RS-232
2. IR Communications
2.A SIR
2.B IRCOMM
2.C Consumer IR
3. TCP/IP
4. Bluetooth
Serial communication over an RS-232 connection is often the easiest way to talk to other devices. Until recently, it was the standard used by almost all devices. You'll need to check whether the device you are using supports serial communication.
The best way to test serial communications is hook your device up to the serial port on your PC. Run HyperTerminal on the PC and set it to 9600 baud, local connection. On your device, run the SerTester sample. You'll be able to send and receive data in both directions.
Have a look at the SerialOpen function in your Handbook or in the Help to get the proper port numbers for opening serial communications. In most cases, you will use 0 as the port number.
If you want more detailed "how to" information on serial communications, see NS Basic Programming for Palm OS, by Michael Verive. It's very similar.
Here are some sources for cables:
b. SerialReceiveWithEvents
This useful feature allows an NS Basic program to respond to an incoming message with an event, rather than having to poll the serial port continuously to see if data comes through.
A. Simple IR (SIR)
To open an IR port for serial communications,
Remember to do the the SerialSet("IR", mode) before sending and receiving. Prior to OS 5, it was possible to both send and receive in mode 1. The new release tightens this up: you need to be in the proper mode to do SerialSend or SerialRecieve.
Have a look at the IRTest sample that comes with NS Basic to see how this works.
Here is another way of doing it:
B. IRComm (IRDA)
To open an IR port for IRComm communications,
IRComm is easy to use. It's not as fast as SIR, but much more reliable and runs on all devices. There is no need to do SerialSet("IR", mode) before sending and receiving. The port number (1769104237) is simply the 4 byte string 'ircm' expressed as an integer. Baud rate (9600) does not seem to matter: the protocol works out the best rate.
For sample code, simply take the SerTester sample and change the port number.
C. Consumer Electronics
Consumer electronics RF sends pulses at a different frequency than data communications. As a result, many IR chips cannot transmit or recieve consumer IR data.
TCP/IP is the communications protocol used by the internet. All Symbian devices have some capability to use TCP/IP. It's a very flexible protocol, but is more trouble to configure.
First, a TCP/IP connection must be established between the device and the system you want to communicate with. From a hardware perspective, this requires a WiFi card (or perhaps a device with built in WiFi) or a USB or serial cable. Next, you need to configure the Network settings in Prefs. The exact settings will depend on the physical connection, then tap Connect.
NS Basic communicates with TCP/IP using the TcpIpLib. This is described in Tech Note 18. Once the connection is established, most standard internet protocols can be used. NS Basic includes the sample TcpClient and TcpServer samples which exchanges messages with a Visual Basic program on the desktop.
To open Bluetooth virtual port for serial communications,
Bluetooth is also easy to use. It's not as fast as SIR, but much more reliable and runs on all devices. The port number (1919312749) is simply the 4 byte string 'rfcm' expressed as an integer. Baud rate (9600) does not seem to matter. Device discovery is done when the port is opened by prompts to the operator.
For sample code, simply take the SerTester sample and change the port number. You can test by using HyperTerminal on the desktop and setting up a serial connection. The com port for Hyperterminal will be in the range of 2-6, depending on your system. Baud rate and other connection settings can be ignored.
res=SerialOpen(32769,9600)
res=SerialSet("IR",1) 'to receive data
res=SerialSet("IR",2) 'to send data
'iError = Serialset("IR", 1)
SerialOpen(1769104237, 9600)
res=SerialOpen(1769104237,9600)
3. TCP/IP
4. Bluetooth
res=SerialOpen(1919312749,9600)