serSendData (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Firmware version:

1.00 / 1.00.00


serSendData will send the contents of a buffer to the specified serial port.

If the "sof" and "eof" variables are set (different from 0), the frame will be "framed". The "sof" character will be sent followed by the data pointed to by "data" with the length in "size" followed by the "eof" character. If the data pointed to by "data" contains either a "sof" or an "eof" character, the "stuffch" will be output first followed by the "sof"/"eof" character. If "stuffch" is part of the data stream, it will be duplicated. This means that the receiver can detect that a "sof", "eof", or "stuffch" is embedded within the data and therefore able to restore the original data.

 

Input:

port : SINT (0..127) (default 0)

Selects which serial port to use.

 

data : PTR

The address of the buffer that contain the data.

 

size : INT

This is the number of bytes to send from the buffer.

 

sof : SINT

The start of frame character (indicates when a frame begins).

 

eof : SINT

The end of frame character (indicates when a frame ends).

 

stuffch : SINT

The character that will be inserted in the frame if the "sof" or "eof" characters are to be sent in the data stream (provided "stuffch" is part of the data stream, it will then be duplicated).

 

timeout : DINT (default -1)

The number of milliseconds to wait (Only used when hardware handshake is enabled. See serSetHandshake).

0

-

Disable timeout. The function will return immediately.

-1

-

Wait forever. The function will only return when data is send.

Supported from firmware 4.68 / R1.10.00

 

Returns: INT

1

-

Success.

-1

-

The serial port is not open.

-2

 

The serial port is no longer present.

-3

-

Timeout waiting for send.

 

Declaration:

FUNCTION serSendData;
VAR_INPUT
   port    : SINT := 0; // Port number
   data    : PTR;       // Address of the buffer to send
   size    : INT;       // Number of bytes to send from the buffer
   sof     : SINT;      // Start of frame indicator
   eof     : SINT;      // End of frame indicator 
   stuffch : SINT;      // Stuffing character
   timeout : DINT := -1;
END_VAR;

 

 

Example:

INCLUDE rtcu.inc
 
VAR_OUTPUT
   led      : BOOL; | Indicates that a serial frame has been received
END_VAR;
 
PROGRAM test;
 
VAR
   RX       : serFrameReceiver;
   rxbuffer : ARRAY[0..63] of SINT; // Declare a buffer for receiving frames from the serial port
   txbuffer : ARRAY[0..63] of SINT; // Declare a buffer for sending frames to the serial port
   portNum  : SINT := 0;                   // Select which port to use for the communication        
END_VAR;
 
// Open the serial port, set baudrate, parity and number of bits
serOpen(port:=portNum, baud:=9600, bit:=8, parity:=0);
// Enable receiving from the serial port, data will be stored in 'rxbuffer', start of frame is a <CR> and end of frame is a <LF>
RX(port:=portNum, enable:=TRUE, frame:=ADDR(rxbuffer), maxSize:=SIZEOF(rxbuffer), sof:=16#0D, eof:=16#0A);
 
// Send the string <CR>ABC<CR><LF> using a buffer
txbuffer[0]:=16#0D;  // <CR>
txbuffer[1]:=16#41;  // 'A'
txbuffer[2]:=16#42;  // 'B'
txbuffer[3]:=16#43;  // 'C'
txbuffer[4]:=16#0D;  // <CR>
txbuffer[5]:=16#0A;  // <LF>
serSendData(port:=portNum, data:=ADDR(txbuffer), size:=6);
 
// Send a <CR>
serSendChar(port:=portNum, ch:=16#0D);
// Send the string 'Hello world' 
serSendString(port:=portNum, str:="Hello world");
// Send a <CR>
serSendChar(port:=portNum, ch:=16#0D);
// Send a <LF>
serSendChar(port:=portNum, ch:=16#0A);
 
BEGIN
   // Allow the receive functionblock to be updated
   RX();
   // Check if a frame has been received (at least a <CR> and a <LF> has been received)
   IF RX.ready THEN
      // Indicate a frame has been received, data is available in 'rxbuffer', length in 'RX.size'
      led:=NOT led;
      // Here we do something with the frame that has been received
      // ..
      // ..
      // ..
      // Release the buffer for the receiver as we are now finished with the received data
      serFrameReceiveDone(port:=portNum);
   END_IF;
END;
 
END_PROGRAM;