You are on page 1of 8

ICS DataCom Application Note AN-4

USING UniComm FOR WINDOWS 95


TO SEND AND RECEIVE SERIAL DATA

INTRODUCTION

Application Note AN-4 provides information on UniComm is language independent and can be
using UniComm Windows 95 drivers to send and used with any Windows compatible language such as
receive serial data. This note also shows how to use Visual Basic, C or Pascal. The UniComm DLL is added
UniComm to control the transmitter in ICS DataCom's to a C language program with an '# include' statement
485 series RS-232 to RS-485 Converters for half- and to a Visual Basic program by adding its definition
duplex applications. A sample program listing is in- file to the program project file list.
cluded with this Application Note. For information on
the theory of serial communication, refer to Application To use UniComm, first install UniComm as di-
Note AN-1, Serial DataCom Backgrounder. Also refer rected in its manual. Edit the Autoexec.bat file if
to your converter's manual for detailed instructions on necessary to add the UniComm directory to the path. In
its use and on how to set its internal jumpers. a C language program, use # include "UniCommw.h" to
add UniComm to the program. For Visual Basic pro-
PROBLEM WITH PC PROGRAM LANGUAGES gram, copy UniCommw.txt to the directory with the
program files and use the Visual Basic File menu to add
The most popular languages for engineering ap- UniCommw.txt to the project. The example Visual
plications on an IBM type PC are variations of BASIC Basic Program in this Application Note shows how to
and C. Because Microsoft Windows has become the use UniComm to select the COM port, set the baud rate,
operating system of choice and has replaced DOS for and send and receive serial messages.
most applications, Visual Basic and Visual C or C/C++
have become the language choice for most users. As
nice as theses languages are to use, they do not provide HANDLING HALF-DUPLEX
a way to control the serial ports on the PC nor to send or COMMUNICATION
receive serial data.
Half-duplex communication systems like two-
Third party DLLs such as UniComm for Win- wire RS-485 networks, have multiple transmitters and
dows provide a selection of functions that let the user receivers connected to the same data path. The system
send and receive serial data as well as control the serial works because only one device transmits at a time.
port handshake signals. By including UniComm with When a device has a message to send, its transmitter is
the user's program, the functions can be called directly enabled, the message is sent and the transmitter is again
within the program. This gives the user direct control disabled. The transmitters on the other devices are held
of the serial ports and their handshake signals. in the disabled state.

USING UniComm FOR WINDOWS External RS-232 to RS-485 Converters such as


ICS DataCom's 485 series Converters, can be set to use
UniComm is one of the easiest serial DLL pack- the RTS signal from the PC's COM port to enable its
ages to use. UniComm supports up to 64 serial ports, all transmitter. This capability makes these converters
standard baud rates and character formats. UniComm ideal for controlling RS-485 systems. Figure 1 shows
works with shared interrupts and controls the serial port how the RTS signal is used in the 485 series Converters
control lines which makes it ideal for controlling half to control the transmitters and the receivers. When RTS
duplex communication systems. is ON, it enables the transmitter and disables the re-
ceiver. When off, the receiver is enabled and the
transmitter is disabled (tristated).
ICS DataCom - Using UniComm for Windows cont'd AN-4

EXAMPLE PROGRAM CONSTRUCTION


