VB.NET 2010 SerialPort和AT命令 [英] VB.NET 2010 SerialPort and AT Command
问题描述
大家好。
这个应用程序连接到COM,设置一个波特率范围,然后让你能够编写字符串并让你看到调制解调器的响应我每个人都写了一个noob应用程序。
这个应用程序连接到COM
这是代码
< span class =code-keyword> Imports 系统
Imports System.ComponentModel
Imports System.Threading
Imports System.IO
Imports System.IO.Ports
公共 类 AtNetMain
Dim myPort 作为数组' 系统上检测到的COM端口将存储在此处
代理 Sub SetTextCallback( ByVal [text] As String )' 已添加以防止在接收数据期间出现线程错误
Dim open As Boolean
私有 Sub AtNetMain_Load(发件人 As System。 Object ,e As System.EventArgs) Handles MyBase .Load
' 当我们的表单加载时,自动检测系统中的所有串行端口并填充cmbPort组合框。
myPort = IO.Ports.SerialPort.GetPortNam es()' 获取所有可用的COM端口
Baud_cmbox.Items.Add( 9600 )' 将cmbBaud组合框填充到常用的波特率
Baud_cmbox.Items.Add( 19200 )
Baud_cmbox.Items.Add( 38400 )
Baud_cmbox.Items.Add( 57600 )
Baud_cmbox.Items.Add( 115200 )
对于 i = 0 到 UBound(myPort)
Port_cmbox.Items.Add(myPort(i))
下一步
Port_cmbox .Text = Port_cmbox.Items.Item( 0 )' 设置cmbP ort文本到检测到的第一个COM端口
Baud_cmbox.Text = Baud_cmbox.Items.Item( 0 )' 将cmbBaud文本设置为列表中的第一个波特率
LogOff_btn.Enabled = 错误 ' 最初断开连接按钮已禁用
结束 Sub
私有 Sub LogOn_btn_Click( ByVal sender As System。对象, ByVal e 作为 System.EventArgs)句柄 LogOn_btn.Click
如果(字符串 .IsNullOrEmpty(Port_cmbox.Text)或 字符串 .IsNullOrEmpty(Baud_cmbox。文本))然后
MessageBox.Show( Non hai selezionato la Porta e / o il波特率, Attenzione, MessageBoxButtons.OK,MessageBoxIcon.Warning)
Else
SerialPort1.PortName = Port_cmbox.Text ' 在启动时将SerialPort1设置为选定的COM端口
SerialPort1.BaudRate = Baud_cmbox.Text ' 将波特率设置为上的选定值
' 其他串行端口属性
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8 ' 打开我们的串口
试试
SerialPort1.Open()
open = True
Catch ex As 异常
MessageBox.Show(ex.Message)
结束 尝试
LogOn_btn.Enabled = 错误 ' 禁用连接按钮
LogOff_btn.Enabled = True '矿泉n> 并启用断开连接按钮
结束 如果
结束 Sub
私有 Sub LogOff_btn_Click( ByVal sender 正如系统。对象, ByVal e As System.EventArgs)句柄 LogOff_btn.Click
SerialPort1.Close()' 关闭我们的串口
LogOn_btn.Enabled = True
LogOff_btn.Enabled = < span class =code-keyword> False
结束 Sub
私有 Sub Send_btn_Click( ByVal sender As System。 Object , ByVal e As System.EventArgs)句柄 Send_btn.Click
如果(打开)然后
尝试
SerialPort1。 WriteLine( AT + CSQ)
' MessageBox.Show(SerialPort1.ReadExisting())
' txtText中包含的文本将被发送到串口,如ascii
' 加上回车符(Enter键)如果另一端不需要,则可以省略回车
Catch ex As 异常
MessageBox.Show(ex.Message)
结束 尝试
否则
MessageBox .Show( Nessuna connessione stabilita, Attenzione,MessageBoxButtons.OK,MessageBoxIcon.Warning)
结束 如果
结束 Sub
< span class =code-keyword>私有 Sub SerialPort1_DataReceiv ed( ByVal sender As Object ,< span class =code-keyword> ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
MessageBox.Show(SerialPort1.ReadExisting())
ReceivedText(SerialPort1.ReadExisting())' 每次在serialPort收到数据时自动调用
结束 Sub
私有 Sub ReceivedText( ByVal [text] 作为 字符串)
' 比较创建Thread t的ID o调用线程的ID
如果 我 .Received_rtbox.InvokeRequired < span class =code-keyword>然后
Dim x As < span class =code-keyword>新 SetTextCallback( AddressOf ReceivedText)
我 .Invoke(x, New Object (){(text)})
< span class =code-keyword> Else
Me .Received_rtbox.Text& = [text]
结束 如果
结束 Sub
私有 Sub Port_cmbox_SelectedIndexChanged( ByVal 发件人作为系统。对象, ByVal e As System.EventArgs)句柄 Port_cmbox.SelectedIndexChanged
如果 SerialPort1.IsOpen = False 那么
SerialPort1.PortName = Port_cmbox.Text ' 如果用户正在更改端口,则弹出一个消息框
其他 ' 首先不断开连接。
MsgBox( 仅在端口关闭时有效,vbCritical)
结束 如果
结束 < span class =code-keyword> Sub
Private Sub Baud_cmbox_SelectedIndexChanged( ByVal sender As System。 Object ,< span class =code-keyword> ByVal e As System.EventArgs)句柄 Baud_cmbox .SelectedIndexChanged
如果 SerialPort1.IsOpen = False 然后
SerialPort1.BaudRate = Baud_cmbox.Text ' 如果他是,则向用户弹出一个消息框更改波特率
其他 ' 而不先断开连接。
MsgBox( 仅在端口关闭时有效,vbCritical)
结束 如果
结束 Sub
私有 Sub SerialPort1_ErrorReceived(发件人作为系统。 对象,e As System.IO.Ports.SerialErrorReceivedEventArgs)句柄 SerialPort1.ErrorReceived
MessageBox.Show( Errore, Attenzione,MessageBoxButtons.OK,MessageBoxIcon.Warning)
结束 Sub
< span class =code-keyword>结束 类
我只使用一个设计为简单app的表单
用于连接和断开连接的两个按钮,用于com和波特选择的两个组合框以及一个可以编写消息的文本框...
我的问题是代码行
SerialPort1.WriteLine( AT + CSQ)
MessageBox.Show(SerialPort1.ReadExisting())
不起作用:(我不知道为什么....
使用您显示的两行代码,它们将一个接一个地执行。有些设备在发送回复之前需要一点时间处理数据。
此外,您还有一个DataReceived事件,该事件应捕获从设备返回的数据。 。所以问题代码的第二部分无论如何都是多余的。
[edit]
在Dim下添加以下内容open as Boolean
到您的声明列表中,代码中缺少它。
WithEvents SerialPort1 作为 新 SerialPort
删除这两个潜艇,不需要它们。
私人 Sub Port_cmbox_SelectedIndexChanged
私人 Sub Baud_cmbox_SelectedIndexChanged
删除消息框行。
私有 Sub SerialPort1_DataReceived( ByVal sender As 对象
MessageBox.Show(SerialPort1.ReadExisting())
我测试了代码,它应该适合你。
你需要使用委托,因为serial_DataReceive函数在一个单独的线程上执行。
在代码顶部添加一个代表。
私人代表Sub SPReceiver( ByVal sender As Object,ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
Private Sub SP_DataReceived(ByVal sender As Object,ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)Handles SP.DataReceived
If Me.InvokeRequired = True Then
Me.Invoke(New SPReceiver(AddressOf SP_DataReceived),sender ,e)
退出Sub
结束如果
dim DataRec As String = SP.ReadExisting
End sub
是的我使用messabox进行调试..我知道它没用(消息框)
Hi everyone.
This application connects to a COM, set a range of baud and then gives you the ability to write strings and makes you see the modem's responseHi everyone i wrote a noob application.
this app connect to a COM
this is the code
Imports System Imports System.ComponentModel Imports System.Threading Imports System.IO Imports System.IO.Ports Public Class AtNetMain Dim myPort As Array 'COM Ports detected on the system will be stored here Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data Dim open As Boolean Private Sub AtNetMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 'When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box. myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available Baud_cmbox.Items.Add(9600) 'Populate the cmbBaud Combo box to common baud rates used Baud_cmbox.Items.Add(19200) Baud_cmbox.Items.Add(38400) Baud_cmbox.Items.Add(57600) Baud_cmbox.Items.Add(115200) For i = 0 To UBound(myPort) Port_cmbox.Items.Add(myPort(i)) Next Port_cmbox.Text = Port_cmbox.Items.Item(0) 'Set cmbPort text to the first COM port detected Baud_cmbox.Text = Baud_cmbox.Items.Item(0) 'Set cmbBaud text to the first Baud rate on the list LogOff_btn.Enabled = False 'Initially Disconnect Button is Disabled End Sub Private Sub LogOn_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogOn_btn.Click If (String.IsNullOrEmpty(Port_cmbox.Text) Or String.IsNullOrEmpty(Baud_cmbox.Text)) Then MessageBox.Show("Non hai selezionato la Porta e/o il Baud Rate", "Attenzione", MessageBoxButtons.OK, MessageBoxIcon.Warning) Else SerialPort1.PortName = Port_cmbox.Text 'Set SerialPort1 to the selected COM port at startup SerialPort1.BaudRate = Baud_cmbox.Text 'Set Baud rate to the selected value on 'Other Serial Port Property SerialPort1.Parity = IO.Ports.Parity.None SerialPort1.StopBits = IO.Ports.StopBits.One SerialPort1.DataBits = 8 'Open our serial port Try SerialPort1.Open() open = True Catch ex As Exception MessageBox.Show(ex.Message) End Try LogOn_btn.Enabled = False 'Disable Connect button LogOff_btn.Enabled = True 'and Enable Disconnect button End If End Sub Private Sub LogOff_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogOff_btn.Click SerialPort1.Close() 'Close our Serial Port LogOn_btn.Enabled = True LogOff_btn.Enabled = False End Sub Private Sub Send_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Send_btn.Click If (open) Then Try SerialPort1.WriteLine("AT+CSQ") 'MessageBox.Show(SerialPort1.ReadExisting()) 'The text contained in the txtText will be sent to the serial port as ascii 'plus the carriage return (Enter Key) the carriage return can be ommitted if the other end does not need it Catch ex As Exception MessageBox.Show(ex.Message) End Try Else MessageBox.Show("Nessuna connessione stabilita", "Attenzione", MessageBoxButtons.OK, MessageBoxIcon.Warning) End If End Sub Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived MessageBox.Show(SerialPort1.ReadExisting()) ReceivedText(SerialPort1.ReadExisting()) 'Automatically called every time a data is received at the serialPort End Sub Private Sub ReceivedText(ByVal [text] As String) 'compares the ID of the creating Thread to the ID of the calling Thread If Me.Received_rtbox.InvokeRequired Then Dim x As New SetTextCallback(AddressOf ReceivedText) Me.Invoke(x, New Object() {(text)}) Else Me.Received_rtbox.Text &= [text] End If End Sub Private Sub Port_cmbox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Port_cmbox.SelectedIndexChanged If SerialPort1.IsOpen = False Then SerialPort1.PortName = Port_cmbox.Text 'pop a message box to user if he is changing ports Else 'without disconnecting first. MsgBox("Valid only if port is Closed", vbCritical) End If End Sub Private Sub Baud_cmbox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Baud_cmbox.SelectedIndexChanged If SerialPort1.IsOpen = False Then SerialPort1.BaudRate = Baud_cmbox.Text 'pop a message box to user if he is changing baud rate Else 'without disconnecting first. MsgBox("Valid only if port is Closed", vbCritical) End If End Sub Private Sub SerialPort1_ErrorReceived(sender As System.Object, e As System.IO.Ports.SerialErrorReceivedEventArgs) Handles SerialPort1.ErrorReceived MessageBox.Show("Errore", "Attenzione", MessageBoxButtons.OK, MessageBoxIcon.Warning) End Sub End Class
I use only one form that was designed as simply app
two button for connect and disconnect, two combobox for com and baud selection and a textbox where you can write the message ...
my problem is that the line of code
SerialPort1.WriteLine("AT+CSQ") MessageBox.Show(SerialPort1.ReadExisting())doesn't working :( i dunno why....
解决方案With the two lines of code you show, they are executed one right after the other. Some devices require a little time to process the data before sending a reply.
Also you have a DataReceived event which should capture the data being returned from the device when it does. So the second part of the problem code is redundant anyway.
[edit]
Add following right underDim open As Boolean
to your list of declarations, it is missing from your code.
WithEvents SerialPort1 As New SerialPort
Delete these two subs, they are not needed.
Private Sub Port_cmbox_SelectedIndexChanged Private Sub Baud_cmbox_SelectedIndexChanged
Remove the message box line.
Private Sub SerialPort1_DataReceived(ByVal sender As ObjectMessageBox.Show(SerialPort1.ReadExisting())
I tested the code, it should work for you now.
You WILL need to use a delegate as the serial_DataReceive function executes on a seperate thread.
Add a Delegate to the top of your code.
Private Delegate Sub SPReceiver(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
Private Sub SP_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SP.DataReceived If Me.InvokeRequired = True Then Me.Invoke(New SPReceiver(AddressOf SP_DataReceived), sender, e) Exit Sub End If dim DataRec As String = SP.ReadExisting End sub
yes i use messabox to debug .. i know it's useless (the messagebox)
这篇关于VB.NET 2010 SerialPort和AT命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!