我正试图从GSM调制解调器发送消息。我可以提交AT命令,响应是确定,没有任何错误。但问题是我无法发送消息或阅读消息。
我已经实现了3个功能:
1。连接到端口:
Private Sub BtnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnConnect.Click
If SerialPort1.IsOpen Then
SerialPort1.Close()
BtnConnect.Text = "Connect"
Else
Try
With SerialPort1
.PortName = Trim(Mid(ComboBox1.Text, 1, 5))
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = Ports.StopBits.One
.Handshake = Ports.Handshake.None
.RtsEnable = True
.DtrEnable = True
.Open()
.WriteLine("AT+CNMI=1,2,0,0,0" & vbCrLf) 'send whatever data that it receives to serial port
End With
BtnConnect.Text = "Disconnect"
Catch ex As Exception
BtnConnect.Text = "Connect"
MsgBox(ex.Message)
End Try
End If
End Sub
2。阅读短信
Private Sub btn_read_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_read.Click
Try
SerialPort1.WriteLine("AT" & vbCrLf) 'is modem okay?
Thread.Sleep(1000)
SerialPort1.WriteLine("AT+CMGF=1" & vbCrLf) 'To format SMS as a TEXT message
Thread.Sleep(1000)
SerialPort1.WriteLine("AT+CPMS=""SM""" & vbCrLf) ' Select SIM storage
Threading.Thread.Sleep(1000)
SerialPort1.WriteLine("AT+CMGL=""REC UNREAD""" & vbCrLf) 'read unread messages
Threading.Thread.Sleep(1000)
SerialPort1.WriteLine("AT+CMGL=""ALL""" & vbCrLf) 'print all message
Threading.Thread.Sleep(1000)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
第3。发送短信
Private Sub btn_send_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_send.Click
Try
With SerialPort1
.WriteLine("AT" & vbCrLf)
Threading.Thread.Sleep(1000)
.WriteLine("AT+CMGF=1" & vbCrLf) 'Instruct the GSM / GPRS modem to operate in SMS text mode
Threading.Thread.Sleep(1000)
.WriteLine("AT+CMGS=""9802100355""" & vbCr) 'sender ko no. rakhne ho tyo txtnumber ma
Threading.Thread.Sleep(1000) 'thapeko
.WriteLine("This is test message" & vbCrLf & Chr(26)) 'txtmessage automatic huna parchha haina?
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
4。处理串行端口中收到的数据
Private Sub serialport1_datareceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'Pause while all data is read
System.Threading.Thread.Sleep(300)
rcvdata = SerialPort1.ReadExisting()
MsgBox(rcvdata, , "Response From AT")
rcvdata = ""
End Sub
我在哪里错过了什么?发送短信时,我收到CMS 500错误。使用调制解调器的软件,我能够读取和发送短信。但我需要在我的软件中实现自己的功能。
答案 0 :(得分:0)
此错误可能有多种原因。首先检查你的网络。第二组使用AT命令发送消息服务中心号码并保存此设置。希望这会对你有所帮助
答案 1 :(得分:0)
在第二个函数中,您可以尝试声明一个字符串变量来接收数据,如下所示:
With serialport1
rcvdata=""
.Write(All AT commands)
Threading.Thread.Sleep(1000)
Msgbox(rcvdata.Tostring)
End With
您可以向datareceived添加处理程序以读取所有字节:
Dim entrada As String = " "
Dim numeros As Integer = SerialPort1.BytesToRead
For i As Integer = 1 To numeros
entrada&= Chr(SerialPort1.ReadChar)
Next
chama(entrada)
Private Sub chama(ByVal dados As String)
rcvdata &= dados
End Sub
答案 2 :(得分:0)
AT+CMGS=""9802100355"
您的电话号码错误,这就是您收到错误500的原因,您需要在前面输入包括0
在内的完整电话号码。