Because there are times when all transmitters are
disabled, termination networks should be added to the The example program has two forms. The Main
system to hold the undriven lines in the mark state. Form is used to send and receive messages and calls the
Because the receive threshold is ± 0.2 volts, the resistors Port Form. The Port Form initializes the COM ports and
are chosen so the difference between the two signals is selects the baud rate. The following commands are used
> 0.4 volts. Short networks can get by with only one to select the COM port:
termination network. Long or high-speed networks
should be terminated with a network at both ends of the If CP <> 0 Then 'if a port is open, close it
cable. If multiple devices are connected to the network, CPClose (CP)
check the devices for extra termination networks that CPDestroy (CP)
may load the network beyond what a standard RS-485 CP = 0
End If
driver's limits. Refer to Application Note AN-2 for
PortNo = 1
information about proper termination of a tristated RS- If CP = 0 Then
485 network. CP = CPCreate(PortNo, SynchronousMode)
If CP = 0 Then
Beep
txtError2.Text = “CPCreate failed to create a
SD TX UniComm device”
TX txtError2.Visible = True
RTS End If
End If
RD RX
RX The CP Close and CPDestroy are essential to be
sure that any open port is closed before opening
Figure 1 Simplified RS-232 to RS-485 a new port. The CPCreate command returns CP
Converter with RTS Control Signal = 1 if the port was successfully opened. Call the
parameter setting functions after CPCreate to
finish setting up the port. i.e.
RS-485 Signal Timing
result = CPSetBaudRate(CP, baud)
Signal timing depends upon the speed of the If result = False Then
computer that you are using. Figure 2 shows the affect result = CPGetLastError(CP, Errcode, Errbuf,
of the RTS signal on the RS-485 lines. The lines are 100)
tristated until RTS is turned on and then return to the txtError2.Text = “SetBaudRate [code “ &
tristate mode when RTS is turned off. The important Errcode & “ - “ + Left$(Errbuf, InStr(Errbuf,
parameter is the turnoff time. Measure the turn-of time Chr$(0)) - 1) + “]”
with your computer and then check to see how fast the End If
other devices respond to a query. If they start transmit-
ting before the turnoff delay has expired, then you will The fixed parameters and the RTS and DTS lines
lose some data. Turnoff delays on a 386/40 MHz PC are initialized in the form Return routine since they are
with a 9600 baud rate can be up to 10 milliseconds. common to all of the COM ports. The Port Form is then
closed. The controls in the Main Form are enabled
when the program returns from the Port Form and the
RTS Start
port was successfully initialized.
Bit

TX Lines
Serial Data
Tristated
Turn on Turn off RS-485 Lines
Delay Delay

Figure 2 RS-485 Transmitter Timing


UniComm is a registered trademark of Amplicon Liveline Ltd.
Windows isa registerd trademarkof Microsoft Corporation.

2
ICS DataCom - Using UniComm for Windows cont'd AN-4

The following commands are used to send and EXAMPLE PROGRAM


receive data.
ICS has prepared a example Visual Basic pro-
‘sends text gram, called SER_KYBD, that can be used to send and
If AddrFlag = True Then receive messages over an RS-232 or RS-485 serial data
OutStr = Chr$(2) + Addr$ + txtMsgBox.Text + link. The program demonstrates how to select and
Chr$(10) initialize the COM port, set the baud rate and how to
Else
send and receive data in full and half-duplex modes.
OutStr = txtMsgBox.Text + Chr$(10)
End If
The program also shows how to address devices on a
RS-485 network. The program is available as an .exe
If FDMode = False Then CPSetRTS (CP) ‘set file that can be run on any computer. A listing of the
RTS on if half duplex source code is attached to this application note. Copies
LenOut = Len(OutStr) of the complete SER_KYBD program are available by
If LenOut > Txbufsize Then LenOut = (Txbufsize - contacting customer service at ICS or may be down-
1) loaded from www.icsdatacom.com.
result = CPWrite(CP, OutStr, LenOut)
If result = False Then The program starts with the Main Form shown in
result = CPGetLastError(CP, Errcode, Errbuf,
Figure 3. The Main Form initially only lets the user
100)
txtError.Text = “Open [code “ & Errcode & “ - “ +
click on "Initialize COM Ports" which brings up the
Left$(Errbuf, InStr(Errbuf, Chr$(0)) - 1) + “]” Port Form.
Beep
txtError.Visible = True Windows 95 Live Keyboard Program
End If
Initialize COM Port

If FDMode = False Then Mode Device Addressing and Value Send Control Char

Do Until CPGetTxCount(CP) = 0 Half-Duplex Enabled Set


CNTL-E CNTL-F
Full-Duplex Disabled Address
Loop
CPClearRTS (CP) ‘drop RTS when done
End If
Clear Message Box
If AutoQueryFlag = True Then ‘read if auto query Message Box

on Send Message

If InStr(LastOutStr, “?”) Then ‘and if ? in last


Get Response
output Automatic Query Response

Call Tdelay(0.4) Enabled Disabled


