Transmitting data, Receiving data – Wavetronix Click 500 (programmable controller) (CLK-500) - Developer Guide User Manual
Page 73
72
CHAPTER 7 • SERIAL PORTS
ClickSerialPortConnect(V1, _485TOP);
ClickSerialPortConnect(V1, _485TBUS);
ClickSerialPortConnect(V2, _232TOP);
ClickSerialPortConnect(V2, _232FRONT);
ClickSerialPortConfigurePhysical( _485TOP, 9600, DATABIT _8,
PARITY_N);
ClickSerialPortConfigurePhysical( _485TBUS, 38400,
DATABIT_8, PARITY_N);
ClickSerialPortConfigureVirtual(V2, 57600, DATABIT_8,
PARITY_N);
Transmitting Data
The ClickSerialPortTransmit function is used to transmit data onto a virtual serial port. In
the following example, the Button Pressed string will be transmitted onto virtual port one.
Strings are null terminated, and since it may be desirable to have a null character inside of
the string of parameters, specifying the number of bytes to be transmitted is required.
ClickSerialPortTransmit(V1, 18, “Button Pressed v1\n”);
Receiving Data
The ClickSerialPortReceive function can be used to retrieve data that has come in on a
virtual serial port. In the example below, up to 29 data bytes will be retrieved from virtual
port 1.
bytesReceived = ClickSerialPortReceive(V1, 29, string);
The return value of the ClickSerialPortReceive function is the actual number of bytes re-
ceived since the last call. The data that is read out of the virtual serial port is put into the
parameter string.
Example code 7.1 retrieves serial data in the main idle loop. To do so, the CLICK_SERI-
AL_LIB_NOUSE_TIMER_LIB directive has to be defined. Otherwise, the normal method
of retrieval is using the timer interrupt scheduled to fire 90 times a second. Using a timer
interrupt is usually preferred in order to prevent dropping data bytes because a receive buf-
fer is full, but is not used in this simple teaching example. When the timer interrupt fires,
the Click 500 library code will automatically retrieve data from the serial ports into a virtual
buffer. Figure 7.1 presents an overview of data flow between the serial library code and the
Click message system.