Using RS-232 Interface Sessions

RS-232 interface sessions can be used to get or set the characteristics of the RS-232 interface. Examples of some of these characteristics are baud rate, parity, and flow control. There are specific interface session interrupts that can be used.

Subtopics are:

Addressing RS-232 Interfaces

To create an interface sessionon RS-232, specify the interface logical unit or SICL interface ID in the addr parameter of the iopen function. The interface logical unit and SICL interface ID are defined by running Connection Expert.

Some example addresses for RS-232 interface sessions follow.

Sample RS-232 Addresses

COM1

A SICL interface ID

serial

A SICL interface ID

1

An interface logical unit

These code samples open an interface session with the RS-232 interface.

C sample:

INST intf;
intf = iopen (“COM1”);

Visual Basic sample:

Dim intf As Integer
intf = iopen (“COM1”)

SICL Functions for RS-232 Interface Sessions

This section describes how some SICL functions are implemented for RS-232 interface sessions.

Implementing Some SICL Functions for RS-232

Functions

Description

iwrite, iread

All I/O functions (non-formatted and formatted) work the same as for device sessions. However, it is recommended that all I/O be performed with device sessions to make your programs easier to maintain.

ixtrig

Provides a method of triggering using either the DTR or RTS modem status line. This function clears the specified modem status line, waits 10 milliseconds, then sets it again. Specifying I_TRIG_STD is the same as specifying I_TRIG_SERIAL_DTR.

itrigger

Pulses the DTR modem control line for 10 milliseconds.

iclear

Sends a break, aborts any pending writes, discards any data in the receive buffer, resets any flow control states (such as XON/XOFF), and resets any error conditions. To reset the interface without sending a break, use: iserialctrl (id, I_SERIAL_RESET, 0)

ionsrq|

Installs a service request handler for this session. The concept of service request (SRQ) originates from GPIB. On a GPIB interface, a device can request service from the controller by asserting a line on the interface bus.

RS-232 does not have a specific line assigned as a service request line. However, you can assign one of the modem status lines (RI, DCD, CTS, or DSR) as the service request line by running Connection Expert.

Any transition on the designated service request line will cause an SRQ handler in your program to be called. (Be sure not to set the SRQ line to CTS or DSR if you are also using that line for hardware flow control.)

Service requests are supported for both device sessions and interface sessions. When the designated SRQ line changes state, the RS-232 driver calls all SRQ handlers installed by either device sessions or interface sessions.

iserialctrl

Sets the characteristics of the Serial interface. The following requests are clarified:

I_SERIAL_DUPLEX: The duplex setting determines whether data can be sent and received simultaneously. Setting full duplex allows simultaneous send and receive data traffic. Setting half duplex (the default) will cause reads and writes to be interleaved, so that data is flowing in only one direction at any given time. (The exception to this is if XON/XOFF flow control is used.)

I_SERIAL_READ_BUFSZ: The default read buffer size is 2048 bytes.

I_SERIAL_RESET: Performs the same function as the iclear function on an interface session, except that a break is not sent.

iserialstat

Gets the characteristics of the Serial interface. The following requests are clarified:

I_SERIAL_MSL: Gets the state of the modem status line. Because of the way Windows supports RS-232, the I_SERIAL_RI bit will never be set. However, the I_SERIAL_TERI bit will be set when the RI modem status line changes from high to low.

I_SERIAL_STAT: Gets the status of the transmit and receive buffers and the errors that have occurred since the last time this request was made. Only the error bits (I_SERIAL_PARITY, I_SERIAL_OVERFLOW, I_SERIAL_FRAMING, and I_SERIAL_BREAK) are cleared. The I_SERIAL_READ_DAV and I_SERIAL_TEMT bits reflect the status of the buffers at all times.

I_SERIAL_READ_DAV: Gets the current amount of data available for reading. This shows how much data is in Windows receive buffer, not how much data is in the buffer used by the formatted input functions such as iscanf.

iserialmclctrl

Controls the modem control lines RTS and DTR. If one of these lines is being used for flow control, you cannot set that line with this function.

iserialmclstat

Determines the current state of the modem control lines. If one of these lines is being used for flow control, this function may not give the correct state of that line.

Interface Sessions Sample Programs

This section contains two sample programs for RS-232 interface device session programming.

Sample: RS-232 Interface Session (C)