Exit
Call query
End If
End If
Figure 3 SER_KYBD Main Form Screen
Data to be sent is passed as txtMsgBox.Text. If the
address flag is on, an address preamble is added to the
front of the message so the proper unit on the RS-485 The Port Form, shown in Figure 4, lets the user
network will recognize the data or command. The select a baud rate and initialize a COM port as the serial
resultant string is saved as OutStr. If the program is port for the rest of the program. The port number is
operating in the half-duplex mode, the RTS line is saved in a global variable called PortNo. The "Return
asserted to enable the RS-485 transmitter by calling the to Main Form" button sets the remaining serial param-
CPSetRTS function. The message in OutStr is then eters, the handshake signal lines and closes the Port
send by calling CPWrite. The UniComm DLL then Form.
passes the characters in OutStr to the UART on the When the COM port has been initialized, the
serial I/O card. Data is received by transferring data remaining buttons on the Main Form are enabled. The
collected in UniComm's receive buffer to the user's user sets the operating mode by selecting Full or Half-
buffer. Refer to the Query subroutine in the listing on Duplex operation. If the program is communicating
page 5. with an addressed RS-485 device, the address is entered
and the Set button pressed to enable addressing. The
3
ICS DataCom - Using UniComm for Windows cont'd AN-4

CNTL-E and CNTL-F buttons sends single characters SER_KYBD Program Listing
to the serial device to enable/disable echo messages
back to the PC.
Note that the form information is not included
Output messages are entered into the Message
with the listing. The user can reconstruct the forms
Box from the keyboard. Clicking on the "Send Mes-
from Figures 3 and 4 or request a copy of the complete
sage" button asserts RTS, puts the message in the Port's
program from ICS.
Transmit buffer and then drops RTS when the message
has been sent. The "Get Response" button moves any
GLOBAL.BAS
data in the Port's Receive buffer into the Message Box. Attribute VB_Name = “GLOBAL”
If the Auto Query function is enabled, the program will Public PortNo As Long ‘COM port number
automatically move data from the Port's Receive buffer Public LenOut As Long ‘length of output string
Public OutStr As String * 256 ‘output string
into the Message Box. The "Clear Message Box" Public LastOutStr$ ‘output string
button clears the Message Box and gives it focus so the Public CP As Long
Public SynchronousMode As Long
user can enter a new message. Public Errcode As Long
Public Errbuf As String * 100
Public result As Boolean
frm PORT
Select Baud Rate and COM Port
frmMAIN File
Attribute VB_Name = “frmMain”
Attribute VB_Creatable = False
Select Baud Rate
2400 Attribute VB_Exposed = False
4800
COM 1 COM 2 Option Explicit
9600
19200
‘Public CP As Long
38400 Dim AddrFlag As Boolean ‘true when address prefix required
57600 COM 2 COM 4
115200
Dim FDMode As Boolean ‘true when full duplex mode
Dim LastOutStr As String
Dim OutStr As String
Dim InBuf As String * 256
Return to
Main Form Dim bl As Long
Dim AutoQueryFlag As Boolean
Dim Addr$ ‘address of serial device
Dim RxCount As Long
Dim Rxbufsize As Long
Figure 4 SER_KYBD Port Form Dim Txbufsize As Long
Sub query()
Dim LenInBuf
Because the example Visual Basic program txtMsgBox.Text = “”
uses two forms, the common variables are defined in txtError.Visible = False
Global.bas. This allows both forms to access the same ‘Call Tdelay(2)
RxCount = CPGetRxCount(CP)
variable and pass data between the two forms. If RxCount = 0 Then
txtError.Text = “No response received in RX Buffer”
Beep
txtError.Visible = True
SUMMARY Else
If RxCount > Rxbufsize Then RxCount = Rxbufsize
InBuf = String$(256, “ “)
This Application Note has shown the user how to result = CPRead(CP, InBuf, RxCount)
send and receive serial messages from a Visual Basic If result = False Then
program using the UniComm serial functions. These result = CPGetLastError(CP, Errcode, Errbuf, 100)
txtError.Text = “CPRead [code “ & Errcode & “ - “ + Left$(Errbuf,
functions are also used to control the transmitter in an InStr(Errbuf, Chr$(0)) - 1) + “]”
RS-232 to RS-485 converter so the PC program can Beep
control devices on an RS-485 network. Source code txtError.Visible = True
Else
from the example program shows how UniComm func- txtError.Visible = False
tions are used in an actual program. txtMsgBox.Text = RTrim$(InBuf)
txtMsgBox.Visible = True
txtError.Text = “Response received”
txtError.Visible = True
End If
End If
End Sub

