Visual Basic中的串口通信问题 [英] Serial port communication problem in visual basic

查看:90
本文介绍了Visual Basic中的串口通信问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!请帮我解决这个问题。



情况是这样的,我在这个格式的串口上得到一个字符串: [00210] [00210],括号内的数字是以克为单位的重量,我必须将其转换为整数&将其与用户定义的值进行比较。



我写了一个代码来完成所有这些,但问题是当我用step into调试它时代码运行良好但是没有给出任何代码当我运行它时的结果。这是代码(抱歉转储完整代码,但我必须提供我正在做的完整信息)。



系统流程图如下:

1. RFID_validation->

2. switch_between_users->

3. get_user_values->

4. open_container_1 - >

5. wait_till_the_current_weight_is_equal_to_user_input

6.关闭容器1

7.打开容器5

8。等待一段时间(10秒)

9.关闭集装箱5

10.从第4步开始重复其他三个集装箱。

11. show流程完成通知。



代码从这里开始:

Hi everyone!, Please help me out on this code.

The situation is like this that i am getting a string on the serial port in this format: [00200][00210][00215], the numbers inside parenthesis is the weight in grams, i have to convert it to integer & compare it with the user defined value.

I have written a code to do all this but the problem is that the code works well when i am debugging it with "step into" but doesn't gives any result when i run it. here is the code (sorry to dump full code but i have to provide full information of what i am doing).

the system flowchart is like this:
1. RFID_validation->
2. switch_between_users->
3. get_user_values->
4. open_container_1->
5. wait_till_the_current_weight_is_equal_to_user_input
6. close container 1
7. open container 5
8. wait for sometime(10 seconds)
9. close container 5
10. repeat from step 4 for other three containers.
11. show notification for process completion.

Code starts form here:

Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)

Private Sub Command1_Click()
    LCCOM.CommPort = lcport.Text
    RFIDCOM.CommPort = rfidport.Text
    Label3.Caption = "Plaease Swipe your card and enter the desired quantity values"
    Call rfid_validation
End Sub

'RFID CARD VALUES
'24664=4A00C36058B1      'farmer 1
'36509=4A00C38E9D9A      'farmer 2
'36507=4A00C38E9B9C      'farmer 3


Private Sub Form_Load()

    LCCOM.RThreshold = 7
    LCCOM.InputLen = 0
    LCCOM.Settings = "2400,n,8,1"

    RFIDCOM.RThreshold = 12
    RFIDCOM.InputLen = 0
    RFIDCOM.Settings = "9600,n,8,1"

    Label3.Caption = "Enter the port numbers"

End Sub

Function get_load_cell_value() As String
    Dim i As Integer
    Dim StartTime
'    Dim SerialInputString As String * 7
    Dim SerialInputString As String

        If LCCOM.PortOpen = True Then LCCOM.PortOpen = False
            LCCOM.PortOpen = True
            StartTime = Now
            ReturnFlag = ""

            Do
                If LCCOM.InBufferCount >= 7 Then Exit Do: ' got enough bytes
                i = DateDiff("s", StartTime, Now)
                If i > 5 Then ReturnFlag = "Timeout": Exit Do: ' timeout in 5 seconds
                Loop

            If ReturnFlag <> "Timeout" Then
                SerialInputString = LCCOM.Input
                get_load_cell_value = SerialInputString         'return value
            Else
                SerialInputString = "No data"
            End If
                LCCOM.PortOpen = False
            Exit Function
GetSerialError: LCCOM.PortOpen = False
End Function

Function load_value_in_gms(serin As String) As Integer
    Dim x As Integer

        x = InStr(serin, "[")
        If x Then
            If x = 1 Then
                serin = Mid$(serin, x + 1, 5)       'if serin = [00020][00020]
            Else
                serin = Mid$(serin, x + 1) & Left$(serin, x - 2)    'if serin = 020][00
            End If
        End If
    load_value = Val(serin)               'return value

End Function

