How to received output of at command [closed] - vb.net

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How to received output of at command.
This my form:
This my code:
Imports System.Net
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load, btnRefresh.Click
PortList.Items.Clear()
For Each a As String In My.Computer.Ports.SerialPortNames
'collecting available port
PortList.Items.Add(a)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnConnect.Click
If btnConnect.Text = "CONNECT" Then 'opeing port
If SerialPort1.IsOpen Then
SerialPort1.Close()
End If
Try
With SerialPort1
.PortName = PortList.Text
.BaudRate = 115200
.ReadBufferSize = 500
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.Handshake = IO.Ports.Handshake.None
End With
SerialPort1.Open()
btnConnect.Text = "DISCONNECT"
PortList.Enabled = False
With lblStatus
.Text = "Connected"
.ForeColor = Color.Green
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else 'closing port
Try
SerialPort1.Close()
btnConnect.Text = "CONNECT"
PortList.Enabled = True
With lblStatus
.Text = "Not Connected"
.ForeColor = Color.Red
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
Try
SerialPort1.Close() 'closing serial port
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles btnSend.Click
If UssdInput.Text.Length < 1 Then Exit Sub
Try
SerialPort1.WriteLine("AT+CUSD=1," & Chr(34) & UssdInput.Text & Chr(34) & ",15" & vbCrLf) 'send at command
UssdOutput.Text = SerialPort1.ReadExisting.ToString
Catch ex As Exception
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
With UssdInput
.SelectAll()
.Focus()
End With
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs)
MsgBox(System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList(1).ToString())
End Sub
Private Sub UssdInput_KeyPress(sender As Object, e As KeyPressEventArgs) Handles UssdInput.KeyPress
If e.KeyChar = vbCr Then
btnSend.PerformClick()
End If
End Sub
End Class

