serSendChar (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

All

Firmware version:

1.00 / 1.00.00


serSendChar will send a single character on the serial port.

It is not recommended to use this function in RS485 mode due to the incurred overhead.

 

Input:

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

Selects which serial port to use.

 

ch : SINT

The character to send.

 

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.

-3

-

Timeout waiting for send.

 

Declaration:

FUNCTION serSendChar : INT;
VAR_INPUT
  port   : SINT := 0;   // Port number
  ch     : SINT;         // Character to send
  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;