“收到的回复不完整”
所以我将这个发送sms代码的语法从c#转换为vb net,这样我就可以重用它。 代码有时会起作用,但由于某些原因,它通常会给我“收到的响应不完整”异常。 我想也许我的代码处理命令比我的GSM设备更快,可以处理,所以我试图增加串口的超时响应,但仍然以此异常结束。
你认为什么是问题,你会推荐我做什么来解决它?
Public Class SmsHelper Public receiveNow As AutoResetEvent #Region "Open and Close Ports" 'Open Port Public Function OpenPort(portName As String, baudRate As Integer, dataBits As Integer, readTimeout As Integer, writeTimeout As Integer) As SerialPort receiveNow = New AutoResetEvent(False) Dim port As New SerialPort() Try port.PortName = portName 'COM1 port.BaudRate = baudRate '9600 port.DataBits = dataBits '8 port.StopBits = StopBits.One '1 port.Parity = Parity.None 'None port.ReadTimeout = readTimeout '300 port.WriteTimeout = writeTimeout '300 port.Encoding = Encoding.GetEncoding("iso-8859-1") port.NewLine = vbCrLf AddHandler port.DataReceived, AddressOf port_DataReceived port.Open() port.DtrEnable = True port.RtsEnable = True Catch ex As Exception Throw ex End Try Return port End Function ' Send AT Command Public Function SendATCommand(port As SerialPort, command As String, responseTimeout As Integer, errorMessage As String) As String Try port.DiscardOutBuffer() port.DiscardInBuffer() receiveNow.Reset() port.Write(command & Convert.ToString(vbCrLf)) Dim input As String = ReadResponse(port, responseTimeout) Console.WriteLine("Received data is " & input) If (input.Length = 0) OrElse ((Not input.EndsWith(vbCr & vbLf & "> ")) AndAlso (Not input.EndsWith(vbCr & vbLf & "OK" & vbCr & vbLf))) Then Throw New ApplicationException("No success message was received.") End If Return input Catch ex As Exception Throw ex Finally End Try End Function 'Receive data from port Public Sub port_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Try If e.EventType = SerialData.Chars Then receiveNow.[Set]() End If Catch ex As Exception Throw ex End Try End Sub Public Function ReadResponse(port As SerialPort, timeout As Integer) As String Dim serialPortData As String = "" Try Do If receiveNow.WaitOne(timeout, False) Then Dim data As String = port.ReadLine() serialPortData += data Else 'Console.WriteLine("SerialPortData data is " & serialPortData) If serialPortData.Length > 0 Then Console.WriteLine("SerialPortData is " & serialPortData.ToString) Throw New ApplicationException("Response received is incomplete.") Else Throw New ApplicationException("No data received from phone.") End If End If Loop While Not serialPortData.EndsWith(vbCr & vbLf & "OK" & vbCr & vbLf) AndAlso Not serialPortData.EndsWith(vbCr & vbLf & "> ") AndAlso Not serialPortData.EndsWith(vbCr & vbLf & "ERROR" & vbCr & vbLf) Catch ex As Exception Throw ex End Try Return serialPortData End Function Shared readNow As New AutoResetEvent(False) Public Function SendMessage(port As SerialPort, phoneNo As String, message As String) As Boolean Dim isSend As Boolean = False Try Dim recievedData As String = SendATCommand(port, "AT", 3000, "No phone connected") Dim command As String = "AT+CMGF=1" + Char.ConvertFromUtf32(13) recievedData = SendATCommand(port, command, 3000, "Failed to set message format.") ' AT Command Syntax - http://www.smssolutions.net/tutorials/gsm/sendsmsat/ command = (Convert.ToString("AT+CMGS=""") & phoneNo) + """" + Char.ConvertFromUtf32(13) recievedData = SendATCommand(port, command, 3000, "Failed to accept phoneNo") 'wait(2) command = message & Char.ConvertFromUtf32(26) recievedData = SendATCommand(port, command, 3000, "Failed to send message") Console.WriteLine("Received data is " & recievedData) If recievedData.EndsWith(vbCr & vbLf & "OK" & vbCr & vbLf) Then isSend = True ElseIf recievedData.Contains("ERROR") Then isSend = False End If Return isSend Catch ex As Exception Throw ex End Try End Function Private Shared Sub DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Try If e.EventType = SerialData.Chars Then readNow.[Set]() End If Catch ex As Exception Throw ex End Try End Sub #End Region End Class链接地址: http://www.djcxy.com/p/42835.html
上一篇: "Response received is incomplete"
下一篇: Syntax error in UPDATE statement vb.net in access database