| Home > Geek stuff > Serial Communications > Sample Code | http://ke3vin.org/geek/serial/code/ |
| Last updated: 270 day(s) ago (Sun Feb 24 15:01:33 2008) | Fri Nov 21 01:40:15 2008 |
Mirrored from http://www.cpcug.org/user/clemenzi/technical/Languages/SerialIO.htm
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'spage provides free ActiveX serial componentsfor 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 #1End 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 = 12End SubPrivate Sub Form_Unload(Cancel As Integer) If ComPort_ActiveXCtl.PortOpen = True Then ComPort_ActiveXCtl.PortOpen = False End IfEnd SubPrivate Sub ComPort_ActiveXCtl_OnComm() Select Case ComPort_ActiveXCtl.CommEvent Case comEvReceive ' Received RThreshold # of chars. Test_UIEdit = ComPort_ActiveXCtl.Input End SelectEnd SubThe trick is to know that ControlName_OnComm() is calledwhen RThreshold is not zero.Unfortunately, this doesn't work so well because barcodes came in variouslength and RThreshold=12 only works with 12-character barcodes.With these modification, any length barcode will work.
' Global Variables for the Barcode ScannerDim Global_Barcode As StringDim Start_Time As Date'********************************************' Start of Barcode reader codePrivate 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 characterEnd SubPrivate 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 SelectEnd SubPrivate Sub Form_Timer() TimerInterval = 0 ' Disable the timer Start_Time = 0 Barcode_UIEdit = Global_Barcode Global_Barcode = "" Barcode_UIEdit_ChangeEnd SubPrivate Sub Form_Unload(Cancel As Integer) If ComPort_ActiveXCtl.PortOpen = True Then ComPort_ActiveXCtl.PortOpen = False End IfEnd 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 thatboth 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 variousRS-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 bioscomto access ports.
Microsoft provides aSimple 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 thedata 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 disconnectsis the Baud Rate.Parity, StopBits and the like are reseteach time the connection is opened.Unfortunately,both SetCommState and SetCommConfig are extremely slow.As a result, it is not practical to disconnect the comportbetween uses.
Warning
This site is Lynx friendly!
![]() |
![]() |
![]() |
| Copyright © 1995-2005 Kevin P. Inscoe | Viewable With Any Browser |
This website and all original artwork and material is © copyright 2005 Kevin P. Inscoe. Other material is used under the "Fair Use" provisions of United States of America Copyright law, and all rights remain with the original copyright holders.