Before I start my question, I would like to thank every member in the perfect website.
......................................................................................................................................................................................................................
I’m working on my final project and I’m facing a problem.
I am trying to design a program that can detect the incoming call, receive data and display it by using MSComm (com3) in visual basic.net.
The program can detect the incoming call but the problem here that the program can’t receive the data and display it.
At the same time, the same program will receive data from 8051 in com1
I tried to change many things but still the program couldn’t receive the data.
If anyone can help me to solve this problem, I will be pleased for your help.
I attached this post with my program
......................................................................................................................................................................................................................
Dim Phase As String
Dim Stage As String
Dim ReceiveBuffer As String
Const ReceivingInterval = 100
Private Sub Command1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Command1.Click
Dim strBuffer As String
Dim InString As String
Dim i As Integer
MSComm2.Settings = "9600,N,8,1"
MSComm2.CommPort = 3
MSComm2.Handshaking = 2
MSComm2.DTREnable = True
MSComm2.RTSEnable = True
MSComm2.InputLen = 0 'Default anyway
MSComm2.RThreshold = 1 'Fire OnComm event with a single character in input buffer
MSComm2.SThreshold = 1
MSComm2.PortOpen = True
Transact("ATE0", 300) 'command to tell the modem not to echo the "AT" back
Stage = "Initialising Modem"
Transact("ATS0=2", 300) ' Set Modem S0 register to 2 (Answer on two rings)
End Sub
Sub Transact(ByVal SendText As String, ByVal TimeOut As Integer)
WriteLog("Sending: " & SendText)
Timer1.Interval = TimeOut 'milliseconds
ReceiveBuffer = ""
MSComm2.Output = SendText & Chr(13) & Chr(10)
Timer1.Enabled = True
Phase = "Sending"
End Sub
Private Sub MSComm2_OnComm(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MSComm2.OnComm
showasc("OnComm: " & MSComm2.CommEvent)
Select Case MSComm2.CommEvent
Case 1 '1 Send event.
Case 2 '2 Receive event.
Phase = "Receiving"
ReceiveBuffer = ReceiveBuffer & MSComm2.Input
WriteLog("ReceiveBuffer: " & ReceiveBuffer)
Timer1.Enabled = False
Timer1.Interval = ReceivingInterval
Timer1.Enabled = True
Case 3 '3 Change in clear-to-send line.
Case 4 '4 Change in data-set ready line.
Case 5 '5 Change in carrier detect line.
Case 6 '6 Ring detect.
Case 7 '7 End of file.
'errors
Case 1001 '1001 A Break signal was received.
Case 1004 '1004 Framing Error. The hardware detected a framing error.
Case 1006 '1006 Port Overrun. A character was not read from the hardware before the next character arrived and was lost.
Case 1008 '1008 Receive Buffer Overflow. There is no room in the receive buffer.
Case 1009 '1009 Parity Error. The hardware detected a parity error.
Case 1010 '1010 Transmit Buffer Full. The transmit buffer was full while trying to queue a character.
Case 1011 '1011 Unexpected error retrieving Device Control Block (DCB) for the port.
End Select
End Sub
Function TrimLeft(ByVal Intext As String) As String
Dim Text As String
If Len(Intext) > 0 Then
Text = Intext
Do While Asc(Text) <= 32
Text = Mid$(Text, 2)
Loop
TrimLeft = Text
End If
End Function
Function TrimBoth(ByVal Intext As String) As String
Dim text As String
text = Intext
Do While Asc(Mid$(text, Len(text), 1)) <= 57
text = Mid$(text, 1, Len(text) - 1)
Loop
Do While Asc(Mid$(text, 1, 1)) <= 57
text = Mid$(text, 2)
Loop
TrimBoth = text
End Function
Dim line = 0
Sub WriteLog(ByVal Text As String)
Dim strBuffer As String
strBuffer = TrimBoth(Text)
strBuffer = Text
line = line + 1
If Len(strBuffer) > 0 Then
Label1.Text = line & " " & strBuffer
End If
End Sub
Sub Listen(ByVal TimeOut As Integer)
WriteLog("Listening: " & TimeOut & " millisecs")
Timer1.Interval = TimeOut 'milliseconds
ReceiveBuffer = ""
Timer1.Enabled = True
Phase = "Receiving"
End Sub
Private Sub showasc(ByVal text As String)
Dim i As Integer
label2.Text = ""
label2.Text = label2.Text & " " & text & " "
End Sub
Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
Dim strBuffer As String
WriteLog("Timer: Interval: " & Timer1.Interval & " Stage: " & Stage & " Phase: " & Phase)
Select Case Phase
Case "Sending"
WriteLog("Nothing Received")
Case "Receiving"
strBuffer = ReceiveBuffer
WriteLog("Received: " & strBuffer)
End Select
showasc(strBuffer)
Timer1.Enabled = False
End Sub
Private Sub DataIn()
Dim strbuffer As String
Do While True
Select Case Stage
Case "Initialising Modem"
If Mid$(strbuffer, Len(strbuffer) - 1, 2) = "OK" Then
Transact("ATS0=2", 300) ' Set Modem S0 register to 2 (Answer on two rings)
Stage = "Setting S Reg"
Phase = "Sending"
Else
WriteLog("Modem not responding")
End If
Case "Setting S Reg"
Select Case Mid$(strbuffer, 1, 2)
Case "OK"
Stage = "Listening"
Phase = "Receiving"
Listen(100)
Case Else
'some sort of error
End Select
Case "Listening"
Select Case strbuffer
Case "OK"
Case "RING"
WriteLog("Incoming call")
ReceiveBuffer = ""
Case "CONNECT" 'The connect message may have some protocol information appended.
WriteLog("Connected")
DataIn()
End Select
End Select
Loop
End Sub
Function TrimRight(ByVal Intext As String) As String
'strip blanks, tabs, line feeds etc from end of string
Dim Text As String
Text = Intext
If Len(Intext) > 0 Then
Do While Asc(Mid$(Text, 1)) <= 32
Text = Mid$(Text, Len(Text) - 1)
Loop
TrimRight = Text
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ReceiveBuffer = ""
Label1.Text = ""
label2.Text = ""
Timer2.Enabled = True
End Sub
Private Sub label2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles label2.TextChanged
End Sub
Private Sub dial_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dial.Click
MSComm3.Output = "ATDT" + TextBox1.Text + Chr(13)
End Sub
Private Sub hang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles hang.Click
MSComm3.Output = "ATH" + TextBox1.Text + Chr(13)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
TextBox1.Text = ""
End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
MSComm3.PortOpen = True
End Sub
Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
MSComm3.PortOpen = False
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub MSComm3_OnComm(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MSComm3.OnComm
MSComm3.CommPort = True
MSComm3.Input = "cpa00000000" + Chr(13)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MSComm2.Output = vbCr + "ATZ" + vbCr
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
End
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Label1.Text = ""
label2.Text = ""
MSComm2.Output = vbCr
Timer2.Enabled = False
End Sub
End Class
......................................................................................................................................................................................................................
I’m working on my final project and I’m facing a problem.
I am trying to design a program that can detect the incoming call, receive data and display it by using MSComm (com3) in visual basic.net.
The program can detect the incoming call but the problem here that the program can’t receive the data and display it.
At the same time, the same program will receive data from 8051 in com1
I tried to change many things but still the program couldn’t receive the data.
If anyone can help me to solve this problem, I will be pleased for your help.
I attached this post with my program
......................................................................................................................................................................................................................
Dim Phase As String
Dim Stage As String
Dim ReceiveBuffer As String
Const ReceivingInterval = 100
Private Sub Command1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Command1.Click
Dim strBuffer As String
Dim InString As String
Dim i As Integer
MSComm2.Settings = "9600,N,8,1"
MSComm2.CommPort = 3
MSComm2.Handshaking = 2
MSComm2.DTREnable = True
MSComm2.RTSEnable = True
MSComm2.InputLen = 0 'Default anyway
MSComm2.RThreshold = 1 'Fire OnComm event with a single character in input buffer
MSComm2.SThreshold = 1
MSComm2.PortOpen = True
Transact("ATE0", 300) 'command to tell the modem not to echo the "AT" back
Stage = "Initialising Modem"
Transact("ATS0=2", 300) ' Set Modem S0 register to 2 (Answer on two rings)
End Sub
Sub Transact(ByVal SendText As String, ByVal TimeOut As Integer)
WriteLog("Sending: " & SendText)
Timer1.Interval = TimeOut 'milliseconds
ReceiveBuffer = ""
MSComm2.Output = SendText & Chr(13) & Chr(10)
Timer1.Enabled = True
Phase = "Sending"
End Sub
Private Sub MSComm2_OnComm(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MSComm2.OnComm
showasc("OnComm: " & MSComm2.CommEvent)
Select Case MSComm2.CommEvent
Case 1 '1 Send event.
Case 2 '2 Receive event.
Phase = "Receiving"
ReceiveBuffer = ReceiveBuffer & MSComm2.Input
WriteLog("ReceiveBuffer: " & ReceiveBuffer)
Timer1.Enabled = False
Timer1.Interval = ReceivingInterval
Timer1.Enabled = True
Case 3 '3 Change in clear-to-send line.
Case 4 '4 Change in data-set ready line.
Case 5 '5 Change in carrier detect line.
Case 6 '6 Ring detect.
Case 7 '7 End of file.
'errors
Case 1001 '1001 A Break signal was received.
Case 1004 '1004 Framing Error. The hardware detected a framing error.
Case 1006 '1006 Port Overrun. A character was not read from the hardware before the next character arrived and was lost.
Case 1008 '1008 Receive Buffer Overflow. There is no room in the receive buffer.
Case 1009 '1009 Parity Error. The hardware detected a parity error.
Case 1010 '1010 Transmit Buffer Full. The transmit buffer was full while trying to queue a character.
Case 1011 '1011 Unexpected error retrieving Device Control Block (DCB) for the port.
End Select
End Sub
Function TrimLeft(ByVal Intext As String) As String
Dim Text As String
If Len(Intext) > 0 Then
Text = Intext
Do While Asc(Text) <= 32
Text = Mid$(Text, 2)
Loop
TrimLeft = Text
End If
End Function
Function TrimBoth(ByVal Intext As String) As String
Dim text As String
text = Intext
Do While Asc(Mid$(text, Len(text), 1)) <= 57
text = Mid$(text, 1, Len(text) - 1)
Loop
Do While Asc(Mid$(text, 1, 1)) <= 57
text = Mid$(text, 2)
Loop
TrimBoth = text
End Function
Dim line = 0
Sub WriteLog(ByVal Text As String)
Dim strBuffer As String
strBuffer = TrimBoth(Text)
strBuffer = Text
line = line + 1
If Len(strBuffer) > 0 Then
Label1.Text = line & " " & strBuffer
End If
End Sub
Sub Listen(ByVal TimeOut As Integer)
WriteLog("Listening: " & TimeOut & " millisecs")
Timer1.Interval = TimeOut 'milliseconds
ReceiveBuffer = ""
Timer1.Enabled = True
Phase = "Receiving"
End Sub
Private Sub showasc(ByVal text As String)
Dim i As Integer
label2.Text = ""
label2.Text = label2.Text & " " & text & " "
End Sub
Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
Dim strBuffer As String
WriteLog("Timer: Interval: " & Timer1.Interval & " Stage: " & Stage & " Phase: " & Phase)
Select Case Phase
Case "Sending"
WriteLog("Nothing Received")
Case "Receiving"
strBuffer = ReceiveBuffer
WriteLog("Received: " & strBuffer)
End Select
showasc(strBuffer)
Timer1.Enabled = False
End Sub
Private Sub DataIn()
Dim strbuffer As String
Do While True
Select Case Stage
Case "Initialising Modem"
If Mid$(strbuffer, Len(strbuffer) - 1, 2) = "OK" Then
Transact("ATS0=2", 300) ' Set Modem S0 register to 2 (Answer on two rings)
Stage = "Setting S Reg"
Phase = "Sending"
Else
WriteLog("Modem not responding")
End If
Case "Setting S Reg"
Select Case Mid$(strbuffer, 1, 2)
Case "OK"
Stage = "Listening"
Phase = "Receiving"
Listen(100)
Case Else
'some sort of error
End Select
Case "Listening"
Select Case strbuffer
Case "OK"
Case "RING"
WriteLog("Incoming call")
ReceiveBuffer = ""
Case "CONNECT" 'The connect message may have some protocol information appended.
WriteLog("Connected")
DataIn()
End Select
End Select
Loop
End Sub
Function TrimRight(ByVal Intext As String) As String
'strip blanks, tabs, line feeds etc from end of string
Dim Text As String
Text = Intext
If Len(Intext) > 0 Then
Do While Asc(Mid$(Text, 1)) <= 32
Text = Mid$(Text, Len(Text) - 1)
Loop
TrimRight = Text
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ReceiveBuffer = ""
Label1.Text = ""
label2.Text = ""
Timer2.Enabled = True
End Sub
Private Sub label2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles label2.TextChanged
End Sub
Private Sub dial_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dial.Click
MSComm3.Output = "ATDT" + TextBox1.Text + Chr(13)
End Sub
Private Sub hang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles hang.Click
MSComm3.Output = "ATH" + TextBox1.Text + Chr(13)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
TextBox1.Text = ""
End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
MSComm3.PortOpen = True
End Sub
Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
MSComm3.PortOpen = False
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub MSComm3_OnComm(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MSComm3.OnComm
MSComm3.CommPort = True
MSComm3.Input = "cpa00000000" + Chr(13)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MSComm2.Output = vbCr + "ATZ" + vbCr
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
End
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Label1.Text = ""
label2.Text = ""
MSComm2.Output = vbCr
Timer2.Enabled = False
End Sub
End Class