Under DOS, it was possible to directly access these ports. However, Windows 95 and above forbid direct hardware access. Therefore, this page exists to help get around windows.
Generic References
' Open the serial port MSComm1.CommPort = 2 ' Set the port number MSComm1.Settings = "56000,N,8,1" ' Set UART parameters MSComm1.PortOpen = True ' Required, might lock port MsComm1.Output = "Text string" ' Send data Buffer$ = Buffer$ & MSComm1.Input ' Read data
Visual Basic 6.0
Visual Basic Programmers Guide to Serial Communications by Richard Grier. Richard's page provides free ActiveX serial components for those that don't have MSComm32.ocx.
VB 6.0 provides VBTerm, a sample terminal emulation application under ..\samples\VB98\MSCOMM.
You can open a port as a file. This example, from comp.lang.basic.visual.misc, opens the printer
Open "LPT1" For Output As #1 Print #1, "Text" & chr$(ascii char) Close #1
MS Access 97 SR-2
On my system, the following code causes Access to hang.
Private Sub ReadPort_UIButton_Click() Dim temp As String Open "com1:" For Input As #1 Len = 3 Input #1, temp ' This line hangs Close #1 End SubI tested several variations with the same result
MS Access 97 SR-2 - Works
Private Sub OpenPort_UIButton_Click() ComPort_ActiveXCtl.CommPort = 1 ' 9600 baud, no parity, 8 data, and 1 stop bit. ComPort_ActiveXCtl.Settings = "9600,N,8,1" ComPort_ActiveXCtl.InputLen = 0 ' Read entire buffer ComPort_ActiveXCtl.PortOpen = True ' Opens the port ComPort_ActiveXCtl.RThreshold = 12 End Sub Private Sub Form_Unload(Cancel As Integer) If ComPort_ActiveXCtl.PortOpen = True Then ComPort_ActiveXCtl.PortOpen = False End If End Sub Private Sub ComPort_ActiveXCtl_OnComm() Select Case ComPort_ActiveXCtl.CommEvent Case comEvReceive ' Received RThreshold # of chars. Test_UIEdit = ComPort_ActiveXCtl.Input End Select End SubThe trick is to know that ControlName_OnComm() is called when RThreshold is not zero. Unfortunately, this doesn't work so well because barcodes came in various length and RThreshold=12 only works with 12-character barcodes. With these modification, any length barcode will work.
' Global Variables for the Barcode Scanner Dim Global_Barcode As String Dim Start_Time As Date '******************************************** ' Start of Barcode reader code Private Sub Form_Load() Start_Time = 0 Global_Barcode = "" ComPort_ActiveXCtl.CommPort = 1 ' 9600 baud, no parity, 8 data, and 1 stop bit. ComPort_ActiveXCtl.Settings = "9600,N,8,1" ComPort_ActiveXCtl.InputLen = 0 ' Read entire buffer ComPort_ActiveXCtl.PortOpen = True ' Opens the port ComPort_ActiveXCtl.RThreshold = 1 ' Call **_OnComm ' for each character End Sub Private Sub ComPort_ActiveXCtl_OnComm() Select Case ComPort_ActiveXCtl.CommEvent Case comEvReceive ' Received RThreshold # of chars. If Start_Time = 0 Then Start_Time = Timer Else If Timer - Start_Time > 200 Then ' in case of failure Form_Timer Exit Sub End If End If Global_Barcode = Global_Barcode & ComPort_ActiveXCtl.Input TimerInterval = 80 ' call Form_Timer 80 ms ' after last character End Select End Sub Private Sub Form_Timer() TimerInterval = 0 ' Disable the timer Start_Time = 0 Barcode_UIEdit = Global_Barcode Global_Barcode = "" Barcode_UIEdit_Change End Sub Private Sub Form_Unload(Cancel As Integer) If ComPort_ActiveXCtl.PortOpen = True Then ComPort_ActiveXCtl.PortOpen = False End If End Sub ' End of Barcode reader code '********************************************Notes:
Delphi
C++ Builder
_bios_serialcom
#include <bios.h> unsigned temp; // Open serial port at 1200 baud, 8 data bits, // No parity, 1 stop bit temp = _bios_serialcom(_COM_INIT, 0, _COM_CHR8 | _COM_NOPARITY | _COM_STOP1 | _COM_1200); temp = _bios_serialcom(_COM_RECEIVE, 0, 0); // Read a character temp = _bios_serialcom(_COM_SEND , 0, '*'); // Write a characterIt is unclear which compilers provide bios.h. On 9-24-01, I found references that both Microsoft Visual C++ and Watcom C++ also claim to support _bios_serialcom(). However, a search of my Visual C++ 6 system found neither bios.h nor _bios_serialcom(). Therefore, I assume that Microsoft has dropped the support.
At any rate, I will NOT provide copies of bios.h to anyone.
Virtual Integrated Design provides various RS-232 circuits and free software examples.
Accessing the RS232 Port in DOS using BIOS.H functions provides a summary of the allowed options/contants. There is also a program showing how to use bioscom to access ports.
Microsoft provides a Simple Example Using _bios_serialcom(). The article states that this interface tends to loose data. In order to improve the reliability, the comport needs to call an interrupt routine which moves the data to a buffer. This was easy under DOS, but generally not allowed under Windows.
Windows API
Use CreateFile to open a handle to a communications resource, such as com1 or lpt1.
Test the returned handle to verify that the port is not locked by another process.
Use GetCommState to determine the current settings, SetCommState to change the settings.
You can use BuildCommDCB to pass common parameters (baud, parity, etc.) to the DCB as a string. But you'll still need SetCommState to actually change the settings.
See the DCB help for the supported constants.
Some additional commands - TransmitCommChar, PurgeComm, FlushFileBuffers
Microsoft Platform SDK - Communication Overview, Communication Functions
The only property which is remembered between disconnects is the Baud Rate. Parity, StopBits and the like are reset each time the connection is opened. Unfortunately, both SetCommState and SetCommConfig are extremely slow. As a result, it is not practical to disconnect the comport between uses.
Warning