我有一个GSM调制解调器(mmx610u)连接到我服务器的USB端口。
我使用以下代码发送短信。代码工作正常,但当有多个收件人时,短信仅发送给前1或2个收件人。
请建议我,代码中有什么问题?
Public Shared Function SendSMSMobile(ByVal MobNo As String, ByVal MessageBody As String) As Boolean
Dim pNo As String() = New String(-1) {}
Dim IsSend As Boolean = False
Try
Dim PortNo As String = ""
Dim mos As New ManagementObjectSearcher("SELECT * FROM Win32_POTSModem")
For Each mo As ManagementObject In mos.[Get]()
Dim COMPort As String = mo("AttachedTo").ToString()
Dim command As [String] = "AT"
Dim serialPort As SerialPort = Nothing
Try
serialPort = New SerialPort()
serialPort.PortName = COMPort
serialPort.BaudRate = 9600
serialPort.DataBits = 8
serialPort.Parity = Parity.None
serialPort.ReadTimeout = 300
serialPort.WriteTimeout = 300
serialPort.StopBits = StopBits.One
serialPort.Handshake = Handshake.None
serialPort.Open()
If serialPort.IsOpen = True Then
PortNo = COMPort
Array.Resize(pNo, pNo.Length + 1)
pNo(pNo.Length - 1) = PortNo
End If
Catch
Finally
serialPort.Close()
serialPort.Dispose()
End Try
Next
If PortNo.Trim().Length > 0 Then
If MobNo.Substring(0, 1) = "0" AndAlso MobNo.Length > 5 Then
MobNo = MobNo.Substring(1, MobNo.Length - 1)
End If
If MobNo.Trim().Length >= 10 AndAlso PortNo.Length > 0 Then
Dim MsgLength As Integer = Convert.ToInt32(MessageBody.Length)
Dim Q__1 As Integer = MsgLength \ 260
Dim R As Integer = MsgLength Mod 260
If R > 0 Then
Q__1 = Q__1 + 1
End If
For i As Integer = 0 To Q__1 - 1
Dim StartIndex As Integer = i * 260
Dim EndIndex As Integer = 260
If i = Q__1 - 1 Then
EndIndex = R
End If
Dim Msg As String = MessageBody.Substring(StartIndex, EndIndex)
Dim sp As New SerialPort()
For q As Integer = 0 To pNo.Length - 1
'#Region "MOBILE"
Try
Dim arr As Char() = New Char(0) {}
arr(0) = ChrW(26)
sp.PortName = pNo(q).ToString()
sp.BaudRate = 96000
sp.Parity = Parity.None
sp.DataBits = 8
sp.StopBits = StopBits.One
sp.Handshake = Handshake.XOnXOff
sp.DtrEnable = True
sp.RtsEnable = True
sp.NewLine = Environment.NewLine
sp.Open()
Dim mSpeed As Integer = 2000
sp.Write("AT+CMGF=1" + Environment.NewLine)
System.Threading.Thread.Sleep(200)
sp.Write("AT+CSCS=GSM" + Environment.NewLine)
System.Threading.Thread.Sleep(200)
sp.Write(("AT+CMGS=" & ChrW(34) & MobNo & ChrW(34)) + Environment.NewLine)
System.Threading.Thread.Sleep(200)
sp.Write(Msg & ChrW(26))
System.Threading.Thread.Sleep(mSpeed)
IsSend = True
Catch
IsSend = False
Finally
sp.Close()
sp.Dispose()
End Try
Next
Next
End If
End If
Catch
IsSend = False
End Try
Return IsSend
End Function