Sub rfid_validation()
   Dim rfvalid As String * 12
   Dim i As Integer
   Dim StartTime

        RFIDCOM.PortOpen = True
        StartTime = Now
        ReturnFlag = ""

         Do
                If RFIDCOM.CommEvent = comEvReceive Then Exit Do: ' got enough bytes
                i = DateDiff("s", StartTime, Now)
                If i > 5 Then ReturnFlag = "Timeout": Exit Do: ' timeout in 5 seconds
        Loop

            If ReturnFlag <> "Timeout" Then
                rfvalid = RFIDCOM.Input
            Else
                Label3.Caption = "Try again!"
                RFIDCOM.PortOpen = False
                Exit Sub    ' if no card is swiped then exit the validation function
            End If




        load_a = Val(Text1.Text)
        load_b = Val(Text2.Text)
        load_c = Val(Text3.Text)
        load_d = Val(Text4.Text)

            Select Case rfvalid

              Case "4A00C36058B1"
                    Label3.Caption = "Welcome Farmer A" & vbCrLf & "Enter the values"
                    Call Dispence(load_a, load_b, load_c, load_d)

              Case "4A00C38E9D9A"
                    Label3.Caption = "Welcome Farmer B" & vbCrLf & "Enter the values"
                    Call Dispence(load_a, load_b, load_c, load_d)

              Case "4A00C38E9B9C"
                    Label3.Caption = "Welcome Farmer C" & vbCrLf & "Enter the values"
                    Call Dispence(load_a, load_b, load_c, load_d)

            End Select
       RFIDCOM.PortOpen = False


End Sub

Sub Dispence(ByVal load_a As Integer, ByVal load_b As Integer, ByVal load_c As Integer, ByVal load_d As Integer)
   ' Dim temp As String * 7
    Dim gms  As Integer
    Dim counter As Integer
 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 'CONTAINER 1
 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Do
            Label3.Caption = "Dispensing fertilizer 1"
            counter = 1
            'UCcom.output = "1"
'                Do
'                    gms = load_value_in_gms(get_load_cell_value())
'                Exit Do
'                Label3.Caption = "Fertilizer 1 filling complete"
'                Loop Until (load_a = gms)
'
                Do
                    gms = load_value_in_gms(get_load_cell_value())
                  '  If (gms = load_a) Then Exit Do:
'                    If (gms < load_a) Then
                Loop Until (gms = load_a)

                Label3.Caption = "Fertilizer 1 filling complete"
                Exit Do
        Loop

 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 'CONTAINER 2
 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Do
            Label3.Caption = "Dispensing fertilizer 2"
            counter = 2
            'UCcom.output = "1"
'                Do
'                    gms = load_value_in_gms(get_load_cell_value())
'                Exit Do
'                Label3.Caption = "Fertilizer 2 filling complete"
'                Loop Until (load_b = gms)
'
                Do
                    gms = load_value_in_gms(get_load_cell_value())
                    'If (gms = load_a) Then Exit Do
                    Loop Until (gms = load_b)
                Label3.Caption = "Fertilizer 2 filling complete"

        Exit Do
        Loop
 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 'CONTAINER 3
 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Do
            Label3.Caption = "Dispensing fertilizer 3"
            counter = 3
            'UCcom.output = "1"
'                Do
'                    gms = load_value_in_gms(get_load_cell_value())
'                Exit Do
'                Label3.Caption = "Fertilizer 3 filling complete"
'                Loop Until (load_c = gms)
''
                Do
                    gms = load_value_in_gms(get_load_cell_value())
                   ' If (gms = load_a) Then Exit Do
                   ' If (gms < load_c) Then
                   ' Loop
                    Loop Until (gms = load_c)
                Label3.Caption = "Fertilizer 3 filling complete"

        Exit Do
        Loop
 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 'CONTAINER 4
 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Do
            Label3.Caption = "Dispensing fertilizer 4"
            counter = 4
            'UCcom.output = "1"
'                Do
'                    gms = load_value_in_gms(get_load_cell_value())
'                Exit Do
'                Label3.Caption = "Fertilizer 4 filling complete"
'                Loop Until (load_d = gms)
''
                Do
                    gms = load_value_in_gms(get_load_cell_value())
                   ' If (gms = load_a) Then Exit Do
                   ' If (gms < load_d) Then
                   ' Loop
                    Loop Until (gms = load_d)
                Label3.Caption = "Fertilizer 4 filling complete"

        Exit Do
       Loop
 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

       'UCcom.output = "5"


End Sub

推荐答案

