如何将手机与电脑连接

时间:2012-01-21 02:48:02

标签: c# winforms mobile

我正在做一个桌面学术项目这个项目的要求之一就是通过短信来控制移动用户,这个短信应该由与PC连接的手机发送。我不知道怎样才能做到这一点。当我在谷歌搜索这里时,我得到了gsm调制解调器网关一种通过电脑发送短信的解决方案。但他们不是免费的。然后诺基亚Conectivity SDK是另一种方式,但它与Visual Studio2010不兼容。我在一个网站上得到了这个例子,但发件人最后说我的代码中有错误。

Option Explicit On

Imports System
Imports System.Threading
Imports System.ComponentModel
Imports System.IO.Ports

Public Class form1



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.load


End Sub


Private WithEvents SMSPort As SerialPort

Private SMSThread As Thread
Private ReadThread As Thread
Shared _Continue As Boolean = False
Shared _ContSMS As Boolean = False
Private _Wait As Boolean = False
Shared _ReadPort As Boolean = False
Public Event Sending(ByVal Done As Boolean)
Public Event DataReceived(ByVal Message As String)

Public Sub New(ByRef COMMPORT As String)
SMSPort = New SerialPort
With SMSPort
.PortName = COMMPORT
.BaudRate = 9600
.Parity = Parity.None
.DataBits = 8
.StopBits = StopBits.One
.Handshake = Handshake.RequestToSend
.DtrEnable = True
.RtsEnable = True
.NewLine = vbCrLf
End With
ReadThread = New Thread(AddressOf ReadPort)
End Sub

Public Function SendSMS(ByVal CellNumber As String, ByVal SMSMessage As String) As Boolean
Dim MyMessage As String = Nothing
'Check if Message Length <= 160

If SMSMessage.Length <= 160 Then
MyMessage = SMSMessage
Else
MyMessage = Mid(SMSMessage, 1, 160)
End If
If IsOpen = True Then
SMSPort.WriteLine("AT+CMGS=" & CellNumber & vbCr)
_ContSMS = False
SMSPort.WriteLine(MyMessage & vbCrLf & Chr(26))
_Continue = False
RaiseEvent Sending(False)
End If
End Function

Private Sub ReadPort()
Dim SerialIn As String = Nothing
Dim RXBuffer(SMSPort.ReadBufferSize) As Byte
Dim SMSMessage As String = Nothing
Dim Strpos As Integer = 0
Dim TmpStr As String = Nothing

While SMSPort.IsOpen = True
If (SMSPort.BytesToRead <> 0) And (SMSPort.IsOpen = True) Then
While SMSPort.BytesToRead <> 0
SMSPort.Read(RXBuffer, 0, SMSPort.ReadBufferSize)
SerialIn = SerialIn & System.Text.Encoding.ASCII.GetString(RXBuffer)
If SerialIn.Contains(">") = True Then
_ContSMS = True
End If
If SerialIn.Contains("+CMGS:") = True Then
_Continue = True
RaiseEvent Sending(True)
_Wait = False
SerialIn = String.Empty
ReDim RXBuffer(SMSPort.ReadBufferSize)
End If
End While
RaiseEvent DataReceived(SerialIn)
SerialIn = String.Empty
ReDim RXBuffer(SMSPort.ReadBufferSize)
End If
End While
End Sub

Public ReadOnly Property IsOpen() As Boolean
Get
If SMSPort.IsOpen = True Then
IsOpen = True
Else
IsOpen = False
End If
End Get
End Property

Public Sub Open()
If IsOpen = False Then
SMSPort.Open()
ReadThread.Start()
End If
End Sub

Public Sub Close()
If IsOpen = True Then
'SMSPort = New SMSCOMMS("COM1")
'SMSEngine.Open()
'SMSEngine.SendSMS("919888888888", "SMS Testing")
'SMSEngine.Close()

SMSPort.Close()
End If
End Sub

End Class

但它在vb.net中我没有命令。它有用吗?

请给我一些想法,例如我使用c sharp在winform中进行。

2 个答案:

答案 0 :(得分:4)

嘿,我使用了GSMCOMM库,这对我来说可以使用计算机发送和接收短信。从谷歌下载并查看其示例,以检查它是如何工作的。 这里有一些有用的链接 GSMCOMM Send and recieve SMS Using GSM Modem

from here you can download GSMCOMM library

答案 1 :(得分:0)

我不确定你是想要一种连接电话的方法,也不想从电脑发送短信的方式,所以我会同时给它们。

要连接到手机(如果它有数据和Android或IOS或可以直接修改代码的东西),您可以创建从PC到手机的套接字连接。我以前在android aps中做过这个并且效果很好。

如果您只需要从计算机发送短信,并且您知道运营商只需使用此列表发送电子邮件http://www.mutube.com/projects/open-email-to-sms/gateway-list/