4
ICS DataCom - Using UniComm for Windows cont'd AN-4
Sub Tdelay(delaytime) LenOut = Len(OutStr$)
Dim starttime result = CPWrite(CP, OutStr$, LenOut)
starttime = Timer If result = False Then
Do Until Timer >= (starttime + delaytime) result = CPGetLastError(CP, Errcode, Errbuf, 100)
Loop txtError.Text = “Open [code “ & Errcode & “ - “ + Left$(Errbuf,
End Sub InStr(Errbuf, Chr$(0)) - 1) + “]”
Beep
Sub cmdClear_Click() txtError.Visible = True
txtError.Visible = False End If
txtMsgBox.Visible = True End Sub
txtMsgBox.Text = “”
LastOutStr = “” Sub cmdInititalize_Click()
txtMsgBox.SetFocus Dim timeout As Long
End Sub txtError.Text = “”
txtError.Visible = False
frmPort.Show 1
Sub cmdE_Click() If CP = 0 Then
txtMsgBox.Text = Chr$(5) ‘cntl-E Beep
If AddrFlag = True Then txtError.Text = “COM Port not initialized, Retry”
OutStr$ = Chr$(2) + Addr$ + txtMsgBox.Text txtError.Visible = True
Else Call Tdelay(20)
OutStr$ = txtMsgBox.Text Else
End If result = CPSetBufferSizes(CP, Rxbufsize, Txbufsize)
If result = False Then
If FDMode = False Then CPSetRTS (CP) ‘set RTS on if half duplex Beep
txtError.Text = “Buffers did not set correctly”
LenOut = Len(OutStr$) txtError.Visible = True
result = CPWrite(CP, OutStr$, LenOut) Call Tdelay(20)
If result = False Then End If
result = CPGetLastError(CP, Errcode, Errbuf, 100) timeout = 2000 ‘time in ms
txtError.Text = “Open [code “ & Errcode & “ - “ + Left$(Errbuf, result = CPSetTimeouts(CP, 0, 0, timeout, 0, timeout)
InStr(Errbuf, Chr$(0)) - 1) + “]” If result = False Then
Beep Beep
txtError.Visible = True txtError.Text = “Timeouts did not set correctly”
End If txtError.Visible = True
End Sub Call Tdelay(20)
End If
optionAddrEna(0).Enabled = True
Sub cmdExit_Click() optionAddrDis(1).Enabled = True
txtError.Visible = False optionFull(1).Enabled = True
If CP <> 0 Then optionHalf(0).Enabled = True
result = CPClearDTR(CP) ‘reset DTR if set optionQueryEna(0).Enabled = True
If result = False Then optionQueryDis(1).Enabled = True
Beep cmdE.Enabled = True
txtError.Text = “DTR did not clear” cmdF.Enabled = True
txtError.Visible = True cmdClear.Enabled = True
End If cmdSend.Enabled = True
result = CPClearRTS(CP) ‘reset RTS if set cmdQuery.Enabled = True
If result = False Then txtMsgBox.Text = “”
Beep LastOutStr = “”
txtError.Text = “RTS did not clear” txtMsgBox.SetFocus
txtError.Visible = True End If
End If End Sub
CPClose (CP)
CPDestroy (CP) Sub cmdQuery_Click()
CP = 0 Dim LenInBuf
End If txtMsgBox.Text = “”
Unload frmMain txtError.Visible = False
End RxCount = CPGetRxCount(CP)
End Sub If RxCount = 0 Then
txtError.Text = “Nothing in RX Buffer”
Sub cmdF_Click() Beep
txtMsgBox.Text = Chr$(6) ‘cntl-F txtError.Visible = True
If AddrFlag = True Then Else
OutStr$ = Chr$(2) + Addr$ + txtMsgBox.Text If RxCount > Rxbufsize Then RxCount = Rxbufsize
Else InBuf = String$(256, “ “)
OutStr$ = txtMsgBox.Text result = CPRead(CP, InBuf, RxCount)
End If If result = False Then
result = CPGetLastError(CP, Errcode, Errbuf, 100)
If FDMode = False Then CPSetRTS (CP) ‘set RTS on if half duplex txtError.Text = “CPRead [code “ & Errcode & “ - “ + Left$(Errbuf,
InStr(Errbuf, Chr$(0)) - 1) + “]”

