You are on page 1of 8

Using ActiveComport Serial Port Toolkit with Visual Basic 5.x/6.

x
ActiveComport is a software development kit (SDK) that enables the user to communicate to a device over a serial interface. Such a device can be: a weight indicator, a modem, a scanner, or any other device that is equiped with a serial port. It can even be another PC, connected via a NULL modem cable. ActiveComport features the following: Direct COM port support (like 'COM1'), TAPI (Windows Telephony Device) support (like 'Standard 56000 bps Modem'), support for RS-232/RS422/RS485, up to 256 simultaneous ports, support for all types of Hayes compatible modems, support for serial cable, USB cable or Bluetooth connections, support for GSM/GPRS modems, support for Virtual COM ports (i.e. COM ports redirected through the network), hardware flow control (RTS/CTS, DTR/DSR), software flowcontrol (XON/XOFF), configurable baudrate/parity/stopbits, full buffered data transfer, text/binary data transfer. ActiveComport can be well integrated into Visual Basic environments. This document describes how the ActiveComport Toolkit can be integrated into Visual Basic 5.x/6.x projects.

Step 1: Download and install the ActiveComport Toolkit


Download the ActiveComport Toolkit from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new Visual Basic project


Launch 'Microsoft Visual Basic' from the Start menu, and choose 'New' from the 'File Menu'. The 'New Project' dialog appears. Select 'Standard Exe' and click 'OK':

(Click on the picture to enlarge)

Step 3: Refer to the ActiveComport Library and create the objects


A new Project is created, with a blank form. First, you must add a reference to ActiveComport in the project to be able to use the object. To do so, choose 'References...' from the 'Project' menu. In the 'References' dialog that pops up, enable the 'ActiveComport 2.2 Type Library' reference as shown in the following picture:

(Click on the picture to enlarge) Click 'OK' to close the 'References...' dialog. Then, select the Project form and choose 'View Code' from the context menu:

(Click on the picture to enlarge) On top of your code, declare the following object:
Public objComport As ACOMPORTLib.ComPort

Step 4: Create the object


From the Code window, select 'Form'. The Private Sub 'Form_Load()' will be displayed now. In the 'Form Load' function, create the object in the following way:
Set objComport = CreateObject("ActiveXperts.ComPort")

Step 5: Send an AT command to a connected Hayes compatible modem


You can now send and/or receive data to and/or from a serial device. The following code shows how to query a modem:
' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!! ' DO NOT FORGET: ADD A REFERENCE TO THE ACTIVECOMPORT LIBRARY FROM THE PROJECT->REFERENCE MENU ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!! Public lTimerID As Long Public objComport As ACOMPORTLib.ComPort Private Sub buttonSubmit_Click() textReceived = "" objComport.WriteString (textSend) GetResult End Sub Private Sub buttonView_Click() If FileExists(textLogfile.Text) = True Then Shell "notepad " + textLogfile.Text, vbNormalFocus End If End Sub Private Sub CheckDTR_Click() objComport.RaiseDTR (CheckDTR.Value) GetResult End Sub Private Sub CheckRTS_Click() objComport.RaiseRTS (CheckRTS.Value) GetResult

End Sub Public Function FileExists(sFileName As String) As Boolean FileExists = CBool(Len(Dir$(sFileName))) And CBool(Len(sFileName)) End Function Private Sub Form_Load() textSend = "AT" Set objComport = CreateObject("ActiveXperts.ComPort") objComport.ComTimeout = 100 For i = 0 To objComport.GetDeviceCount() - 1 comboDevice.AddItem (objComport.GetDevice(i)) Next comboDevice.AddItem comboDevice.AddItem comboDevice.AddItem comboDevice.AddItem comboDevice.AddItem comboDevice.AddItem comboDevice.AddItem comboDevice.AddItem "COM1" "COM2" "COM3" "COM4" "COM5" "COM6" "COM7" "COM8"

comboDevice.ListIndex = 0 comboSpeed.AddItem comboSpeed.AddItem comboSpeed.AddItem comboSpeed.AddItem comboSpeed.AddItem comboSpeed.AddItem comboSpeed.AddItem comboSpeed.AddItem comboSpeed.AddItem comboSpeed.AddItem comboSpeed.AddItem comboSpeed.AddItem comboSpeed.AddItem comboSpeed.AddItem comboSpeed.AddItem comboSpeed.AddItem "Default" "110" "300" "600" "1200" "2400" "4800" "9600" "14400" "19200" "38400" "57600" "64000" "115200" "128000" "256000"

