GSM-串口和GPRS-网口通信

本文介绍了一种使用Visual Basic实现的串口通信方法,并详细解释了如何进行十六进制与字符数据之间的转换,包括设置COM端口参数、判断发送数据格式、十六进制与中文之间的相互转换等。

Option Explicit

'************************************************
'******************COM 端口设置******************
'************************************************
Public Function COMSet(CommObj As MSComm, ByVal mPort As String, ByVal mSet As String) As Boolean
    On Error GoTo Err
    If CommObj.PortOpen = True Then CommObj.PortOpen = False
    CommObj.InBufferSize = 1024
    CommObj.OutBufferSize = 512
    CommObj.CommPort = mPort
    CommObj.Settings = mSet
    CommObj.PortOpen = True
    CommObj.RThreshold = 1
    CommObj.NullDiscard = False
    CommObj.InputLen = 0
    CommObj.SThreshold = 1
    CommObj.InputMode = comInputModeBinary
    COMSet = True
    Exit Function
Err:
    COMSet = False
End Function

'************************************************
'****************十六进制转十进制****************
'************************************************
Public Function HexToDec(HexValue As Variant) As Variant
    Dim LowValue, HighValue As String
    
    If Len(HexValue) = 1 Then
       HexValue = "0" & HexValue
    End If
    Select Case Left(HexValue, 1)
           Case "A"
                LowValue = "10"
           Case "B"
                LowValue = "11"
           Case "C"
                LowValue = "12"
           Case "D"
                LowValue = "13"
           Case "E"
                LowValue = "14"
           Case "F"
                LowValue = "15"
           Case Else
                LowValue = Left(HexValue, 1)
    End Select
    Select Case Right(HexValue, 1)
           Case "A"
                HighValue = "10"
           Case "B"
                HighValue = "11"
           Case "C"
                HighValue = "12"
           Case "D"
                HighValue = "13"
           Case "E"
                HighValue = "14"
           Case "F"
                HighValue = "15"
           Case Else
                HighValue = Right(HexValue, 1)
    End Select
    HexToDec = Val(LowValue) * 16 + Val(HighValue)
End Function

'************************************************
'***********判断发送数据是否是十六进制***********
'************************************************
Public Function OpinHEX(strobj As String) As Boolean
    Dim i As Long
    If Len(strobj) Mod 2 = 0 Then
        OpinHEX = True
    Else
        OpinHEX = False
        Exit Function
    End If
    For i = 1 To Len(strobj)
        If (Asc(Mid(strobj, i, 1)) >= 48 And Asc(Mid(strobj, i, 1)) <= 57) Or (Asc(Mid(strobj, i, 1)) >= 65 And Asc(Mid(strobj, i, 1)) <= 70) Then
            OpinHEX = True
        Else
            OpinHEX = False
            Exit Function
        End If
    Next
End Function

'************************************************
'***将接收到的十六进制数据转换为中文***
'************************************************
Public Function HexToChinese_RHR(DataStr As String) As String
    Dim i As Long, j As Long
    i = Len(DataStr) \ 4
    For j = 0 To i - 1
        HexToChinese_RHR = HexToChinese_RHR & ChrW(HexToDec(Mid(DataStr, 1 + 4 * j, 2)) * 256 + HexToDec(Mid(DataStr, 1 + 2 + 4 * j, 2)))
    Next
End Function

'************************************************
'****将接收到的十六进制数据转换为中文(标准)****
'************************************************
Public Function HexToChinese_Stand(DataStr As String) As String
    Dim i As Long, j As Long
    i = Len(DataStr) \ 4
    For j = 0 To i - 1
        HexToChinese_Stand = HexToChinese_Stand & Chr(HexToDec(Mid(DataStr, 1 + 4 * j, 2)) * 256 + HexToDec(Mid(DataStr, 1 + 2 + 4 * j, 2)))
    Next
End Function

'************************************************
'********将接收到的中文转换为十六进制数据********
'************************************************
Public Function ChineseToHex(DataStr As String) As String
    Dim i As Long, j As Long
    Dim DataStr_Tmp As String
    
    For i = 0 To Len(DataStr) - 1
        DataStr_Tmp = Hex(AscW(Mid(DataStr, i + 1, 1)))
        For j = 1 To 4 - Len(DataStr_Tmp)
            DataStr_Tmp = "0" & DataStr_Tmp
        Next
        ChineseToHex = ChineseToHex & DataStr_Tmp
    Next
End Function

'************************************************
'**************将数据转换为十六进制**************
'************************************************
Public Function HEXData(DataStr As String) As String
    Dim DataStr_Tmp As String
    Dim SendData As String
    Dim i As Long
    
    '去掉字符串中的空格
    DataStr_Tmp = DelBlank(DataStr)
    '转换为十进制
    For i = 1 To Len(DataStr_Tmp) \ 2
        SendData = SendData & Chr(HexToDec(Mid(DataStr_Tmp, 2 * (i - 1) + 1, 2)))
    Next
    HEXData = SendData
End Function

'************************************************
'************以GSM按 十六进制发送数据************
'************************************************
Public Function GSM_SendHEX(DataStr As String)
    Dim DataStr_Tmp As String
    Dim SendData() As Byte
    Dim i As Long
    
    '去掉字符串中的空格
    DataStr_Tmp = DelBlank(DataStr)
    '发送数据
    ReDim SendData(Len(DataStr_Tmp) \ 2 - 1) As Byte
    For i = 1 To Len(DataStr_Tmp) \ 2
        SendData(i - 1) = HexToDec(Mid(DataStr_Tmp, 2 * (i - 1) + 1, 2))
    Next
    
    FrmMain.MSComm1.Output = SendData