5
ICS DataCom - Using UniComm for Windows cont'd AN-4
Beep Addr$ = (txtAddr.Text)
txtError.Visible = True End If
Else cmdClear.Enabled = True
txtMsgBox.Text = RTrim$(InBuf) cmdSend.Enabled = True
txtMsgBox.Visible = True cmdQuery.Enabled = True
txtError.Text = “Rx Buffer Read” txtMsgBox.SetFocus
txtError.Visible = True cmdSet.Enabled = False
End If End If
End If End Sub
End Sub
Sub Form_Load()
Sub cmdSend_Click() txtError.Text = “”
‘sends MsgBox text txtMsgBox.Text = “”
txtError.Visible = False txtAddr = “”
If Len(txtMsgBox.Text) = 0 And Len(LastOutStr) = 0 Then ‘if nothing txtError.Visible = False
to send optionAddrEna(0).Enabled = False
Beep optionAddrDis(1).Enabled = True
txtError.Text = “Nothing to send” optionQueryEna(0).Enabled = True
txtError.Visible = True optionQueryDis(1).Enabled = False
Else optionHalf(0).Enabled = False
If Len(txtMsgBox.Text) = 0 Then optionFull(1).Enabled = True
txtMsgBox.Text = LastOutStr cmdSet.Enabled = False
End If cmdE.Enabled = False
LastOutStr = txtMsgBox.Text ‘save msg cmdF.Enabled = False
If AddrFlag = True Then AddrFlag = False
OutStr = Chr$(2) + Addr$ + txtMsgBox.Text + Chr$(10) FDMode = True
Else PortNo = 0
OutStr = txtMsgBox.Text + Chr$(10) AddrFlag = False
End If AutoQueryFlag = True
SynchronousMode = False
If FDMode = False Then CPSetRTS (CP) ‘set RTS on if half duplex Txbufsize = 256
Rxbufsize = 256
LenOut = Len(OutStr) End Sub
If LenOut > Txbufsize Then LenOut = (Txbufsize - 1)
result = CPWrite(CP, OutStr, LenOut) Sub optionAddrDis_Click(Index As Integer)
If result = False Then AddrFlag = False
result = CPGetLastError(CP, Errcode, Errbuf, 100) End Sub
txtError.Text = “Open [code “ & Errcode & “ - “ + Left$(Errbuf,
InStr(Errbuf, Chr$(0)) - 1) + “]” Sub optionAddrEna_Click(Index As Integer)
Beep AddrFlag = True
txtError.Visible = True cmdClear.Enabled = False
End If cmdSend.Enabled = False
cmdQuery.Enabled = False
If FDMode = False Then txtAddr.SetFocus
Do Until CPGetTxCount(CP) = 0 End Sub
Loop
CPClearRTS (CP) ‘drop RTS when done Sub optionFull_Click(Index As Integer)
End If FDMode = True ‘full duplex, RTS always on
If AutoQueryFlag = True Then ‘read if auto query on result = CPSetRTS(CP)
If InStr(LastOutStr, “?”) Then ‘and if ? in last output If result = False Then
Call Tdelay(0.4) result = CPGetLastError(CP, Errcode, Errbuf, 100)
Call query txtError.Text = “Set RTS [code “ & Errcode & “ - “ + Left$(Errbuf,
End If InStr(Errbuf, Chr$(0)) - 1) + “]”
End If Beep
txtError.Visible = True
End If End If
End Sub End Sub

Sub cmdSet_Click() Sub optionHalf_Click(Index As Integer)


‘verifies and sets address FDMode = False ‘half duplex RS-485 mode
txtError.Visible = False result = CPClearRTS(CP)
If Val(txtAddr.Text) > 15 Or (Val(txtAddr.Text) = 0 And txtAddr.Text If result = False Then
<> “0”) Then result = CPGetLastError(CP, Errcode, Errbuf, 100)
Beep txtError.Text = “Clear RTS [code “ & Errcode & “ - “ + Left$(Errbuf,
txtError.Text = “Address value not 0 to 15, re-enter” InStr(Errbuf, Chr$(0)) - 1) + “]”
txtError.Visible = True Beep
Else txtError.Visible = True
If Val(txtAddr.Text) > 9 Then End If
Addr$ = Chr$(Val(txtAddr.Text) + 48) End Sub
Else

