2

我正在尝试在 VB6 中编写一个通过 COM 端口接受数据的代码。现在,一部 GSM 手机正在发送数据。数据可以是任何东西,包括电话或消息。我能够获得呼叫和消息的格式:

来电:

RING+CLIP: "+919823596784",145,"",,"",0

和消息:

+CMT: "AD-bytwoo",,"14/06/05,17:19:31+22"
9860939518:
Hi Hw r u

RThreshold现在的问题是,我每次都必须更改呼叫和消息的值。就像上面提到的MSComm1.Rthreshold = 47获取整个字符串和小消息的调用一样。MSComm1.RThreshold = 70对于调用,如果 RThreshold 小于或大于 47,则数据继续移动。无论我读过什么线程MSComm1,它都说RThreshold应该等于 1,因为MSComm1.Oncom事件将在接收到 1 个字符本身时触发,但我的代码不会发生这种情况。这是我的代码:

Dim str_1 As String
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)


Private Sub Form_Load()
    'On Error GoTo errx:
    Dim strValue As String '  define Buffer value from Modem
    MSComm1.CommPort = 6   'comm port no.
    MSComm1.InBufferSize = 100
    MSComm1.Handshaking = comNone
    MSComm1.Settings = "9600,n,8,1"
    MSComm1.RThreshold = 67    'no. of chr to receive
    MSComm1.InputLen = 0  '  no. of chr on which oncomm  event fires
    MSComm1.RTSEnable = True
    MSComm1.PortOpen = True  'open comm port
    ''MSComm1.Output = "AT + CLIP = 1" + Chr(13)
    'Sleep 1500
    'MSComm1.Output = "AT + CNUM" + Chr(13)
    'Sleep 1500
    'MSComm1.Output = "AT+CMGF=1" + Chr(13) '& Chr(10)
    'Sleep 500
    MSComm1.Output = "AT+CNMI=1,2" + Chr$(13)
    Sleep 500
    'Exit Sub
    'errx:
    'MsgBox "error"
End Sub

Private Sub MSComm1_OnComm()
    If MSComm1.CommEvent = comEvReceive Then
        If MSComm1.InBufferCount Then
            Text1.Text = MSComm1.Input
        End If
    End If
    MSComm1.InBufferCount = 0
    str_1 = Text1.Text
End Sub

如果MSComm1.RThreshold = 1,则不接收任何字符。谁能告诉我是什么问题?

4

4 回答 4

3

根据MSDNRThreshold属性:

设置并返回在 MSComm 控件将 CommEvent 属性设置为 comEvReceive 并生成 OnComm 事件之前要接收的字符数。

在冒泡到 OnComm 事件之前,由您决定应该读取多少个字符。在这种情况下,您必须保留一个字符缓冲区,在回车或 vbCRLF 处拆分它们(无论返回的数据结束其句子)。

举个简单的例子:

Private Sub MSComm1_OnComm 
Static Buffer As String 
Dim CRPosition As Integer
Dim wholeSentence as String

    Buffer = Buffer & MSComm1.Input 
    CRPosition = InStr(Buffer, vbCR) 
    If CRPosition > 1 Then 
        wholeSentence = Left$(Buffer, CRPosition - 1) 
        Buffer = Mid$(Buffer, CRPosition + 1) 
    End If 

End Sub 

该变量wholeSentence将包含一个完整的行(如果正在接收的字符串由回车符和换行符分隔,则将 vbCr 替换为 vbCRLF)。

由您决定 RThreshold 的合适值。对于 GPS,我使用了 150 个字符。出于您的目的,您可能希望使用尽可能长的句子长度(70?)。

于 2014-06-05T13:20:11.073 回答
1

报告的字符串不会总是相同的长度,并且 MSComm RThreshold 不是很准确。因此,仅在电话号码可用时解析它会更准确。我们使用一个名为Supercom的库,它提供所谓的“DataCollector”,以便接收自定义协议上的完整电报。SuperCom DataCollector完全在后台运行,能够为您提供通过连接(串行、tcp)接收到的任何数据,这些数据基于一些定义,如开始和结束,例如 Start="CLIP:" 和 End=","。您的应用程序不会浪费时间等待数据,而是会在可用时获取电话号码。是的,它是商业产品,但它可以运行几个月而不会失败。

于 2015-11-12T15:46:57.297 回答
0

你写了 MSComm1.InputLen = 0 ' 没有。of chr on which oncomm event fire 将此更改为 MSComm1.InputLen = 1 以触发 oncomm 事件,然后检查。Oncomm 事件将为每个字符触发。

于 2015-11-16T07:19:11.380 回答
-1

设置串口通讯

- ComPort mean is Port interfacing RS-232 (Com1,Com2) 
- Setting mean is Baud,Parity,Data(number of bits),Stop Ex. "1200,n,8,1 " 
- HandShaking mean is we can define to 4 type 1.comNone 2.comXonXoff 3. comRTS 4.comTRSXonXoff

使用 Buffer 接收和发送数据

-InBuffersize mean is define buffer for receive data.
-OutBuffersize mean is define buffer for send data.
-Rthreshold mean is define to occur in Event-driven for send data
-Sthreshold mean is define to occur in Event-driven for receive data
-Inputlen mean is number of data to read in a buffer receive data
-EOFEnable mean is End-Of-File(EOF)

关于硬件

- ParityReplace mean is character value instead of occur Parity Error 
- NullDiscard mean is define in receive or not "NULL CHARACTER"
- RTSEnablemean is define signal RTS (Request To Send)
- DTSEnablemean is define signal DTR(Data Terminal Ready)
于 2014-12-04T05:26:31.483 回答