Using the Serial Comm from VB Net

This demonstrate will show you how to use the Serial Communication from Visual Basic Dot Net using the ‘Serial Comm ActiveX’ component (OCX).


Requirements:

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
Option Strict Off
Option Explicit On
Friend Class frmMain
	Inherits System.Windows.Forms.Form
 
 
	Private Sub cmdATCommand_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdATCommand.Click
 
		'Write data to serial port.
		
		Dim lngSize As Short
		Dim strData As String
		Dim lngStatus As Integer
 
		'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(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdConfig.Click
 
		'Display configuration:
		
		Dim lErr As Integer
 
		lErr = cComm1.LineConfigDialog(Combo1.SelectedIndex)
		If lErr <> 0 Then
			'Err
		End If
 
	End Sub
 
	Private Sub cmdLineInfo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdLineInfo.Click
 
		Dim LineDev As SerialCommAX.LineDevCap
 
		'Set the line index to get the results for:
		cComm1.LineIndex = Combo1.SelectedIndex
 
		'Get the device index to the LineDev:
		cComm1.LineGetDevDetails(LineDev)
 
		'Display the results:
		txtResults.Text = 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.SelectedIndex) & vbCrLf
 
		txtResults.Text = txtResults.Text & vbCrLf
 
	End Sub
 
	Private Sub cmdLocInfo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdLocInfo.Click
 
		'Get the country code:
		txtResults.Text = txtResults.Text & "Local country Code:" & cComm1.GetCountryCode & vbCrLf
 
		'Get the city code:
		txtResults.Text = txtResults.Text & "Local city Code:" & cComm1.GetCityCode & vbCrLf
 
	End Sub
 
 
	Private Sub cmdLocProp_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdLocProp.Click
 
		cComm1.LineTranslateDialog()
 
	End Sub
 
 
	Private Sub cmdOff_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdOff.Click
 
		Dim rc As Integer
 
		'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(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdOn.Click
 
		Dim lPort As Short
		Dim rc As Integer
 
		'First, get the port number of the device:
		lPort = cComm1.GetPortNumber(Combo1.SelectedIndex)
		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 frmMain_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.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 Integer
		Dim sDeviceName As String
		Dim LineDev As SerialCommAX.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.Items.Add(sDeviceName)
 
		Next LineIndex
 
		Combo1.SelectedIndex = 0
 
 
	End Sub
 
	Private Sub frmMain_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
 
		cComm1.CommClose()
 
		'Release the control
		cComm1.DeInit()
 
	End Sub
 
	'UPGRADE_WARNING: Event txtResults.TextChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub txtResults_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtResults.TextChanged
 
		'Auto scroll down:
		txtResults.SelectionStart = Len(txtResults.Text) - 1
 
	End Sub
 
 
	'*******************************************************
	'   Events:
	'*******************************************************
	
	Private Sub cComm1_CommEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxSerialCommAX.__cComm_CommEventEvent) Handles cComm1.CommEvent
 
		txtResults.Text = txtResults.Text & eventArgs.sMessage & vbCrLf
 
	End Sub
 
	Private Sub cComm1_Error(ByVal eventSender As System.Object, ByVal eventArgs As AxSerialCommAX.__cComm_ErrorEvent) Handles cComm1.Error
 
		MsgBox(eventArgs.sErrMessage)
 
	End Sub
End Class

Links:





read the original story ...

Replies are closed.