本文介绍了我在使用rs232发送和编码的编码方面遇到了问题。收到数据..请帮助! !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai friends !!我在编写有关如何通过rs232发送和接收数据时遇到问题。我正在尝试循环回程过程,好像我再次从我的笔记本电脑发送数据到rs232我应该通过端口接收数据到我的笔记本电脑。我正在使用visual basic 6.0进行编程。请帮助我编写如何编写。我尝试过一点,但它不起作用。



你的帮助意味着很多



谢谢。



我的尝试:



  Dim  Newdata  As  字符串 

私有 Sub Command35_Click()
Text21.Text = MSComm1.Input
结束 Sub

私有 Sub Command36_Click()
Text20 .Text = Val(MSComm1.Input)
结束 Sub

私有 Sub Form_Load()
MSComm1.S ettings = 9600,N,8,1
MSComm1.CommPort = 8
MSComm1.InputLen = 1
MSComm1.PortOpen = True
MSComm1.RThreshold = 1

Newdata =
结束 Sub

私有 Sub Form_Unload(取消 As Integer

MSComm1.PortOpen = False

结束 Sub

私有 Sub MSComm1_OnComm()
Dim Dat As String
Dim I As 整数 ' 每个传入的字符
Dim 接收作为 字符串 ' 每个收到char
Dim theinfo As 字符串
选择 案例 MSComm1.CommEvent
案例 comEventRxOver
案例 comEventTxFull
案例 comEvReceive

数据= MSComm1.Input
对于 I = 1 Len(Dat)
receive = Mid $(Dat,I, 1
如果 Asc(接收)= 13 那么
theinfo = Mid $(Newdata, 2 1
如果 theinfo = 然后
InformationDisplay.SelLength = 0
InformationDisplay.SelStart = Len(InformationDisplay.Text)
InformationDisplay.SelText = Newdata + vbCr + vbLf ' 包括CR和LF以与OutputDisplay中的下一行分开
InformationDisplay.SelLength = 0

ElseIf theinfo = P 然后
OutputDisplay.SelLength = 0
OutputDisplay.SelStart = Len(OutputDisplay.Text)
OutputDisplay.SelText = Newdata + vbCr + vbLf ' 包括CR和LF以与OutputDisplay中的下一行分开
OutputDisplay.SelLength = 0
结束 如果

如果 Asc(theinfo)= 73 那么
InformationDisplay.SelStart = Len(InformationDisplay.Text)
信息onDisplay.SelText = Newdata + vbCr + vbLf
Newdata = ' 清除NewData以便它可以汇编下一个数据包

Data.SelLength = 0
Data.SelStart = 2
Data.SelText = vbCrLf
Data.SelLength = 0

ElseIf Asc(theChar)<> 10 然后 ' 忽略换行符

Newdata = Newdata + theChar ' 已收到一个字符 - 将其附加到NewData

Data.SelLength = 0
Data.SelStart = Len(数据.Text)
Data.SelText = theChar
Data.SelLength = 0

结束 如果
结束 如果
下一步

案例 comEvSend
案例 comEvEOF
结束 选择
结束 Sub
解决方案




Hai friends!!i have a problem in coding about how to send and receive data through rs232. i am trying a loop back process as if i send data to rs232 from my laptop again i should receive the data through the port to my laptop. I am using visual basic 6.0 for programming.can u pls help me on coding how to write.I have tried a little,but it's not working.

Your help means alot

Thank You.

What I have tried:

Dim Newdata As String

Private Sub Command35_Click()
    Text21.Text = MSComm1.Input
End Sub

Private Sub Command36_Click()
    Text20.Text = Val(MSComm1.Input)
End Sub

Private Sub Form_Load()
    MSComm1.Settings = "9600,N,8,1"
    MSComm1.CommPort = 8
    MSComm1.InputLen = 1
    MSComm1.PortOpen = True
    MSComm1.RThreshold = 1

    Newdata = ""
End Sub

Private Sub Form_Unload(Cancel As Integer)

    MSComm1.PortOpen = False

End Sub

Private Sub MSComm1_OnComm()
    Dim Dat As String
    Dim I As Integer    'each incoming char
    Dim receive As String   'each received char
    Dim theinfo As String
    Select Case MSComm1.CommEvent
       Case comEventRxOver
       Case comEventTxFull
       Case comEvReceive
       
       Data = MSComm1.Input
       For I = 1 To Len(Dat)
        receive = Mid$(Dat, I, 1)
        If Asc(receive) = 13 Then
            theinfo = Mid$(Newdata, 2, 1)
    If theinfo = "I" Then
        InformationDisplay.SelLength = 0
        InformationDisplay.SelStart = Len(InformationDisplay.Text)
        InformationDisplay.SelText = Newdata + vbCr + vbLf   'include a CR and LF to separate from next line placed in OutputDisplay
        InformationDisplay.SelLength = 0
        
    ElseIf theinfo = "P" Then
        OutputDisplay.SelLength = 0
        OutputDisplay.SelStart = Len(OutputDisplay.Text)
        OutputDisplay.SelText = Newdata + vbCr + vbLf   'include a CR and LF to separate from next line placed in OutputDisplay
        OutputDisplay.SelLength = 0
    End If
    
    If Asc(theinfo) = 73 Then
        InformationDisplay.SelStart = Len(InformationDisplay.Text)
        InformationDisplay.SelText = Newdata + vbCr + vbLf
        Newdata = "" 'clear NewData so it can assemble the next packet
                    
            Data.SelLength = 0
            Data.SelStart = 2
            Data.SelText = vbCrLf
            Data.SelLength = 0
            
    ElseIf Asc(theChar) <> 10 Then  'ignore linefeeds

            Newdata = Newdata + theChar 'received a character -- append it to NewData

            Data.SelLength = 0
            Data.SelStart = Len(Data.Text)
            Data.SelText = theChar
            Data.SelLength = 0

    End If
    End If
    Next I
    
       Case comEvSend
       Case comEvEOF
    End Select
End Sub
解决方案




这篇关于我在使用rs232发送和编码的编码方面遇到了问题。收到数据..请帮助! !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 20:04