Windows Standard
Serial Communications
for PowerBASIC
Programmer's Manual
(WSC_4PB)
Version 4.3
October 2, 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
FAX : 1.256.880.0925
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.7 Updates
1.8 Keycode (License Key)
2.1 Dynamic Link Libraries3 Compiler Issues
2.2 Win32 STDCALL and DECLSPEC
2.3 Using Threads
2.4 Limitations on COM Ports
2.5 Waiting for New Serial Data
2.6 Error Display
2.7 SioEvent Logic
2.8 Virtual Serial Ports
2.9 Adding WSC to a PowerBASIC program
2.10 PowerBASIC Declaration Files
3.1 PowerBASIC Console Compiler4 Example Programs
3.2 PowerBASIC DLL Compiler
3.3 PowerBASIC Windows Compiler
3.4 Compiling WSC Source
3.5 Compiling Example Programs
4.1 WSCVER5 Revision History
4.2 SIMPLE
4.3 SIMPLE2
4.4 SELFTEST
4.5 FINDER
4.6 DIALER
4.7 THREAD
4.8 DEVICE
4.9 XMS and XMR
4.10 YMS and YMR
4.11 ECHOPORT
4.12 RS485
4.13 PORTS
4.14 HELLO
4.15 EventW
The Windows Standard Serial Communications Library for PowerBASIC (WSC4PB) is a component library of functions providing the capability to quickly write serial communication applications in PowerBASIC. The WSC component library uses the standard Windows API (Application Programmer's Interface) to communicate with any device connected to a serial port.
A straight forward 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.
WSC4PB includes several example programs demonstrating serial communication functions and has been tested on multiple computers running Windows 95/98, Windows Me, Windows NT4, Windows 2000, Windows XP, and Vista. The WSC4PB library supports and has been tested with the PowerBASIC Console Compiler (PBCC), PowerBASIC DLL Compiler (PBDLL), and PowerBASIC Windows Compiler (PBWIN).
The WSC DLL (WSC32.DLL) can be used from any 32-bit language (C/C++, Visual C++ .NET, Visual C#, Visual Basic, VB.NET, Delphi, 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), Delphi (WSC4D), Visual Basic (WSC4VB), 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 are:
Also see WSC versions for other supported languages:
Several PowerBASIC example programs with full source code are included. Refer to Section 4 for more details on each of the example programs.
DEVICE : Sends text string to serial device.
DIALER : Modem dialer.
ECHOPORT : Multi-threaded console mode program echoes all input.
FINDER : Finds a modem connected to one of your serial ports.
HELLO : PowerBASIC Windows compiler (PBWIN) version of WSCVER.
PORTS : PowerBASIC DLL Compiler (PBDLL) example program finds ports.
RS485 : RS485 example terminal program.
SELFTEST : Performs COM port functionality testing.
SIMPLE : A simple terminal emulator.
SIMPLE2 : Same as SIMPLE, but uses WSC32PTR.PBI.
THREAD : Same as SIMPLE, but implemented using a Win32 thread.
WSCVER : Display WSC version number.
XMR : Receives files via XMODEM protocol.
XMS : Sends files via XMODEM protocol.
YMR : Receives files via YMODEM protocol.
YMS : Sends files via YMODEM protocol.
EventW : Terminal program that uses SioEventWait to efficiently wait.
The complete set of documentation consists of four manuals in three formats. This is the first manual (WSC_4PB) in the set.
The WSC_4PB Programmer's Manual is the language specific (PowerBASIC) manual. All language dependent programming issues are discussed in this manual. Information needed to compile your programs in a PowerBASIC 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/wsc4pb.htm
The following example program segment displays the WSC registration string.
$INCLUDE "WSC32.PBI"
$INCLUDE "KEYCODE.PBI"
FUNCTION PbMain() AS LONG
Dim Code As Integer
Dim Temp As Asciiz * 51
Code = SioKeyCode(%WSC_KEY_CODE)
IF Code < 0 THEN
PRINT "SioKeyCode fails: Check value of %WSC_KEY_CODE"
EXIT FUNCTION
END IF
' display registration string
Code = SioGetReg(Temp, 50)
IF Code > 0 THEN
PRINT Left$(Temp, Code)
END IF
END FUNCTION
Refer to section 4.0 for complete examples with source.
Uninstalling WSC4PB is very easy. WSC4PB does not modify the registry. First, delete the WSC4PB project directory created when installing WSC4PB. Second, delete WSC32.DLL from your Windows directory, typically C:\WINDOWS for Windows 95/98/Me/XP 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 WSC4PB 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 (WSCxxxx.LIC) that can be used to update 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 $30 if ordered within one year of the original purchase (or previous update). After one year, licenses can be updated for $55. Updates to the source code can be purchased for $40.
Note that the registered DLL's do not expire.
WSC16.DLL and WSC32.DLL each has a keycode encoded within them. Your keycode is a 9 or 10 digit decimal number (unless it is 0), and will be found in the file KEYCODE.PBI. The keycode for the evaluation (shareware) version is 0. You will receive a new keycode and a set of new DLL's when registering. The KEYCODE is passed to SioKeyCode.
If you get an error message (value -108) when calling SioKeyCode, it means that the keycode in your application does not match the keycode in the 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.
The Windows Standard Serial Communications Library (WSC) has been tested on multiple computers running Windows 95/98/Me/XP, and Windows NT/2000.
The WSC4PB serial communication library component (WSC32.DLL) is implemented as a Win32 dynamic link libraries (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 VBX 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".
WSC32 is written in ANSI C and is compiled using the STDCALL and DECLSPEC keywords. This means that WSC4PB 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 the proper declaration file is used.
WSC4PB is thread safe, and can be used from any Windows application capable of using threads. Refer to the THREAD and ECHOPORT example programs, which demonstrate the use of threads. ECHOPORT also demonstrates the use of the WIN32 Sleep() function.
The necessary Win32 thread functions are declared in the PowerBASIC INCLUDE file "WIN32API.INC"
The 32-bit version of WSC4PB (WSC32.DLL) can use any port from COM1 to COM256, provided that the port is known to Windows 95/98/NT/2000/Me/XP and there is physical hardware present.
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 receive incoming serial data. SioMessage can be used in PBWIN programs since they have a message loop. SioGetc and SioGets can be called directly. Note that if there is no input data available, SioGetc returns WSC_NO_DATA while SioGets returns zero. Also see section 2.7.
The error message text associated with WSC error codes can be displayed by calling SayError (refer to Errors32.bas). 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.
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 will work with USB to serial port converters and 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 )
In order to add WSC to a PowerBASIC program, add
$INCLUDE "WSC32.PBI"
$INCLUDE "KEYCODE.PBI"
after any other $INCLUDE statements. Then add
Code = SioKeyCode(%WSC_KEY_CODE)
If Code < 0 Then
'... display error code to user here...
EXIT FUNCTION
END IF
as the fist executed WSC function.
WSC constants are defined and all WSC functions are declared in the file WSC32.PBI and is included in all of the example programs except for SIMPLE2.BAS.
Two of the WSC functions (SioPuts and SioGets) have alternate declarations in the file WSC32PTR.PBI. The alternate declarations allow greater flexibility in how strings are handled in your PowerBASIC program. For an illustration, see the example program SIMPLE2.BAS.
The WSC4PB library supports and has been tested with the PowerBASIC Console Compiler (PBCC), PowerBASIC DLL Compiler (PBDLL), and PowerBASIC Windows Compiler (PBWIN).
Please examine the WSC32.PBI file. Note that %COM1 has numerical value zero, not one. The user must assume the responsibility for passing the correct information when calling WSC4PB functions
Most of the example programs are written for the PowerBASIC Console Compiler (PBCC). Be sure to read the comments at the beginning of each example program. Also see section 4.0 "Example Programs" below.
WSC functions can be called from programs written for the PowerBASIC DLL Compiler (PBDLL). The same DLL (WSC32.DLL) is used for both PowerBASIC and Visual Basic. WSC functions are defined in WSC32.PBI for PowerBASIC and in WSC32.BAS for Visual Basic. The Visual Basic version can be downloaded from www.marshallsoft.com/wsc4vb.htm See the PORTS.BAS example program.
WSC functions can be called from programs written for the PowerBASIC Windows Compiler (PBWIN). There are no differences in how WSC functions are called in programs written in any of the Windows PowerBASIC compilers.
WSC32.DLL is written in standard ANSI C (WSC32.C), and has been compiled using Microsoft Visual C/C++ 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.
WSC32.C 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 WSC32.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
Compiling programs using the PowerBASIC Console Compiler is very easy. For example, to compile SIMPLE.BAS, type
PBCC SIMPLE.BAS
at the command prompt. If your program requires access to any Windows API functions, you must include the path on the command line. For example, assuming that you have installed PBCC at C:\PBCC, to compile the ECHOPORT.BAS program, type (the "-MT" is for Multi-Tasking support)
PBCC ECHOPORT.BAS -IC:\PBCC\WINAPI -MT
WSC4PB functions can also be called from PBDLL programs. See the PORTS.BAS example.
The example programs are designed to demonstrate the various capabilities of WSC4PB. The best way to become familiar with WSC4PB is to study and run the example programs.
WSCVER ("WSC Version") is a PBCC example program displays the WSC version number. This is the first program to compile and build since it versifies that WSC32.DLL is installed properly. Also see the HELLO.BAS (PBWIN) example program.
SIMPLE is a very simple serial communications program using WSC4PB. 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 SIMPLE 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 SIMPLE on both machines. Whatever is typed on one machine will be displayed on the other.
SIMPLE2 is the same program as SIMPLE, except that it uses WSC32PTR.PBI rather than WSC32.BPI, in which alternate definitions of SioPuts and SioGets are used.
SELFTEST 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 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.
FINDER takes no arguments. After connecting your modem to one of your serial ports, type
FINDER
The DIALER programs dials up a specified host, such as a Bulletin Board System (BBS).
The THREAD example program is similar to SIMPLE, except that it uses a Win32 thread. Note that the PowerBASIC thread function blocks awaiting serial input.
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.
XMS (XMODEM Send) and XMR (XMODEM Receive) are programs that send and receive files using the XMODEM protocol.
YMS (YMODEM Send) and YMR (YMODEM Receive) are programs that send and receive files using the YMODEM protocol.
The ECHOPORT program echoes characters incoming on COM1 and COM2 using threads. To use,
The RS485 program is a simple terminal emulator using a RS485 port in which RTS is raised before transmitting and dropped afterwards.
The PORTS example program demonstrates how to call DLL's created by the PowerBASIC DLL compiler from (32-bit) Microsoft Visual Basic. PORTS.BAS is compiled by
PBDLL PORTS.BAS
You must also compile the Visual Basic program PORTS.FRM by loading PORTS.VBP in Visual Basic version 4.0 or above.
HELLO is a PowerBASIC for Windows (PB/WIN) example program similar to WSCVER. PB/WIN is required.
EventW is a PBCC terminal program. It works exactly like SIMPLE and SIMPLE2 except that SioEventWait is used to efficiently wait for new incoming data.
NOTE: Version 2.3 was the first PowerBASIC version.
Version 2.3: August 3, 1998.
Initial release of PowerBASIC version.
Version 2.4: May 24, 1999
Version 3.0: September 8, 2000
Version 3.1: May 15, 2001.
Version 3.2: August 8, 2002.
Version 4.0: December 2, 2003.
Version 4.1: August 12, 2004
Version 4.2: March 28, 2006.
Version 4.3: October 2, 2007.