6
ICS DataCom - Using UniComm for Windows cont'd AN-4
Sub optionQueryDis_Click(Index As Integer) txtError2.Visible = False
AutoQueryFlag = False If CP <> 0 Then
End Sub CPClose (CP)
CPDestroy (CP)
Sub optionQueryEna_Click(Index As Integer) CP = 0
AutoQueryFlag = True End If
End Sub PortNo = 2
If CP = 0 Then
Private Sub txtAddr_Change() CP = CPCreate(PortNo, SynchronousMode)
cmdSet.Enabled = True If CP = 0 Then
End Sub Beep
txtError2.Text = “CPCreate failed to create a UniComm device”
txtError2.Visible = True
frmPORT File End If
End If
Attribute VB_Name = “frmPort” If CP <> 0 Then
Attribute VB_Creatable = False result = CPOpen(CP)
Attribute VB_Exposed = False If result = False Then
Option Explicit result = CPGetLastError(CP, Errcode, Errbuf, 100)
Dim Index As Integer txtError2.Text = “CPOpen [code “ & Errcode & “ - “ + Left$(Errbuf,
Dim baud As Long InStr(Errbuf, Chr$(0)) - 1) + “]”
Sub Tdelay(delaytime) Beep
Dim starttime txtError2.Visible = True
starttime = Timer Else
Do Until Timer >= (starttime + delaytime) result = CPSetBaudRate(CP, baud)
Loop If result = False Then
End Sub result = CPGetLastError(CP, Errcode, Errbuf, 100)
txtError2.Text = “SetBaudRate [code “ & Errcode & “ - “ +
Sub cmdCOM1_Click() Left$(Errbuf, InStr(Errbuf, Chr$(0)) - 1) + “]”
txtError2.Text = “” Beep
txtError2.Visible = False txtError2.Visible = True
If CP <> 0 Then Else
CPClose (CP) txtError2.Text = “COM2 initialized at “ + Str$(baud)
CPDestroy (CP) txtError2.Visible = True
CP = 0 End If
End If End If
PortNo = 1 End If
If CP = 0 Then End Sub
CP = CPCreate(PortNo, SynchronousMode)
If CP = 0 Then Sub cmdCOM3_Click()
Beep txtError2.Text = “”
txtError2.Text = “CPCreate failed to create a UniComm device” txtError2.Visible = False
txtError2.Visible = True If CP <> 0 Then
End If CPClose (CP)
End If CPDestroy (CP)
If CP <> 0 Then CP = 0
result = CPOpen(CP) End If
If result = False Then PortNo = 3
result = CPGetLastError(CP, Errcode, Errbuf, 100) If CP = 0 Then
txtError2.Text = “CPOpen [code “ & Errcode & “ - “ + Left$(Errbuf, CP = CPCreate(PortNo, SynchronousMode)
InStr(Errbuf, Chr$(0)) - 1) + “]” If CP = 0 Then
Beep Beep
txtError2.Visible = True txtError2.Text = “CPCreate failed to create a UniComm device”
Else txtError2.Visible = True
result = CPSetBaudRate(CP, baud) End If
If result = False Then End If
result = CPGetLastError(CP, Errcode, Errbuf, 100) If CP <> 0 Then
txtError2.Text = “SetBaudRate [code “ & Errcode & “ - “ + result = CPOpen(CP)
Left$(Errbuf, InStr(Errbuf, Chr$(0)) - 1) + “]” If result = False Then
Beep result = CPGetLastError(CP, Errcode, Errbuf, 100)
txtError2.Visible = True txtError2.Text = “CPOpen [code “ & Errcode & “ - “ + Left$(Errbuf,
Else InStr(Errbuf, Chr$(0)) - 1) + “]”
txtError2.Text = “COM1 initialized at “ + Str$(baud) Beep
txtError2.Visible = True txtError2.Visible = True
End If Else
End If result = CPSetBaudRate(CP, baud)
End If If result = False Then
End Sub result = CPGetLastError(CP, Errcode, Errbuf, 100)
txtError2.Text = “SetBaudRate [code “ & Errcode & “ - “ +
Sub cmdCOM2_Click() Left$(Errbuf, InStr(Errbuf, Chr$(0)) - 1) + “]”
txtError2.Text = “” Beep

