我希望尽可能发送数据并以字符串形式接收数据,或者至少作为整数

时间:2013-04-20 11:20:15

标签: vb6 port mscomm32

  1. 我正在尝试通过mscomm端口发送和接收数据,但是我接收数据类型mismatch.so我该怎么做才能避免这个错误,我需要发送数据作为整数并尽可能接收它作为字符串,最小我应该把它作为整数数据类型。以下是我的代码,请帮我解决这个问题。

  2. 独立运行时,发送和接收代码正常工作。

  3. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Private Sub Command1_Click()
        If (MSComm1.PortOpen = False) Then
            MSComm1.PortOpen = True
        End If
        Command1.Enabled = False
        Command2.Enabled = True
        Text3.Text = "COM1, Baud - 9600, Databit - 8, Parity - None, Stopbit - 1....CONNECTED." & Text3.Text
    End Sub
    
    Private Sub Command2_Click()
        If (MSComm1.PortOpen = True) Then
            MSComm1.PortOpen = False
        End If
        Command1.Enabled = True
        Command2.Enabled = False
        Text3.Text = "DISCONNECTED" & Text3.Text
    End Sub
    
    Private Sub Command3_Click()
        Text1.Text = " "
        Text2.Text = " "
        Text3.Text = "CLEARED" & Text3.Text
    End Sub
    
    Private Sub Command4_Click()    
        MSComm1.Output = Text2.Text
        Text3.Text = "SENDING" & Text3.Text
    End Sub
    
    Private Sub Command5_Click()
        Text3.Text = " "
    End Sub
    
    Private Sub Form_Load()
        MSComm1.CommPort = 1
        MSComm1.Settings = "9600,N,8,1"
        MSComm1.DTREnable = True
        MSComm1.Handshaking = comRTS
        MSComm1.InBufferSize = 2
        MSComm1.RThreshold = MSComm1.InBufferSize
        MSComm1.RTSEnable = True
        MSComm1.InputLen = 2
        MSComm1.InputMode = comInputModeText
        MSComm1.NullDiscard = True
        MSComm1.OutBufferSize = 2
        MSComm1.SThreshold = MSComm1.OutBufferSize    
        MSComm1.PortOpen = True  
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        If (MSComm1.PortOpen = True) Then
            MSComm1.PortOpen = False
        End If
    End Sub
    
    Private Sub MSComm1_OnComm()
        Dim Buffer As String
        Select Case MSComm1.CommEvent
            Case comEvReceive
                'Text1.Text = " "
                Buffer = Cstr(MSComm1.Input)
                Text1.Text = Buffer
        End Select
    End Sub
    

1 个答案:

答案 0 :(得分:0)

我认为主要的问题是您在不尝试传输控制的情况下异步发送数据。由于要传输多个字符,您需要告诉接收器新传输的开始位置,以便找到正确的偏移量。

在您的情况下,您说您只想发送0..100范围内的值。通过使用0到255范围内的单个字节值,可以在每种情况下发送每个值的单个字节,对于单个字符,不需要进行转换控制。

这是一个将数字转换为单个字节,发送并重新转换为数字的示例:

Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub cmdOpen_Click()
    If (MSComm1.PortOpen = False) Then
        MSComm1.PortOpen = True
    End If
    cmdOpen.Enabled = False
    cmdClose.Enabled = True
    txtState.Text = "COM1, Baud - 9600, Databit - 8, Parity - None, Stopbit - 1....CONNECTED." & txtState.Text
End Sub

Private Sub cmdClose_Click()
    If (MSComm1.PortOpen = True) Then
        MSComm1.PortOpen = False
    End If
    cmdOpen.Enabled = True
    cmdClose.Enabled = False
    txtState.Text = "DISCONNECTED" & txtState.Text
End Sub

Private Sub cmdClear_Click()
    txtReceived.Text = " "
    txtSend.Text = " "
    txtState.Text = "CLEARED" & txtState.Text
End Sub

Private Sub cmdSend_Click()
Dim Number As Byte
    'some checking - needed depending on source of data
    If Not IsNumeric(txtSend.Text) Then
        MsgBox "only numbers (from 0 to 255)"
        Exit Sub
    End If
    If Val(txtSend.Text) < 0 Or Val(txtSend.Text) > 255 Then
        MsgBox "out of range (from 0 to 255)"
        Exit Sub
    End If

    Number = CByte(Val(txtSend.Text))
    MSComm1.Output = Chr(Number)
    txtState.Text = "SENDING" & txtState.Text
End Sub

Private Sub cmdClearState_Click()
    txtState.Text = " "
End Sub

Private Sub Form_Load()
    MSComm1.CommPort = 1
    MSComm1.Settings = "9600,N,8,1"
    MSComm1.DTREnable = True
    MSComm1.Handshaking = comNone 'comRTS i only got a 3-wire connection - so no handshaking
    MSComm1.InBufferSize = 1024 'not 2 we don't want a buffer overflow THAT fast
    MSComm1.RThreshold = 1 'Raise OnComm-Event if 1 character is in Rx-buffer
    MSComm1.RTSEnable = True
    MSComm1.InputLen = 1 'get one character at a time
    MSComm1.InputMode = comInputModeText 'the Binary mode never worked out for me - even when transmitting binary data
    MSComm1.NullDiscard = False 'also NULL-Characters will be received
    MSComm1.OutBufferSize = 512 'again we don't want overflow
    MSComm1.SThreshold = 0 'don't hold back data - blow them out directly
    MSComm1.PortOpen = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If (MSComm1.PortOpen = True) Then
        MSComm1.PortOpen = False
    End If
End Sub

Private Sub MSComm1_OnComm()
    Dim Buffer As String
    Select Case MSComm1.CommEvent
        Case comEvReceive
            'txtReceived.Text = " "
            Buffer = MSComm1.Input
            txtReceived.Text = Asc(Buffer)
            txtState.Text = "RX" + txtState.Text
    End Select
End Sub