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
- SICL Functions for RS-232 Interface Sessions
- Interface Sessions Sample Programs
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.
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.
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