comboSpeed.ListIndex = 0 comboHWFlowControl.AddItem "Default" comboHWFlowControl.AddItem "Disable" comboHWFlowControl.AddItem "Enable" comboHWFlowControl.ListIndex = 0 comboSWFlowControl.AddItem "Default" comboSWFlowControl.AddItem "Disable" comboSWFlowControl.AddItem "Enable"

comboSWFlowControl.ListIndex = 0 comboDataFormat.AddItem "Default" comboDataFormat.AddItem "8,n,1" comboDataFormat.AddItem "7,e,1" comboDataFormat.ListIndex = 0 textReceived = "" EnableControls End Sub Private Sub buttonOPEN_Click() objComport.Device = comboDevice.List(comboDevice.ListIndex) If (comboSpeed.Text = "Default") Then objComport.BaudRate = 0 Else objComport.BaudRate = comboSpeed.Text End If objComport.LogFile = textLogfile.Text objComport.HardwareFlowControl = comboHWFlowControl.ListIndex objComport.SoftwareFlowControl = comboSWFlowControl.ListIndex If (comboDataFormat.ListIndex = 0) Then objComport.DataBits = objComport.asDATABITS_DEFAULT objComport.StopBits = objComport.asSTOPBITS_DEFAULT objComport.Parity = objComport.asPARITY_DEFAULT End If If (comboDataFormat.ListIndex = 1) Then objComport.DataBits = objComport.asDATABITS_8 objComport.StopBits = objComport.asSTOPBITS_1 objComport.Parity = objComport.asPARITY_NONE End If If (comboDataFormat.ListIndex = 2) Then objComport.DataBits = objComport.asDATABITS_7 objComport.StopBits = objComport.asSTOPBITS_1 objComport.Parity = objComport.asPARITY_EVEN End If objComport.Open GetResult EnableControls End Sub

Private Sub buttonClose_Click() objComport.Close GetResult EnableControls End Sub Private Sub Form_Unload(Cancel As Integer) Timer1.Enabled = False End Sub Private Sub EnableControls() Dim bOpened bOpened = objComport.IsOpened CheckDTR.Enabled = bOpened CheckRTS.Enabled = bOpened checkCTS.Enabled = bOpened checkDCD.Enabled = bOpened checkRI.Enabled = bOpened checkDSR.Enabled = bOpened buttonOpen.Enabled = bOpened + 1 buttonClose.Enabled = bOpened buttonSubmit.Enabled = bOpened Timer1.Enabled = bOpened End Sub Private Sub linkErrorCodes_Click() Shell "Explorer http://www.activexperts.com/support/errorcodes/Index.asp" End Sub Private Sub linkWebsite_Click() Shell "Explorer http://www.activexperts.com" End Sub Private Sub Timer1_Timer() Dim strString strString = objComport.ReadString If (strString <> "") Then textReceived = textReceived & strString & vbCrLf End If checkDCD.Value = Abs(objComport.QueryDCD) checkCTS.Value = Abs(objComport.QueryCTS) checkDSR.Value = Abs(objComport.QueryDSR) checkRI.Value = Abs(objComport.QueryRI)

End Sub Private Sub GetResult() If objComport.LastError = 0 Then textResult.Caption = "SUCCESS" Else textResult.Caption = "ERROR " & objComport.LastError & " ( " & objComport.GetErrorDescription(objComport.LastError) & " )" End If End Sub

There are many working samples included with the product. You can also find them on the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/serial-port-component.

2012 ActiveXperts Software B.V. All rights reserved. Site Map Contact Us Terms of Use Privacy Policy

Hola todos, utilizar el mscomm de visual basic 6.0 no es dificil Primero se debe incluir el componente en el proyecto. Si el visual basic est en espaol, se va a "Proyecto -> Componentes" (o se puede presionar CTRL T) y en la ventana que se abre se selecciona "Microsoft comm control 6.0", con esto sale un icono con la forma de un telefono en la barra de herramientas, se arrastra el control al formulario y ya est. Configuracin basica del puerto (si el control tiene por nombre MSCOMM1) with MSCOMM1 .CommPort = 1 'Numero del puerto .Settings = "9600,N,8,1" 'Rata de baudios, paridad, bits de datos, bits de parada .Handshaking = comNone .InputMode = comInputModeText .RThreshold = 1 'Define cada cuantos bytes recibidos se genera un evento .InputLen = 1 'Cuantos bytes se extraen al leer el puerto end with Para abrir el puerto MSCOMM1.PortOpen = True

Para enviar un dato por el puerto (debe estar abierto) MSCOMM1.Output = Chr(0x31) 'Envia el numero "1" por el puerto

Para leer un dato del puerto Inport=MSCOMM1.Input Espero que esto le sirva a alguien

You might also like