如何接收at命令的输出。
这是我的表格:
这是我的代码:
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
答案 0 :(得分:1)
您需要收听串口的DataReceived
事件。这是一个例子:
Private Sub SerialPort1_DataReceivedHandler(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Invoke(Sub() UssdOutput.Text &= SerialPort1.ReadExisting())
End Sub