"Response received is incomplete"

So I reversed the syntax of this send sms code from c# to vb net so I can reuse it. The code works sometimes, but for some reason it would often give me the "response received is incomplete" exception. I was thinking perhaps my code is processing commands faster than my GSM device, could handle, so I tried increasing the timeout response for the serial port, still ends up with this exception.

What do you think is the problem and what would you recommend I do to solve it?

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/42836.html

上一篇: 在VB.NET中合并运算符和条件运算符

下一篇: “收到的回复不完整”