本文介绍了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_input6. close container 17. open container 58. 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_validationEnd Sub'RFID CARD VALUES'24664=4A00C36058B1 'farmer 1'36509=4A00C38E9D9A 'farmer 2'36507=4A00C38E9B9C 'farmer 3Private 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 SubFunction 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 FunctionGetSerialError: LCCOM.PortOpen = FalseEnd FunctionFunction 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 valueEnd FunctionSub 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 = FalseEnd SubSub 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中的串口通信问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-16 06:02