(serin,x + 1 5 ' if serin = [00020]
否则
serin = Mid
(serin, x + 1, 5) 'if serin = [00020][00020] Else serin = Mid


(serin,x + 1 )&左
(serin, x + 1) & Left


(serin,x - 2 ' if serin = 020] [00
结束 如果
结束 如果
load_value = Val(serin)' 返回值

结束 功能

Sub rfid_validation()
Dim rfvalid 作为 字符串 * 12
Dim i 作为 整数
Dim StartTime

RFIDCOM.PortOpen = True
StartTime =现在
ReturnFlag = < span class =code-string>


如果 RFIDCOM.CommEvent = comEvReceive 然后 退出执行:' 得到足够的字节
i = DateDiff( s,StartTime,Now)
如果 i> 5 然后 ReturnFlag = 超时退出执行:' 5秒内超时
循环

如果 ReturnFlag<> 超时 然后
rfvalid = RFIDCOM.Input
其他
Label3.Caption = 再试一次!
RFIDCOM.PortOpen = False
退出 Sub ' 如果没有卡片刷过然后退出验证功能
结束 如果




load_a = Val(Text1.Text)
load_b = Val(Text2.Text)
load_c = Val(Text3.Text)
load_d = Val(Text4.Text)

选择 Ca se rfvalid

案例 4A00C36058B1
Label3.Caption = 欢迎农民A& vbCrLf& 输入值
致电 Dispence(load_a,load_b,load_c,load_d)

案例 4A00C38E9D9A
Label3.Caption = Welcome农民B& vbCrLf& 输入值
致电 Dispence(load_a,load_b,load_c,load_d)

案例 4A00C38E9B9C
Label3.Caption = Welcome农民C& vbCrLf& 输入值
致电 Dispence(load_a,load_b,load_c,load_d)

结束 选择
RFIDCOM.PortOpen = 错误


结束 Sub

Sub Dispence( ByVal load_a 作为 整数 ByVal load_b 作为 整数 ByVal load_c 作为 整数 ByVal load_d As 整数
' Dim temp As String * 7
Dim gms 作为 整数
Dim 计数器作为 整数
''' ''''''''''''''''' '''''
' CONTAINER 1
''' ''''''''''''''''''''''''''' '
执行
Label3.Caption = 配药肥1
counter = 1
' UCcom.output =1
'
' gms = load_value_in_gms(get_load_cell_value())
' < span class =code-comment>退出执行
' Label3.Caption =Fertilizer 1填写完整
' Loop Until(load_a = gms)
'

gms = load_value_in_gms(get_load_cell_value())
' If(gms = load_a)然后退出Do:
' 如果(gms< load_a)然后
循环直到(gms = load_a)

Label3.Caption = 肥料1填写完整
退出 执行
循环

' '' ''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''
'CONTAINER 2
'''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''''' $ b$b Do
Label3.Caption = \"Dispensing fertilizer 2\"
counter = 2
'UCcom.output = \"1\"
' Do
' gms = load_value_in_gms(get_load_cell_value())
' Exit Do
' Label3.Caption = \"Fertilizer 2 filling complete\"
' Loop Until (load_b = gms)
'
Do
gms = load_value_in_gms(get_load_cell_value())
'If (gms = load_a) Then Exit Do
Loop Until (gms = load_b)
Label3.Caption = \"Fertilizer 2 filling complete\"

Exit Do
Loop
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'CONTAINER 3
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Do
Label3.Caption = \"Dispensing fertilizer 3\"
counter = 3
'UCcom.output = \"1\"
' Do
' gms = load_value_in_gms(get_load_cell_value())
' Exit Do
' Label3.Caption = \"Fertilizer 3 filling complete\"
' Loop Until (load_c = gms)
''
Do
gms = load_value_in_gms(get_load_cell_value())
' If (gms = load_a) Then Exit Do
' If (gms < load_c) Then
' Loop
Loop Until (gms = load_c)
Label3.Caption = \"Fertilizer 3 filling complete\"

Exit Do
Loop
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'CONTAINER 4
''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''''''
Do
Label3.Caption = \"Dispensing fertilizer 4\"
counter = 4
'UCcom.output = \"1\"
' Do
' gms = load_value_in_gms(get_load_cell_value())
' Exit Do
' Label3.Caption = \"Fertilizer 4 filling complete\"
' Loop Until (load_d = gms)
''
Do
gms = load_value_in_gms(get_load_cell_value())
' If (gms = load_a) Then Exit Do
' If (gms < load_d) Then
' Loop
Loop Until (gms = load_d)
Label3.Caption = \"Fertilizer 4 filling complete\"

Exit Do
Loop
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'UCcom.output = \"5\"


End Sub
(serin, x - 2) 'if serin = 020][00 End If End If load_value = Val(serin) 'return value End Function Sub rfid_validation() Dim rfvalid As String * 12 Dim i As Integer Dim StartTime RFIDCOM.PortOpen = True StartTime = Now ReturnFlag = "" Do If RFIDCOM.CommEvent = comEvReceive Then Exit Do: ' got enough bytes i = DateDiff("s", StartTime, Now) If i > 5 Then ReturnFlag = "Timeout": Exit Do: ' timeout in 5 seconds Loop If ReturnFlag <> "Timeout" Then rfvalid = RFIDCOM.Input Else Label3.Caption = "Try again!" RFIDCOM.PortOpen = False Exit Sub ' if no card is swiped then exit the validation function End If load_a = Val(Text1.Text) load_b = Val(Text2.Text) load_c = Val(Text3.Text) load_d = Val(Text4.Text) Select Case rfvalid Case "4A00C36058B1" Label3.Caption = "Welcome Farmer A" & vbCrLf & "Enter the values" Call Dispence(load_a, load_b, load_c, load_d) Case "4A00C38E9D9A" Label3.Caption = "Welcome Farmer B" & vbCrLf & "Enter the values" Call Dispence(load_a, load_b, load_c, load_d) Case "4A00C38E9B9C" Label3.Caption = "Welcome Farmer C" & vbCrLf & "Enter the values" Call Dispence(load_a, load_b, load_c, load_d) End Select RFIDCOM.PortOpen = False End Sub Sub Dispence(ByVal load_a As Integer, ByVal load_b As Integer, ByVal load_c As Integer, ByVal load_d As Integer) ' Dim temp As String * 7 Dim gms As Integer Dim counter As Integer ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'CONTAINER 1 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Do Label3.Caption = "Dispensing fertilizer 1" counter = 1 'UCcom.output = "1" ' Do ' gms = load_value_in_gms(get_load_cell_value()) ' Exit Do ' Label3.Caption = "Fertilizer 1 filling complete" ' Loop Until (load_a = gms) ' Do gms = load_value_in_gms(get_load_cell_value()) ' If (gms = load_a) Then Exit Do: ' If (gms < load_a) Then Loop Until (gms = load_a) Label3.Caption = "Fertilizer 1 filling complete" Exit Do Loop ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'CONTAINER 2 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Do Label3.Caption = "Dispensing fertilizer 2" counter = 2 'UCcom.output = "1" ' Do ' gms = load_value_in_gms(get_load_cell_value()) ' Exit Do ' Label3.Caption = "Fertilizer 2 filling complete" ' Loop Until (load_b = gms) ' Do gms = load_value_in_gms(get_load_cell_value()) 'If (gms = load_a) Then Exit Do Loop Until (gms = load_b) Label3.Caption = "Fertilizer 2 filling complete" Exit Do Loop ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'CONTAINER 3 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Do Label3.Caption = "Dispensing fertilizer 3" counter = 3 'UCcom.output = "1" ' Do ' gms = load_value_in_gms(get_load_cell_value()) ' Exit Do ' Label3.Caption = "Fertilizer 3 filling complete" ' Loop Until (load_c = gms) '' Do gms = load_value_in_gms(get_load_cell_value()) ' If (gms = load_a) Then Exit Do ' If (gms < load_c) Then ' Loop Loop Until (gms = load_c) Label3.Caption = "Fertilizer 3 filling complete" Exit Do Loop ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'CONTAINER 4 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Do Label3.Caption = "Dispensing fertilizer 4" counter = 4 'UCcom.output = "1" ' Do ' gms = load_value_in_gms(get_load_cell_value()) ' Exit Do ' Label3.Caption = "Fertilizer 4 filling complete" ' Loop Until (load_d = gms) '' Do gms = load_value_in_gms(get_load_cell_value()) ' If (gms = load_a) Then Exit Do ' If (gms < load_d) Then ' Loop Loop Until (gms = load_d) Label3.Caption = "Fertilizer 4 filling complete" Exit Do Loop ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'UCcom.output = "5" End Sub


这篇关于Visual Basic中的串口通信问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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