如何使用vb.net从加密狗发送消息到移动设备? [英] How to send a message from dongle to mobile using vb.net?

查看:74
本文介绍了如何使用vb.net从加密狗发送消息到移动设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..



如何使用vb.net从加密狗向手机发送消息?

我收到的错误是是进程无法访问端口'com7',因为它被另一个进程使用。我的编码如下...



Hi Everyone..

How to send a message from dongle to mobile using vb.net?
I am getting error that is The process cannot access the port 'com7' because it is being used by another process.My coding is below...

Option Explicit On
Imports System
Imports System.Threading
Imports System.ComponentModel
Imports System.IO.Ports
Public Class Class1
    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 & vbCrLf)
            _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.Close()
        End If
    End Sub
End Class










Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim aa As New Class1("COM7")

        'aa = New Class1("COM7")
        aa.Open()
        aa.SendSMS("919888888888", "SMS Testing")
        aa.Close()

    End Sub
End Class

推荐答案

这篇关于如何使用vb.net从加密狗发送消息到移动设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