SerialStream

(Engine-Level Function)

Description: Returns a serial stream that can be used in any of the serial port functions or with any of the stream functions. Please note that the ComPort function (which functions somewhat differently than the SerialPort function) may also be utilized.
Returns: Stream
Usage: Script Only.
Function Groups: Serial Port,  Stream and Socket
Related to: COMPort | SerCheck | SerIn | StrLen | SerOut | SerRcv | SerSend | SerString | SerStrEsc | SerWait
Format: SerialStream(Port, ReceiveLen, TransmitLen, Baud, DataBits, StopBits, Parity, RTS, XOnXOff, Control)
Parameters:  
Port
Required. Any numeric expression giving the serial port number to be used. For COM1: Port = 1. For COM2: Port = 2. The valid range for Port is 1 to 4096.
ReceiveLen
Required. Any numeric expression giving the size of the receiver buffer in bytes. ReceiveLen must be in the range 2 to 32766.
If more bytes are received than can fit in the receive buffer before your application removes them using SerRcv or a similar VTScada function, the additional data will be lost.
TransmitLen
Required. Any numeric expression giving the size of the transmitter buffer in bytes. TransmitLen must be in the range 2 to 32766. The buffer must be large enough to hold the maximum number of bytes pending transmission at any instance.
Baud
Required. Any numeric expression giving the baud rate. Baud must be in the range 10 to 57600, and must divide evenly into 115200 with no more than 2.5% error.
DataBits
Required. Any numeric expression giving the number of data bits per character. DataBits must be 5, 6, 7, or 8.
StopBits
Required. Any numeric expression giving the number of stop bits per character. StopBits must be 1 or 2.
Parity
Required. Any numeric expression giving the parity checking to use:

Parity

Checking

0

No parity

1

Odd parity

2

Even parity

3

0 Stick (space parity)

4

1 Stick (mark parity)

RTS
Required. Any numeric expression that gives the RTS buffer control method. RTS is on while transmitting. When a transmission is complete, RTS is off.
This is usually used to control the transmitters on RS-422/485 ports. This parameter has no effect if the automatic RTS control is selected in the Control parameter.
Acceptable values of the RTS parameter are as follows:

RTS

Buffer Control Method

0

 Force RTS off

1

Force RTS on

2

Half-duplex operation

3

Controlled by SerRTS function

If this parameter is 2, the SerRTS function can set its value. However, regardless of SerRTS, the RTS control line will be asserted when data is sent.

If the SerRTS is called to change the RTS line while data is being transmitted, the RTS line will not change when the last byte is sent. If SerRTS is not executed while the data is transmitted, the RTS line will be cleared after the last byte is transmitted.

XOnXOff
Required. Any logical expression. If true (non-0) software flow control is to be used. If false (0) it is not.
Control
Required. Any numeric expression that specifies the handling procedure for the clear to send (CTS), carrier detect (CD), and data set ready (DSR) input lines, and the data terminal ready (DTR) output line on the serial port. The value must be in the range 0 to 63. The desired action is the sum of the following values:

Control

Bit No.

Handling Procedure

1

0

DTR on (otherwise DTR is off)

2

1

Enable CTS control

4

2

Enable CD control

8

3

Enable DSR control

16

4

Enable RTS/CTS control

32

5

Enable DTR/DSR control

(See: Bitwise Parameters & Operations)

If bit 1 (CTS control) is set, data will only be transmitted if the CTS signal is on. If CTS control is disabled, the CTS line is ignored.

If bit 2 (CD control) is set, data will only be transmitted when the CD signal is on. If CD control is disabled, the CD line is ignored.

If bit 3 (DSR control) is set, data will only be transmitted when the DSR signal is on. If DSR control is disabled, the DSR line is ignored.

If bit 4 (RTS/CTS control) is set, the CTS control behaves as described above, and the RTS line will be held high until the receive buffer reaches 75% full. It will then go low, indicating to the other device to stop transmitting data. The RTS line will go high again when the receive data buffer drops below 25% full. This is known as hardware flow control. RTS/CTS control enabled overrides the RTS parameter.

If bit 5 (DTR/DSR control) is set, the DSR control behaves as described above, and the DTR line will be held high until the receive buffer reaches 75% full. It will then go low, indicating to the other device to stop transmitting data. The DTR line will go high again when the receive data buffer drops below 25% full. This is known as hardware flow control. DTR/DSR control enabled overrides bit 0, DTR on option.

Comments: The parameters for this function are a reduced set from the Comport statement.

Make sure that VTScada's mouse (if it is serial) is on a different port, because the mouse and SerialStream can interfere. Also make sure that no other hardware or software is interfering with the serial port hardware interrupts (IRQ4 for COM1:, IRQ3 for COM2:). Network cards often use IRQ4, which will cause a problem with a mouse or SerialStream on COM1:.

The stream will automatically close when there are no variables referencing the stream. However, if there are bytes still in the transmit buffer, they will not be sent before the stream is closed.

Example:

streamCom2 = SerialStream(2 { COM2: },
                    1024 { Buffer 1024 bytes of received data },
                    1024 { Buffer 1024 bytes of transmitted data },
                    9600 { Baud rate },
                    8 { Data bits per byte },
                    1 { Stop bit per byte },
                    0 { No parity bit },
                    1 { Force RTS on },
                    0 { No Software flow control (XonXOff) },
                    3 { Control: DTR On, CTS control enabled });

This opens COM2: for use with serial port functions. These functions should use streamCom2 as their Port parameter.