This demonstrate will show you how to use the Serial Communication from Visual Basic Dot Net using the ‘Serial Comm ActiveX’ component (OCX).
Requirements:
- Visual Basic 6
- Serial Comm ActiveX | Download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | Option Explicit Private Sub cmdATCommand_Click() 'Write data to serial port. Dim lngSize As Integer Dim strData As String Dim lngStatus As Long 'Get the command: strData = txtATCommand.Text & vbCr 'Get the size: lngSize = Len(strData) 'Write the data: lngStatus = cComm1.CommWrite(strData) 'Handle error. If lngStatus <> lngSize Then 'Error.. End If End Sub Private Sub cmdConfig_Click() 'Display configuration: Dim lErr As Long lErr = cComm1.LineConfigDialog(Combo1.ListIndex) If lErr <> 0 Then 'Err End If End Sub Private Sub cmdLineInfo_Click() Dim LineDev As LineDevCap 'Set the line index to get the results for: cComm1.LineIndex = Combo1.ListIndex 'Get the device index to the LineDev: cComm1.LineGetDevDetails LineDev 'Display the results: txtResults = vbCrLf _ & "Device Name:" & LineDev.DeviceName & vbCrLf _ & "Tapi Capability: " & LineDev.TapiCapability & vbCrLf _ & "Max Rate: " & LineDev.MaxRate & vbCrLf _ & "Support Monitor Digits: " & LineDev.MonitorDigits & vbCrLf _ & "Support Generate Digits: " & LineDev.GenerateDigits & vbCrLf _ & "Data Mode:" & LineDev.MediaDatamodem & vbCrLf _ & "Interactivevoice:" & LineDev.MediaInteractivevoice & vbCrLf _ & "Auto Voice:" & LineDev.MediaAutomatedvoice & vbCrLf _ & "Comm Port:" & cComm1.GetPortNumber(Combo1.ListIndex) & vbCrLf txtResults = txtResults & vbCrLf End Sub Private Sub cmdLocInfo_Click() 'Get the country code: txtResults.Text = txtResults & "Local country Code:" & cComm1.GetCountryCode & vbCrLf 'Get the city code: txtResults.Text = txtResults & "Local city Code:" & cComm1.GetCityCode & vbCrLf End Sub Private Sub cmdLocProp_Click() cComm1.LineTranslateDialog End Sub Private Sub cmdOff_Click() Dim rc As Long 'Close the comm port rc = cComm1.CommClose If rc = 0 Then cmdOff.Enabled = False cmdATCommand.Enabled = False cmdOn.Enabled = True End If End Sub Private Sub cmdOn_Click() Dim lPort As Integer Dim rc As Long 'First, get the port number of the device: lPort = cComm1.GetPortNumber(Combo1.ListIndex) If lPort < 0 Then 'Error Exit Sub End If 'Set the port number: cComm1.Port = lPort 'Open the comm port rc = cComm1.CommOpen("COM" & CStr(lPort), txtSetting.Text) If rc = 0 Then cmdOff.Enabled = True cmdATCommand.Enabled = True cmdOn.Enabled = False End If End Sub Private Sub Form_Load() 'Init the control, must be call 'before using any functionality 'of the control. ' 'sKey = Your license key. ' cComm1.Init "Trial Mode." Dim LineIndex As Long Dim sDeviceName As String Dim LineDev As LineDevCap 'Scan all the lines and get the device name: For LineIndex = 0 To cComm1.NumOfLines - 1 'Get device details by the index cComm1.LineIndex = LineIndex cComm1.LineGetDevDetails LineDev sDeviceName = LineDev.DeviceName 'Add to the list DeviceName and DeviceIndex. Combo1.AddItem sDeviceName Next LineIndex Combo1.ListIndex = 0 End Sub Private Sub Form_Unload(Cancel As Integer) cComm1.CommClose 'Release the control cComm1.DeInit End Sub Private Sub txtResults_Change() 'Auto scroll down: txtResults.SelStart = Len(txtResults.Text) - 1 End Sub '******************************************************* ' Events: '******************************************************* Private Sub cComm1_CommEvent(sMessage As String) txtResults.Text = txtResults.Text & sMessage & vbCrLf End Sub Private Sub cComm1_Error(lErrNumber As Long, sErrMessage As String) MsgBox sErrMessage End Sub |
Links:
- Visual Basic 6
- Serial Comm ActiveX | Download
read the original story ...
It’s really a nice and helpful piece of information. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thanks for sharing.
your good