7
ICS DataCom - Using UniComm for Windows cont'd AN-4
txtError2.Visible = True txtError2.Visible = True
Else Call Tdelay(20)
txtError2.Text = “COM3 initialized at “ + Str$(baud) End If
txtError2.Visible = True frmPort.Hide
End If End Sub
End If
End If Sub Form_GotFocus()
End Sub Call newsub
End Sub
Sub cmdCOM4_Click()
txtError2.Text = “” Sub Form_Load()
txtError2.Visible = False txtError2.Visible = False
If CP <> 0 Then baud = 9600
CPClose (CP) Index = 2
CPDestroy (CP) optionBaud(2).value = True
CP = 0 End Sub
End If
PortNo = 4 Sub optionBaud_Click(Index As Integer)
If CP = 0 Then Select Case Index
CP = CPCreate(PortNo, SynchronousMode) Case 0
If CP = 0 Then baud = 2400
Beep Case 1
txtError2.Text = “CPCreate failed to create a UniComm device” baud = 4800
txtError2.Visible = True Case 2
End If baud = 9600
End If Case 3
If CP <> 0 Then baud = 19200
result = CPOpen(CP) Case 4
If result = False Then baud = 38400
result = CPGetLastError(CP, Errcode, Errbuf, 100) Case 5
txtError2.Text = “CPOpen [code “ & Errcode & “ - “ + Left$(Errbuf, baud = 57600
InStr(Errbuf, Chr$(0)) - 1) + “]” Case Else
Beep baud = 115200
txtError2.Visible = True End Select
Else If CP <> 0 Then
result = CPSetBaudRate(CP, baud) result = CPSetBaudRate(CP, baud)
If result = False Then If result = False Then
result = CPGetLastError(CP, Errcode, Errbuf, 100) result = CPGetLastError(CP, Errcode, Errbuf, 100)
txtError2.Text = “SetBaudRate [code “ & Errcode & “ - “ + txtError2.Text = “SetBaudRate [code “ & Errcode & “ - “ +
Left$(Errbuf, InStr(Errbuf, Chr$(0)) - 1) + “]” Left$(Errbuf, InStr(Errbuf, Chr$(0)) - 1) + “]”
Beep Beep
txtError2.Visible = True txtError2.Visible = True
Else Else
txtError2.Text = “COM4 initialized at “ + Str$(baud) txtError2.Text = “Baud rate set to “ + Str$(baud)
txtError2.Visible = True txtError2.Visible = True
End If End If
End If End If
End If End Sub
End Sub

Sub cmdReturn_Click()
result = True
result = CPSetParity(CP, NOPARITY) ‘sets no parity
result = CPSetDataBits(CP, 8) ‘sets 8 data bits
result = CPSetStopBits(CP, 1) ‘sets 1 stop bit
result = CPSetHandshaking(CP, HS_NONE) ‘sets no handshaking
result = CPSetDTR(CP) ‘sets DTR on
If result = False Then
result = CPGetLastError(CP, Errcode, Errbuf, 100)
txtError2.Text = “SetDTR “ & Errcode & “ - “ + Left$(Errbuf,

Beep
InStr(Errbuf, Chr$(0)) - 1) ICS DataCom
txtError2.Visible = True division of Systems West, Inc.
Call Tdelay(20)
End If 473 Los Coches Street
result = CPSetRTS(CP) ‘sets RTS on,assumes FD mode
If result = False Then Milpitas, CA 95035
result = CPGetLastError(CP, Errcode, Errbuf, 100) Phone: (408) 263-5500
txtError2.Text = “SetRTS [code “ & Errcode & “ - “ + Left$(Errbuf,
InStr(Errbuf, Chr$(0)) - 1) + “]” Fax: (408) 263-5896
Beep email: custsvc@icsdatacom.com
Rev 5/98
8

You might also like