End Function

'************************************************
'*************以GSM按ASCII码发送数据*************
'************************************************
Public Function GSM_SendASCII(DataStr As String)
    Dim DataStr_Tmp As String
    Dim SendData() As Byte
    Dim i As Long
    
    '去掉字符串中的空格
    DataStr_Tmp = DelBlank(DataStr)
    '发送数据
    ReDim SendData(Len(DataStr_Tmp) - 1) As Byte
    For i = 1 To Len(DataStr_Tmp)
        SendData(i - 1) = Asc(Mid(DataStr_Tmp, i, 1))
    Next
    
    FrmMain.MSComm1.Output = SendData
End Function

'************************************************
'********以GPRS按十六进制发送数据 (TCP)********
'************************************************
Public Function GPRS_SendHEX_TCP(DataStr As String, IDString As String)
    Dim DataStr_Tmp As String
    Dim SendData() As Byte
    Dim i As Long
    
    '去掉字符串中的空格
    DataStr_Tmp = DelBlank(DataStr)
    '发送数据
    ReDim SendData(Len(DataStr_Tmp) \ 2 - 1) As Byte
    For i = 1 To Len(DataStr_Tmp) \ 2
        SendData(i - 1) = HexToDec(Mid(DataStr_Tmp, 2 * (i - 1) + 1, 2))
    Next
    FrmMain.Winsock2(IDString).SendData SendData
End Function

'************************************************
'*********以GPRS按ASCII码发送数据(TCP)*********
'************************************************
Public Function GPRS_SendASCII_TCP(DataStr As String, IDString As String)
    Dim DataStr_Tmp As String
    Dim i As Long
    
    '转换数据
    DataStr_Tmp = DataStr
    
    FrmMain.Winsock2(IDString).SendData DataStr_Tmp
End Function

'************************************************
'********以GPRS按十六进制发送数据 (UDP)********
'************************************************
Public Function GPRS_SendHEX_UDP(DataStr As String, IDString As String)
    Dim DataStr_Tmp As String
    Dim SendData() As Byte
    Dim i As Long
    
    '去掉字符串中的空格
    DataStr_Tmp = DelBlank(DataStr)
    '发送数据
    ReDim SendData(Len(DataStr_Tmp) \ 2 - 1) As Byte
    For i = 1 To Len(DataStr_Tmp) \ 2
        SendData(i - 1) = HexToDec(Mid(DataStr_Tmp, 2 * (i - 1) + 1, 2))
    Next
    FrmMain.Winsock3(IDString).SendData SendData
End Function

'************************************************
'*********以GPRS按ASCII码发送数据(UDP)*********
'************************************************
Public Function GPRS_SendASCII_UDP(DataStr As String, IDString As String)
    Dim DataStr_Tmp As String
    Dim i As Long
    
    '转换数据
    DataStr_Tmp = DataStr
    
    FrmMain.Winsock3(IDString).SendData DataStr_Tmp
End Function

Option Explicit Public g_blIsListSMS As Boolean Public g_blIsSysBusy As Boolean Public g_blIsWaiting As Boolean Public g_blIsNewSMSIn As Boolean Public g_blIsSendingSMS As Boolean Public g_blIsHexCommData As Boolean Public g_blMaySaveAllSMS As Boolean Public g_blwav As Boolean Public g_strSave As String Public g_strThisAT As String Public g_strLastAT As String Public g_strLatestAT As String Public n_CountListSMS As Long Public g_SysInfo As SysStruct Public TheForm As Form Public TheMenu As Menu Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'VB延时API函数 Public Declare Sub winhelp Lib "user32" (ByVal hWnd As Long, ByVal lphelpfile As String, ByVal wcommand As Long, ByVal dwData As Long) '调用帮助API函数 Public Declare Function htmlHelp Lib "hhctrl.ocx" Alias "HtmlHelpA" (ByVal hwndCaller As Long, ByVal pszFile As String, ByVal uCommand As Long, ByVal dwData As Long) As Long Public Const HH_DISPLAY_INDEX = 2 Public Const HH_DISPLAY_TOC = 1 Public Const HH_DISPLAY_TOPIC = 0 Type SysStruct CommPort As Integer Baud As String ServiceNo As String DestNo As String SMSMelody As Integer Clock As Boolean ClockSet As String End Type Type SMSDef ListOrRead As Boolean '是否用列举(List)方法读取 SmsIndex As Long SourceNo As String ReachDate As String ReachTime As String SmsMain As String End Type '功能:解析串口中AT命令返回的信息 '输入参数: 串口数据 '输出参数: 'strATData: 短消息内容(UD) 'iSMSIdx: 短消息序号 'strSMSTime: 短消息接收时间 'strSMSSourceNO:源SIM卡号 'strSMSStatus: 短消息状态:"READ"—已读,"UNREAD"—未读,"SENT"—发送,"UNSENT"—未发送 Public Function GetDataFromCommPort(ByVal strInput As String, strATData As String, Optional CommandStatus As String, Optional TimedOut As Boolean, Optional iSMSIdx As Integer, Optional strSMSTime As String, Optional strSMSSourceNO As String, Optional strSMSStatus As String) As Boolean On Error GoTo ErrorG Dim strTmp As String, strTmp1 As String
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值