serOpen (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

All

Firmware version:

1.00 / 1.00.00


serOpen will open the serial port for direct access in either RS232 or RS485 mode depending on the capabilities of the port.

Please note that a special cable may be required (see ser: Serial port).

 

Input:

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

Selects which serial port to use. 0 is the programming port on non-USB devices, 1 is serial port 2, 2 is serial port 3, 3 is serial port 4.

Not all serial ports are available on all devices. Please consult the technical manual of the specific device for more information.

On the NX32L, the port numbers from usbHostGetSerialPort, btSerialPortProfileConnect and btHandleSerialPortIncomingConnection can also be used.

 

baud : DINT (1200,2400,4800,9600,19200,38400,57600,115200) (default 9600)

The desired baud rate.

 

bit : SINT (7/8) (default 8)

Selects the number of bits/character

 
Note:

7 bit only works when parity is used (even or odd).

 

parity : SINT (0,1,2) (default 0)

Selects the desired parity. 0 is none, 1 is even, and 2 is odd.

 

stopbit : SINT (1,2) (default 1)

Selects the number of stop bits.

 

Note:

Only 1 stop bit is supported on the RS485 ports on NX32L devices.

 

rs485 : BOOL (default false)

This sets to true if communication is through the RS485 port, and false if the RS232 port is used.

 

Returns: INT

0

- Successful operation.

1

- Unsupported baud rate (X32 only).

2

- Baud rate not supported at the selected CPU speed.

4

- Serial port is already open (this might be a firmware option like I/O Extension).

5

- Serial port specified is not present on the device.

6

- Unsupported RS485 option.

7

- Unsupported parameter.

 

Declaration:

FUNCTION serOpen : INT;
VAR_INPUT
  port   : SINT := 0;     // Port number, 0 is programming port on non-USB devices, 1 is serial port 2 (only available on some devices)
  baud   : DINT := 9600; // baud rate (1200,2400,4800,9600,19200,38400,57600)
  bit     : SINT := 8;     // number of bits (7/8)
  parity : SINT := 0;     // 0=none,1=even,2=odd
  stopbit : SINT := 1;     // number of stopbits (1/2)
  rs485   : BOOL := FALSE; // TRUE if using RS485
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;