Windows Standard
Serial Communications
Library for Delphi
Programmer's Manual
(WSC_4D)
Version 4.3
September 28, 2007
This software is provided as-is.
There are no warranties, expressed or implied.
Copyright (C) 2007
All rights reserved
MarshallSoft Computing, Inc.
Post Office Box 4543
Huntsville AL 35815
Voice : 1.256.881.4630
email : info@marshallsoft.com
Web : www.marshallsoft.com
MARSHALLSOFT is a registered trademark of MarshallSoft Computing.
1 Introduction
1.1 Features2 Library Overview
1.2 Documentation Set
1.3 Example Program
1.4 Installation
1.5 Uninstalling
1.6 Pricing
1.8 Updates
2.1 Dynamic Link Libraries3 Compiler Issues
2.2 Keycode
2.3 Win32 STDCALL and DECLSPEC
2.4 Limitations on COM Ports
2.5 Using Threads
2.6 Waiting for New Serial Data
2.7 Using Messages
2.8 Error Display
2.9 SioEvent Logic
2.10 Virtual Serial Ports
2.11 Using the WSC Unit
2.12 Adding WSC4D to your Project
3.1 Delphi Versions4 Example Programs
3.1.1 Delphi 13.2 Compiling Programs
3.1.2 Delphi 2
3.1.3 Delphi 3
3.1.4 Delphi 4.5. and 6
3.1.5 Delphi 7
3.1.6 Delphi 2005, 2006, 2007
3.3 Compiling WSC Source
4.1 VERSION5 Revision History
4.2 EASY
4.3 SELFTEST
4.4 MODEM
4.5 TERM
4.6 FINDER
4.7 EVENT
4.8 DEVICE
4.9 MESSAGE
4.10 XMS and XMR
4.11 YMS and YMR
4.12 RS485
4.13 SIMPLE
The Windows Standard Serial Communications Library for Delphi (WSC4D) is a component library of functions providing the capability to quickly write serial communication applications in Delphi or Delphi .NET. The WSC component library uses the standard Windows API (Application Programmer's Interface) to communicate with any device connected to a serial port.
A straightforward interface is provided to access data from a serial port using RS232 or multi-drop RS422 or RS485 ports. WSC also supports virtual serial ports using Bluetooth serial and USB-to-serial converters.
The Windows Standard Serial Communications Library for Delphi (WSC4D) supports and has been tested with all versions of Borland Delphi including Delphi 2005, Delphi 2006, Delphi 2007 and Delphi for .NET. WSC4D includes numerous Delphi example programs that demonstrate serial port communications to help software developers easily build software applications using the WSC SDK.
WSC has been tested on multiple computers running Windows 95/98, Windows Me, Windows NT4, Windows 2000, Windows XP and Windows Vista. Both Win16 and Win32 DLLs are provided. The WSC DLL's (WSC16.DLL and WSC32.DLL) can be used from any language (C/C++, Visual C++ .NET, Visual C#, Visual FoxPro, Visual Basic, VB.NET, Visual FoxPro, Visual dBase, and Xbase++) capable of calling the Windows API.
When comparing the Windows Standard Serial Communications Library against our competition, note that:
MarshallSoft also has versions of the Windows Standard Serial Communications Library for Visual C/C++ (WSC4C), Visual Basic (WSC4VB), PowerBASIC (WSC4PB), Visual FoxPro (WSC4FP), Visual dBASE (WSC4DB), and Xbase++ (WSC4XB). All versions of WSC use the same DLLs (WSC16.DLL or WSC32.DLL). However, the examples provided for each version are written in the specified computer environment. Development time is shortened because programmers need only learn one interface.
The latest versions of the Windows Standard Serial Communications Library (WSC) can be downloaded from our web site at http://www.marshallsoft.com/serial-communication-library.htm
Our goal is to provide a robust serial communication library component that you and your customers can depend upon. A fully functional evaluation version is available. Contact us if you have any questions.
Some of the many features of the Windows Serial Communications Library for Delphi are:
A good selection of Delphi example programs with full source code is included. Refer to section 4.0 for more details on each of the example programs.
VERSION : Displays WSC version number.
EASY : A simple RS232 terminal program.
RS485 : A simple RS485 terminal program.
SELFTEST: Performs COM port functionality testing.
MODEM : Same as EASY but controls flow control, modem lines, etc.
TERM : Terminal emulator with XMODEM, YMODEM, and ANSI support.
FINDER : Finds a modem connected to one of the serial ports.
EVENT : Same as EASY except uses SioEvent to await incoming data.
MESSAGE : Same as EASY except uses SioMessage to await incoming data.
XMS/XMR : XMODEM send/receive example programs.
YMS/YMR : YMODEM send/receive example programs.
DEVICE : Sends text string to serial device.
SIMPLE : Delphi 2005/2006/2007 .NET terminal program.
WSC4D contains 34 functions and modem control. All functions return a negative number if an error condition is detected. For more details, consult the WSC Reference Manual (WSC_REF) and the Serial User's Manual (SERIAL).
The complete set of documentation consists of four manuals in three formats. This is the first manual (WSC_4D) in the set.
The WSC_4D Programmer's Manual is the language specific manual. All language dependent programming issues are discussed in this manual. Information needed to compile your programs in a Delphi environment is provided in this manual.
The WSC User's Manual (WSC_USR) discusses language independent serial communications programming issues including modem control. Purchasing and license information is also provided.
The WSC Reference Manual (WSC_REF) contains details on each individual WSC function.
The Serial Communications User's Manual (SERIAL) contains background information on serial port hardware.
Each manual comes in three formats:
The documentation is also provided on our web site at
http://www.marshallsoft.com/wsc4d.htm
var
Code : Integer;
begin
{pass the key code}
Code := SioKeyCode(WSC_KEY_CODE) < 0 then
Code := SioReset(COM1, 1024, 1024);
If Code < 0 Then
begin
WriteLn('Cannot open port');
exit
end
{ transmit "AT" }
Code := SioPutc(COM1, 'A');
Code := SioPutc(COM1, 'T');
Code := SioPutc(COM1, Chr(13))
end;
Refer to section 4.0 for complete examples with source. Refer to the WSC Reference Manual (WSC_REF) for individual function details. Access online at http://www.marshallsoft.com/wsc_ref.htm
WSC4D comes configured for 32-bit Delphi (Delphi 2 and up). Before compiling for 16-bit Delphi (Delphi 1) you must first change the uses clause in all source files from wsc32.pas to wsc16.pas (or wsc16nt.pas if running Win NT/2000/XP.
Uninstalling WSC4D is very easy. WSC4D does not modify the registry. First, delete the WSC4D project directory created when installing WSC4D. Next, delete WSC16.DLL and WSC32.DLL from your Windows directory, typically C:\WINDOWS for Windows 95/98/Me/XP/Vista or C:\WINNT for Windows NT/2000.
Running the UNINSTAL.BAT batch file will also delete WSC32.DLL, MIO32.DLL, XYM32.DLL, and ASD32.DLL as described above.
A developer license for WSC4D can be registered for $115 (or $195 with ANSI C source code to the library DLL's). Purchasing details can be found in Section 1.3 "How to Purchase" in the WSC User's Manual (WSC_USR.HTM). Also see INVOICE.TXT provided with the evaluation version or order directly on our web site at
http://www.marshallsoft.com/order.htm
When a developer license is purchased, the developer receives a set of registered DLLs plus a license file (WSCxxxxx.LIC) The license file is needed to download updates to the registered DLL's for a period of one year from purchase. Updates can be downloaded from
http://www.marshallsoft.com/oem.htm
After one year, your license must be updated if you want to be able to download updates. Your license can be updated for:
If source code was previously purchased, updates to the source code can be purchased for $50 along with the DLL update.
Note that the registered DLL's do not expire.
WSC4D has been tested on multiple computers running Windows 95/98, Windows Me, Windows NT4, Windows 2000, Windows XP and Windows Vista.
Please examine the WSC16.PAS and WSC32.PAS files. Note that COM1 is defined as port zero, not port one. The user must assume the responsibility for passing the correct information when calling WSC4D functions.
The Windows Serial Communications (WSC) library component includes both Win16 and a Win32 dynamic link library (DLL). A DLL is characterized by the fact that it need not be loaded until required by an application program and that only one copy of the DLL is necessary regardless of the number of application programs that use it. Contrast this to the traditional static library that is bound to each and every application that uses it at link time.
An important advantage that DLLs have over other "popular" library formats such as DelphiX or OCX is that DLLs are callable by all Windows applications. Since DLLs are the building blocks of the Windows Operating System, they will not be replaced by a "newer technology".
WSC16.DLL and WSC32.DLL each has a keycode encoded within them. The keycode is a 9 or 10 digit decimal number (unless it is 0), and will be found in the file KEYCODE.PAS. The keycode for the evaluation (shareware) version is 0. A new keycode and a set of new DLL's are provided after purchasing a developer license. The KEYCODE is passed to SioKeyCode.
If an error message (value -108) is received when calling SioKeyCode, it means that the keycode in your application does not match the keycode in the WSC DLL. After registering, it is best to remove the evaluation version of the WSC32.DLL and WSC16.DLL from the Windows search path or delete them.
WSC32 is written in ANSI C and is compiled using the STDCALL and DECLSPEC keywords. This means that WSC4D uses the same calling conventions and file naming conventions as the Win32 API. In particular, function names are NOT decorated. There are neither leading underscores nor trailing "@size" strings added to function names.
The WSC32.DLL functions may be called by any Windows application program capable of calling the Windows API provided that the proper declaration file is used.
The 32-bit version of WSC4D (WSC32.DLL) can use any port from COM1 to COM256, provided that the port is known to Windows 95/98/NT/2000/Me/XP/Vista and there is physical hardware present.
WSC4D is thread safe, and can be used from any Windows Win32 application capable of using threads. Note that 16-bit Windows does not support threads.
Refer to the EVENT program (EVNT_PRJ) for an example of using threads with WSC4D.
All serial data is moved from the UART's buffer to the receive queue in memory (by the Windows serial port driver) under interrupt control. Similarly, all out going serial data is moved to the transmit queue in memory.
There are several methods that can be used to wait for new incoming serial data, as follows:
The most straightforward method is to use a Delphi timer to check the receive queue every so often. The timer interval should be set between 50 milliseconds and 250 milliseconds. Setting it much less than 50 milliseconds would consume considerable system resources polling, and setting it greater than 250 milliseconds will result in sluggish menu response times. A good compromise is to set the timer interval to 125 milliseconds.
The "Message Method" is probably the most natural method to use with Delphi (recall that WSC works with many different computer languages). In this method, the SioMessage function is called which sends a Windows message to a Delphi button when new data is ready to be read.
This method uses SioEvent in a thread (background process) which blocks (efficiently waits) until new data is available. This method requires creating a thread in Delphi.
This method uses the SioEventWait function in a timer procedure to block (efficiently wait) until new data is available or until the timer period expires. A good choice for the timer interval is 250 milliseconds because it allows quick response to user input and also at the same time minimizes polling. Note that a 250 millisecond interval represents quite a large interval of CPU time.
WSC is capable (SioMessage function) of sending windows messages in response to specified serial events. Like using threads, 32-bit Delphi is required.
Refer to the MESSAGE program (MESS_PRJ) for an example of using messages with WSC4D.
The error message text associated with WSC error codes can be displayed by calling SioError (refer to sioerror.c and paint.c) in GUI mode applications or SayError (see SayError.c) in console mode applications. Each sample program contains examples of error processing.
SioEvent, SioEventChar, and SioEventWait will block until the specified event occurs. If a call to SioEvent, SioEventChar, or SioEventWait is placed in a thread, then the thread will block but the application calling the thread will not.
See the EVNT_PGM.PAS and EASY_PGM.PAS example programs.
A "virtual" serial port is COM port that appears to be a real RS232 serial port to the Windows API (and thus to WSC), but is in reality a COM port emulator.
The two most common virtual ports are those created for USB/serial port converters and Blue Tooth. WSC does not work with USB ports directly but will work with most USB-to-serial port converters as well as with Bluetooth serial.
More information about Virtual serial ports can be found in Section 2.12 of the WSC User's Manual (WSC_USR). (http://www.marshallsoft.com/wsc_usr.htm#Section_2.12 )
The Windows Standard Serial Communications (WSC) library is written in ANSI C (like Windows itself). In C, strings are zero terminated. Note the manner in which strings are passed to WSC functions.
var
Code : Integer;
BufferLen : Integer;
BufferStr : String;
BufferPtr : PChar;
begin
BufferStr := 'AT' + Chr(13);
BufferLen := Length(BufferStr);
StrPCopy(BufferPtr, BufferStr);
{pass buffer to WSC function}
Code := SioPuts(COM1, BufferPtr, BufferLen);
end;
Buffers can also be converted to Delphi strings with StrPas. For example:
Text := StrPas(BufferPtr);
Copy WSC32.PAS (if running 32-bit Delphi), WSC16.PAS (if running 16-bit Delphi on Win 95/98/Me/Vista), or WSC16NT.PAS (if running 16-bit Delphi on Win NT/2000/XP) to the same directory (folder) as the application program to which you want to add WSC code. You will find these files in the APPS directory (folder) created when you ran SETUP, usually C:\WSC4D\APPS.
For 32-bit Delphi, add wsc32 (wsc32uc for Delphi .NET) and keycode to your "uses" clause in your source program (*.PAS). For example,
uses
wsc32, keycode, ...
You can leave 'keycode' out above if you put your numerical keycode value (found in keycode.pas) directly into the call to SioKeyCode. Also add wsc32 to your project file (*.DPR). For example,
uses
wsc32 in 'wsc32.pas', ...
{pass the key code}
Code := SioKeyCode(123456789) < 0 then
Applications written with Delphi link with the same identical DLL's as for applications written in all other supported languages, such as C/C++ and Visual Basic.
The first release of Borland Delphi (version 1) generated Win16 code. Therefore, applications written using Delphi 1 will access WSC16.DLL.
One very significant limitation of Delphi 1 is that strings are limited to 255 bytes.
Before compiling any of the example programs with Delphi 1, change the uses clause in all source files from wsc32.pas to wsc16.pas (or wsc16nt.pas if running Win NT/2000/XP).
Delphi version 2 and above generates Win32 code. Therefore, applications written using Delphi 2 will access WSC32.DLL. Strings can be up to 2GB rather than 255 bytes as in Delphi 1.
Delphi 2 seems to have a problem with some of the PChar string functions. Although the default is "large strings", some of the string functions (such as StrPCopy) copy only 255 bytes. The MYSTRING.PAS unit contains a replacement unit to use instead of StrPCopy.
Delphi 3 also has some problems with PChar string functions such as StrPCopy. See the above section.
There are no known Delphi problems impacting our example programs in Delphi version 4 and above. Applications written using Delphi 4 through Delphi 6 will access WSC32.DLL.
Beginning in Delphi 7, the filename of a unit must match the unit name. Applications written using Delphi 7 will access WSC32.DLL.
Delphi 2005, 2006 and 2007 are Borland's latest Delphi products with support for both Win32 and the Microsoft .NET Framework. Application programs written using Delphi 2005, 2006, and 2007 will access WSC32.DLL.
When loading Win32 Delphi projects with Delphi 2005, 2006 and 2007, a window entitled "Project Upgrade" will be displayed:
This project must be upgraded before it can be opened. Please select which project type you wish to target:
( ) Delphi for .NET
( ) Delphi for Win32
Choose "Delphi for Win32" for all projects except "simple_project.bdsproj", which is a Delphi for .NET project.
The example programs are compiled from the Delphi development environment using the provided Delphi project files (*.DPR).
The example programs will compile and run with any version of Delphi. They have each been tested using Delphi 1 through Delphi 5 and Delphi 2005 through Delphi 2007.
Respond with "OK" to the message "Cannot find resource file..." and it will be properly rebuilt.
See Section 4 "Example Programs" for more details on each of the example programs.
WSC4D may also be used with "Borland Pascal for Windows".
WSC is written in standard ANSI C (WSC16.C and WSC32.C), and has been compiled using Microsoft Visual C/C++. The Win32 version is compiled with the STDCALL and DECLSPEC compiler keywords. Source code for the WSC library can be purchased at the same time as a WSC developer license is purchased.
WSC may also be compiled using Borland C/C++ or Watcom C/C++ compilers. If you recompile WSC32.C using Borland or Watcom compilers, the resulting DLL can only be used by applications compiled with the same compiler, unless the STDCALL and DECLSPEC keywords are specified.
For more information on the C/C++ version of WSC, download the latest version of WSC4C from our web site at http://www.marshallsoft.com/wsc4c.htm
The example programs are designed to demonstrate the various capabilities of WSC4D. The best way to become familiar with WSC4D is to study and run the example programs.
Most of the example programs are compiled from the Delphi development environment using the provided Delphi project files (*.DPR). Recall that Delphi 1 generates a Win16 application while Delphi 2 and above generates a Win32 application. If you will be compiling with Delphi 1 (which generates 16-bit code), change the uses clause in all source files from wsc32.pas to wsc16.pas (or wsc16nt.pas if running Win NT/2000/XP).
Most of the example programs can be compiled with any version of Delphi. However, some require 32-bit (Delphi 2) or above.
The VER ("WSC Version") example program displays the WSC version number. This is the first program to compile and build since it verifies that WSC32.DLL (or WSC16.DLL) is installed properly.
The project files are:
VER_PRJ.DPR : Project file.
VER_PGM.PAS : Program file.
VER_PGM.DFM : Delphi Form file.
EASY is a very simple communications program using WSC4D. Everything that is typed on the keyboard is sent to the serial port, and everything incoming from the serial port is displayed on the screen.
The easiest way to test EASY is to connect to a modem. Typing 'AT' should result in an 'OK' being displayed.
A null-modem cable can also be used to connect two computers together with their serial ports. Run EASY on both machines. Whatever is typed on one machine will be displayed on the other.
The project files are:
EASY_PRJ.DPR : Project file.
EASY_PGM.PAS : Program file.
EASY_PGM.DFM : Delphi Form file.
The SELF ("selftest") program performs a serial port I/O functionality test. Either a pair of ports on the same computer (using a null modem cable) or a single port (using a loopback adapter) can be tested.
Refer to LOOPBACK.TXT for an explanation of how to make a loopback adapter (without tools!).
The project files are:
SELF_PRJ.DPR : Project file.
SELF_PGM.PAS : Program file.
SELF_PGM.DFM : Delphi Form file.
MODEM is similar to EASY, but with enhanced capability. It can set flow control (hardware, software, or none), DTR line (set or clear), RTS line (set or clear), display the transmit and receive queue sizes, detect a break signal and detect changes in DSR and CTS. It can also check for various line errors (parity error, framing error, data overrun, receive queue overflow, and transmit buffer full).
The project files are:
MODM_PRJ.DPR : Project file.
MODM_PGM.PAS : Program file.
MODM_PGM.DFM : Delphi Form file.
TERM is a simple terminal emulator suitable for calling up a BBS and downloading or uploading files using XMODEM or YMODEM. The TERM program uses MIO.DLL for modem control commands, ASD.DLL for the ASCII protocol, and XYM.DLL for XMODEM & YMODEM protocol.
Selecting 'Dial' from the menu bar will result in a pop-up dialog requesting the phone number to dial. Once entered, the number is dialed, and the program will wait for up to 60 seconds for the 'CONNECT' string from the modem. This wait can be terminated at any time by choosing 'BREAK' on the menu bar.
Once logged on, files can be uploaded or downloaded by selecting 'Send' or 'Receive' from the menu bar. To abort a file transfer, choose 'BREAK' from the menu bar then type a series of Ctrl-X (^X) characters from the keyboard.
The project files are:
TERM_PRJ.DPR : Project file.
TERM_PGM.PAS : Program file.
TERM_PGM.DFM : Delphi Form file.
The FINDER program searches for a connected modem. Your modem must be connected to one of COM1, COM2, COM3, or COM4, and must be turned on.
The project files are:
FIND_PRJ.DPR : Project file.
FIND_PGM.PAS : Program file.
FIND_PGM.DFM : Delphi Form file.
The EVENT example program is similar to EASY, except that it uses SioEvent to wait for incoming serial data. EVENT requires 32-bit Delphi.
The project files are:
EVNT_PRJ.DPR : Project file.
EVNT_PGM.PAS : Program file.
EVNT_PGM.DFM : Delphi Form file.
The DEVICE example program is designed to send a text string to a serial device. A carriage return is appended to the end of the string.
The DEVICE program can be used to send commands to serial devices which use ASCII commands, such as bar code readers, XY-plotters, etc. The project files are:
DVCE_PRJ.DPR : Project file.
DVCE_PGM.PAS : Program file.
DVCE_PGM.DFM : Delphi Form file.
The MESSAGE program is similar to EASY, except that rather than using a timer as in EASY, it uses the SioMessage function to request that WSC send a "Left Button Down" Windows message whenever any new serial data is available.
The MESSAGE example program requires Delphi 2 or above. The project files are:
MESS_PRJ.DPR : Project file.
MESS_PGM.PAS : Program file.
MESS_PGM.DFM : Delphi Form file.
XMS (XMODEM Send) and XMR (XMODEM Receive) are programs that send and receive files using the XMODEM protocol. XMS and XMR must be edited before compiling.
See XMODEM.TXT for more information on the XMODEM protocol. The project files are:
XMS_PRJ.DPR, XMR_PRJ.DPR : Project files.
XMS_PGM.PAS, XMR_PGM.PAS : Program files.
XMS_PGM.DFM, XMS_PGM.DFM : Delphi Form files.
YMS (YMODEM Send) and YMR (YMODEM Receive) are programs that send and receive files using the YMODEM protocol. YMS and YMR must be edited before compiling.
See YMODEM.TXT for more information on the YMODEM protocol. The project files are:
YMS_PRJ.DPR, YMR_PRJ.DPR : Project files.
YMS_PGM.PAS, YMR_PGM.PAS : Program files.
YMS_PGM.DFM, YMS_PGM.DFM : Delphi Form files.
The RS485 example console mode program operates like SIMPLE, except that it assumes an RS485 port. RTS is set before transmitting data, and cleared after the last bit of the last byte has been sent.
The project files are:
RS485PRJ.DPR : Project file.
RS485PGM.PAS : Program file.
Rs485PGM.DFM : Delphi Form file.
The SIMPLE example program is the Delphi.NET (2005/ 2006/2007) equivalent of EASY, and can only be loaded using Delphi.NET 2005, 2006 or 2007.
The project files are:
SIMPLE_PROJECT.BDSPROJ : Project file.
SIMPLE_WINFORM.PAS : Program file.
NOTE: Version 2.0 was the first Delphi version of WSC.
Version 2.0: February 17, 1997.
Version 2.1: June 9, 1997.
Version 2.2: October 20, 1997.
Version 2.3: August 19, 1998
Version 2.4: June 7, 1999
Version 3.0: September 1, 2000
Version 3.1: May 8, 2001.
Version 3.2: August 16, 2002.
Version 4.0: December 12, 2003.
Version 4.1: August 12, 2004
Version 4.2: March 3, 2006.
Version 4.3: September 28, 2007.