/*ser_intf.c
This program gets the current configuration of the serial port, sets it to 9600 baud, no parity, 8 data bits, and 1 stop bit, and prints the old configuration.*/

#include <stdio.h>
#include <sicl.h>

main()
{
INST intf; /* interface session id */
unsigned long baudrate, parity, databits,
stopbits;
char *parity_str;

/* Log message and exit program on error */
ionerror (I_ERROR_EXIT);

/* open RS-232 interface session */
intf = iopen (“COM1”);
itimeout (intf, 10000);

/* get baud rate, parity, data and stop bits */
iserialstat (intf, I_SERIAL_BAUD, &baudrate);
iserialstat (intf, I_SERIAL_PARITY, &parity);
iserialstat (intf, I_SERIAL_WIDTH, &databits);
iserialstat (intf, I_SERIAL_STOP, &stopbits);

/* determine string to display for parity */
if (parity == I_SERIAL_PAR_NONE) parity_str =
“NONE”;
else if (parity == I_SERIAL_PAR_ODD)
parity_str = “ODD”;
else if (parity == I_SERIAL_PAR_EVEN)
parity_str = “EVEN”;
else if (parity == I_SERIAL_PAR_MARK)
parity_str = “MARK”;
else /*parity == I_SERIAL_PAR_SPACE*/
parity_str = “SPACE”;

/* set to 9600,NONE,8,1 */
iserialctrl (intf, I_SERIAL_BAUD, 9600);
iserialctrl (intf, I_SERIAL_PARITY,
I_SERIAL_PAR_NONE);
iserialctrl (intf, I_SERIAL_WIDTH,
I_SERIAL_CHAR_8);
iserialctrl (intf, I_SERIAL_STOP,
I_SERIAL_STOP_1);

/* Display previous settings */
printf(“Old settings: %5ld,%s,%ld,%ld\n”,
baudrate, parity_str, databits, stopbits);

/* close port */
iclose (intf);

/* This call is a no-op for WIN32 programs. */
_siclcleanup();

return 0;
}

Sample: RS-232 Interface Session (Visual Basic)

Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''
' set_intf.bas
' This program (1) gets the current
‘ configuration of the ' serial port; (2) sets
‘ it to 9600 baud, no parity, 8 data bits, and 1
‘ stop bit;(3) prints the old configuration
'''''''''''''''''''''''''''''''''''''''''''''''

Sub Main()

Dim intf As Integer
  Dim baudrate As Long
Dim parity As Long
Dim databits As Long
Dim stopbits As Long
Dim parity_str As String
Dim msg_str As String

' open RS-232 interface session
' "COM1" is the SICL Interface name as defined
‘ in Connection Expert
  ' Change this to the SICL Name you have
‘ defined in Connection Expert

intf = iopen("COM1")

Call itimeout(intf, 10000)

' get baud rate, parity, data bits, and stop
‘ bits
Call iserialstat(intf, I_SERIAL_BAUD,
baudrate)
Call iserialstat(intf, I_SERIAL_PARITY,
parity)
Call iserialstat(intf, I_SERIAL_WIDTH,
databits)
Call iserialstat(intf, I_SERIAL_STOP,
stopbits)

' determine string to display for parity
Select Case parity
Case I_SERIAL_PAR_NONE
parity_str = "NONE"
Case I_SERIAL_PAR_ODD
parity_str = "ODD"
Case I_SERIAL_PAR_EVEN
parity_str = "EVEN"
Case I_SERIAL_PAR_MARK
parity_str = "MARK"
Case Else
parity_str = "SPACE"
End Select

' set to 9600,NONE,8, 1
Call iserialctrl(intf, I_SERIAL_BAUD, 9600)

Call iserialctrl(intf, I_SERIAL_PARITY,_
I_SERIAL_PAR_NONE)
Call iserialctrl(intf, I_SERIAL_WIDTH,_
I_SERIAL_CHAR_8)
Call iserialctrl(intf, I_SERIAL_STOP,
I_SERIAL_STOP_1)

' display previous settings
msg_str = "Old settings: " & _
Str$(baudrate) & "," & _
parity_str & "," & _
Str$(databits) & "," & _
Str$(stopbits)
MsgBox msg_str, vbExclamation

' close port
Call iclose(intf)

' Tell SICL to cleanup for this task
Call siclcleanup

End Sub