You need to listen to the serial port's DataReceived event. Here's an example:
Private Sub SerialPort1_DataReceivedHandler(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Invoke(Sub() UssdOutput.Text &= SerialPort1.ReadExisting())
End Sub

Related

VB .NET Socket Programing

I have a tools build in VB .Net and use Socket Programing. If just use 1 Server my code already work but when I wanna user 2 server and connect from 1 server my tools can't be handle that problem.
I wanna 1 Client can connect to 2 server
My code like this
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports Pabertiyan
Public Class Form1
Dim Client As TcpClient
Dim Client2 As TcpClient
Dim sWrinteras As StreamWriter
'Dim NickFrefix As Integer = New Random().Next(1111, 9999)
Dim Pabertiyan As New Pabertiyan.Pabertiyan
Delegate Sub _xUpdate(ByVal str As String)
Sub xUpdate(ByVal str As String)
If InvokeRequired Then
Invoke(New _xUpdate(AddressOf xUpdate), str)
Else
MsgBox("Sum of word in server is : " & str)
'TextBox1.AppendText(str & vbNewLine)
End If
End Sub
Sub Read(ByVal str As IAsyncResult)
Try
xUpdate(New StreamReader(Client.GetStream).ReadLine)
Client.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
Catch ex As Exception
xUpdate("Disconnect From Server")
Exit Sub
End Try
End Sub
Private Sub Send(ByVal str As String)
Try
sWrinteras = New StreamWriter(Client.GetStream)
sWrinteras.WriteLine(str)
sWrinteras.Flush()
Catch ex As Exception
MsgBox("You not Connect to Server")
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Button1.Text = "Connect" Then
Try
Client = New TcpClient(Pabertiyan.GetIPServer, Pabertiyan.GetPortServer)
Client.GetStream.BeginRead(New Byte() {0}, 0, 0, New AsyncCallback(AddressOf Read), Nothing)
Button1.Text = "Disconnect"
TextBox3.BackColor = Color.Lime
Catch ex As Exception
MsgBox("Server is Offline. Please Try Again Later!!!")
End Try
Else
Client.Client.Close()
Client = Nothing
Button1.Text = "Connect"
TextBox3.BackColor = Color.Red
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Send(TextBox5.Text)
End Sub
Private Sub CheckConfig_Tick(sender As Object, e As EventArgs) Handles CheckConfig.Tick
If Pabertiyan.ReloadConfig = 1 Then
Button4.Enabled = True
TextBox6.Text = Pabertiyan.ReloadConfig
End If
If Pabertiyan.ReloadConfig = 0 Then
Button4.Enabled = False
TextBox6.Text = Pabertiyan.ReloadConfig
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox5.Text = Pabertiyan.SearchChar
TextBox2.Text = Pabertiyan.TotalServer
TextBox7.Text = Pabertiyan.ModuleLogs
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
TextBox7.Text = Pabertiyan.ReloadConfig
TextBox5.Text = Pabertiyan.SearchChar
End Sub
End Class
Anyone can help me to solve my problem?

AccessViolationException was unhandled [VB.Net] [Emgucv]

Attempted to read or write protected memory. This is often an
indication that other memory is corrupt.
This was the error after I set the Image to my PictureBox. Its working fine but later on the error just pop-out.
Here is my code.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
Dim cap As New Capture() 'first line
PictureBox1.Image = cap.QueryFrame.ToBitmap 'this line AccessViolationException
Catch ex As Exception
Timer1.Stop()
MsgBox("CAMERA ERROR " & ex.Message)
End Try
End Sub
Private Sub MetroTile1_Click(sender As Object, e As EventArgs) Handles MetroTile1.Click
Try
Dim cap As New Capture() 'first line
Select Case MetroTile1.Text
Case "Capture"
Timer1.Start()
MetroTile1.Text = "OK"
Case "OK"
Timer1.Stop()
frmStudentAddEdit.picImage.Image = PictureBox1.Image
MetroTile1.Text = "Capture"
Me.Close()
End Select
Catch ex As Exception
Timer1.Stop()
End Try
End Sub
The cap.QueryFrame.ToBitmap is the AccessViolationException was unhandled error.
How can I Fix this ? What causing this error ? Please Help.
Aim for something like the following.
Capture is a member of the form (not created new each time)
oldImage is disposed of after replacing
Private mCapture As Capture
Private Sub Form12_Load(sender As Object, e As System.EventArgs) Handles Me.Load
mCapture = New Capture()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
Dim oldImage = PictureBox1.Image
Dim newFrame = mCapture.QueryFrame.ToBitmap
PictureBox1.Image = newFrame.ToBitmap
If oldImage IsNot Nothing Then oldImage.Dispose()
Catch ex As Exception
Timer1.Stop()
MsgBox("CAMERA ERROR " & ex.Message)
End Try
End Sub
Private Sub MetroTile1_Click(sender As Object, e As EventArgs) Handles MetroTile1.Click
Try
Select Case MetroTile1.Text
Case "Capture"
Timer1.Start()
MetroTile1.Text = "OK"
Case "OK"
Timer1.Stop()
frmStudentAddEdit.picImage.Image = PictureBox1.Image
MetroTile1.Text = "Capture"
Me.Close()
End Select
Catch ex As Exception
Timer1.Stop()
End Try
End Sub

How to enable Live Service Controller Reporting?

Application Screenshot:
Application Code:
Dim myController As New System.ServiceProcess.ServiceController("SQL Server (SQLEXPRESS)")
Sub ed()
If txtNum.Text = "Started" Then
btnStart.Enabled = False
btnStop.Enabled = True
ElseIf txtNum.Text = "Stopped" Then
btnStop.Enabled = False
btnStart.Enabled = True
End If
End Sub
Sub Na1()
If myController.Status = ServiceProcess.ServiceControllerStatus.Running Then
txtNum.Text = "Started"
ElseIf myController.Status = ServiceProcess.ServiceControllerStatus.Stopped Then
txtNum.Text = "Stopped"
End If
End Sub
Private Sub Test_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Na1()
ed()
End Sub
Private Sub Test_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
Me.Refresh() '////////This one is not required
End Sub
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
ProgressBar1.Value = 0
Try
myController.Start()
Catch ex As Exception
MsgBox(ex.Message)
Finally
ProgressBar1.Value = 100
Na1()'//////////////////////////////////////////////Edit Code here
End Try
End Sub
Private Sub txtNum_TextChanged(sender As Object, e As EventArgs) Handles txtNum.TextChanged
ed()
End Sub
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
ProgressBar1.Value = 0
Try
myController.Stop()
Catch ex As Exception
MsgBox(ex.Message)
Finally
ProgressBar1.Value = 100
Na1() '//////////////////////////////////////////////Edit Code here
End Try
End Sub
You need to stop SQL Service to Copy .mdf File and it is very irritating by Stopping/Starting the service manually from 'Services' so I tried to make it easier by using Vb.Net coding...
Edit the Following codes,
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
ProgressBar1.Value = 0
Try
myController.Start()
Catch ex As Exception
MsgBox(ex.Message)
Finally
ProgressBar1.Value = 100
Na1()'//////////////////////////////////////////////Edit Code here
End Try
End Sub
To
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
ProgressBar1.Value = 0
Try
myController.Start()
Catch ex As Exception
MsgBox(ex.Message)
Finally
ProgressBar1.Value = 100
myController.WaitForStatus(ServiceProcess.ServiceControllerStatus.Running)'//Add
Na1()
End Try
End Sub
And
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
ProgressBar1.Value = 0
Try
myController.Stop()
Catch ex As Exception
MsgBox(ex.Message)
Finally
ProgressBar1.Value = 100
Na1() '//////////////////////////////////////////////Edit Code here
End Try
End Sub
To
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
ProgressBar1.Value = 0
Try
myController.Stop()
Catch ex As Exception
MsgBox(ex.Message)
Finally
ProgressBar1.Value = 100
myController.WaitForStatus(ServiceProcess.ServiceControllerStatus.Stopped) '//Add
Na1()
End Try
End Sub
And all is done!

Regain focus after a background task is completed

I have a datagridView(dgv) in the active tab(tabControl) that is filled with data obtained from a web service, when i start the query i create a separated thread that will assign the data to the dgv on his RunWorkerCompleted event, but then the dgv doesn't select a row when i make a click on it, in order to work properly i have to select the other tab and select again the tab containing this dgv
-the dgv still responds to the event cellContentClick
some code:
Private Sub backgroundWorker1_DoWork(sender As System.Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
Try
If UCA1.getAlumnos(codCarrrera, anio, ciclo, carnet_opc).alumnosArray IsNot Nothing Then
For i As Integer = 0 To UCA1.getAlumnos(codCarrrera, anio, ciclo, carnet_opc).alumnosArray.Length - 1
Dim a As sv.edu.uca.wsprb.alumnos = UCA1.getAlumnos(codCarrrera, anio, ciclo, carnet_opc).alumnosArray(i)
'here i add the rows
'TablaFAlumno.Rows.Add(...)
If i < 10 Then
BackgroundWorker1.ReportProgress(10)
End If
Next
Else
TablaFAlumno.Rows.Clear()
End If
Catch ex As System.Net.WebException
MessageBox.Show("->" + ex.Message)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub backgroundWorker1_ProgressChanged(sender As System.Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
If ProgressBar1.Value = 0 Then
ProgressBar1.Visible = True
End If
If ProgressBar1.Value < 100 Then
ProgressBar1.Value += 10
End If
End Sub
Private Sub backgroundWorker1_RunWorkerCompleted(sender As System.Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If e.Error IsNot Nothing Then
MessageBox.Show("E -->" + e.Error.Message)
ElseIf e.Cancelled = True Then
ProgressBar1.Visible = False
MessageBox.Show("C -->" + e.Error.Message)
End If
ProgressBar1.Visible = False
dgvAlumnos.DataSource = TablaFAlumno
End Sub
try ad the end :
ProgressBar1.Visible = False
'TabControl name with Ficha Alumno
Me.TabControl1.SelectedIndex = 0
dgvAlumnos.DataSource = TablaFAlumno
dvgAlumnos.Select()
End Sub

How to check if Bluetooth connection is closed or cannot be created with vb.net?

I have created a simple app in vb.net that receives data from a Bluetooth device (simple strings like "90123.9mm").
Is there a way to check if the device has closed the connection?
I have tried to put the code in a timer
If SerialPort1 Is Nothing Then
MsgBox("no connect")
End If
If Not (SerialPort1.IsOpen) Then
MsgBox("no connect")
End If
but with no luck.
Here is the connection and reception code.
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
SerialPort1.PortName = "COM22"
SerialPort1.BaudRate = "9600"
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8
SerialPort1.Open()
If SerialPort1 Is Nothing Then
MsgBox("no connect")
End If
If Not (SerialPort1.IsOpen) Then
MsgBox("no connect")
End If
btnConnect.Enabled = False
btnDisconnect.Enabled = True
End Sub
Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
SerialPort1.Close()
btnConnect.Enabled = True
btnDisconnect.Enabled = False
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting()) 'aftomata kalite kathe fora pou dexomai nea dedomena
End Sub
Private Sub ReceivedText(ByVal [text] As String)
If Me.rtbReceived.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.rtbReceived.Text &= [text]
End If
End Sub
Just try with appending try catch blocks
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
'--close the serail port if it is open --
If serialPort1.IsOpen Then
serialPort1.Close()
End If
Try
'-- configure the various parameters of the serial port --
With serialPort1
.PortName = "COM22"
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.StopBits = IO.Ports.StopBits.One
.DataBits = 8
End With
' -- now open the port
serialPort1.Open()
' -- update the status if you like and
' -- enable/disable the buttons --
btnConnect.Enabled = False
btnDisconnect.Enabled = True
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
Try
' -- close the serial port here --
SerialPort1.Close()
' -- update the status if you like and
' -- enable/disable the buttons --
btnConnect.Enabled = True
btnDisconnect.Enabled = False
Catch ex As Exception
MsgBox(ex.ToString())
End Sub
This is one of the problems of using Virtual COM Ports -- all the visibility of connecting to the remote device and any connection problems is hidden. That's why I always try to use the direct comms API. One can use my 32feet.NET library for instance to make the Bluetooth connection directly.
See e.g. http://32feet.codeplex.com/wikipage?title=Bluetooth%20Serial%20Ports http://32feet.codeplex.com/wikipage?title=General%20Bluetooth%20Data%20Connections etc