Results 1 to 14 of 14

Thread: VB6 program help- Nina

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    7

    VB6 program help- Nina

    Hi all,

    I am a beginner for VB6. I need to write a program for a system to send a command like 100 times. I think i can use a counter and while loop. I need to find that during this multiple iterations, would there be any disconnect in communication? The system i am working on will send an ack after it receives the command. Please help.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: VB6 program help- Nina

    I would suggest that you use a For Next loop rather than a while loop with a counter. As for the com part you should start by telling us what type of com you are talking about ... Serial,UDP,TCP ?

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    7

    Re: VB6 program help- Nina

    Thanks for responding. I started with the below,

    Private sub IRdisc_Click()
    'To check for IR disconnects'



    Private sub cmdCoun_Click()
    Dim Countmsg As integer
    For first counter= 1 to 100
    Print'command running'
    For second counter=1 to 5
    print'No disconnect'
    Next second counter
    Next first counter
    Print'Successful'
    End sub

    I am sure there will be lot of errors. Please advise how i can correct it. Thanks!

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    7

    Re: VB6 program help- Nina

    Also, i will be connecting the system through a dongle to the computer and it uses a USB port.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: VB6 program help- Nina

    You need to dim all your variables
    Variables can not have spaces in them e.g. First counter Second counter

    not sure what you are supposed to be checking for the code there does not check for anything it just loops and prints to the form Command Running followed by 5 lines of No Disconnect and repeats those 6 lines 100 times

  6. #6
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Re: VB6 program help- Nina

    here is piece of code stripped out of my source for you to se how it works

    for now add all these codes to fresh project and test so u can check it and tell me if this is something u want


    add to fresh project

    Code:
    text5.text = "100"  
    text1.text = "1"  '  speed 
    Label2    ' with caption set to 0
    
    
    Private Sub Command8_Click()
    Dim x As Integer
    Dim i As Integer
    For x = 0 To Text5.Text - 1
    ' your code here
    'example  winsock1.close
    'winsock1.connect ip,port
    Pause Text1
    Label2.Caption = Label2.Caption + 1
    DoEvents
        Next x
        Debug.Print x
    End Sub
    Code:
    Sub Pause(interval)
    Dim atime
        atime = Timer
     Do While Timer - atime < Val(interval)
        DoEvents
     Loop
    End Sub

    so your code is simile to mine

    Code:
    Private sub cmdCoun_Click()
    Dim Countmsg As integer
    For first counter= 1 to 100 '  is my text5.text
    Print'command running'
    For second counter=1 to 5 '  is my text5.text
    print'No disconnect'
    Next second counter
    Next first counter
    Print'Successful'
    End sub

    so here is your fixed code

    Code:
    Private Sub Command12_Click()
    Dim Countmsg As Integer
    Dim x As Integer
    Dim i As Integer
    'For first counter = 1 to 100 '  is my text5.text
    For x = 1 To 100
    'put your command here
    'example   command1_click or  any execution code etc it will keep sending till it reaches 100
    Label2.Caption = Label2.Caption + 1
    Pause 1
    DoEvents
        Next x
        Debug.Print x
    End Sub
    add this to your source

    Code:
    Sub Pause(interval)
    Dim atime
        atime = Timer
     Do While Timer - atime < Val(interval)
        DoEvents
     Loop
    End Sub

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: VB6 program help- Nina

    You should not use that busy loop pause method. That is very bad programing practice and can lead to all sorts of unexpected issues.
    There is rarely ever a case where you would need to resort to this and even when there is it is usually because your code was not written properly.

    I strongly advise against using it and against advising others to use it.

    Not to mention that the sub does not define a variable type and then uses Val() to convert it to a number which also is not properly defined in the code.

  8. #8
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Re: VB6 program help- Nina

    thanks for the advise, made many progs users never complained so its good anyways
    since no one posted i thought might aswell help her understand how its done so she gets the idea

  9. #9
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: VB6 program help- Nina

    Is this an InfraRed (IR) project ? If so, and if you're connecting via USB I suspect the IR Device looks like a Comm Port in which case you'll need to use the MSComm control.

    Do you have any documentation regarding the device ? (e.g. What Communication protocols it supports)

  10. #10

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    7

    Re: VB6 program help- Nina

    hhhhhh
    Last edited by neelima.namburi; May 23rd, 2013 at 08:40 AM.

  11. #11
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: VB6 program help- Nina

    You have attached the Form rather than the Code. Please .Zip up the Project and attach it here.

  12. #12

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    7

    Re: VB6 program help- Nina

    Hi Please find the attached project.

    Basically, i need to write a code for Number of cycles, delay, timer. The user feeds in the number of cycles and delay he wants. And send a pump command that should run the number of cycles he wanted it to run.

    Below is the command that will be executed.

    Private Sub cmdSendCommand_Click()

    Dim aCommand As String
    Dim itsArgs As String



    aCommand = txtPumpCommand & "/" & txtPumpCommandValue ' get stuff off of the form
    Call SendCommand(aCommand)

    End Sub

    Please help.
    Attached Files Attached Files

  13. #13

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    7

    Re: VB6 program help- Nina

    Hi guys, Do you think its understandable?

  14. #14
    Lively Member
    Join Date
    May 2009
    Location
    UK
    Posts
    72

    Re: VB6 program help- Nina

    Hi

    I just took a quick look, I would change the following :

    text box Number of Cycles, 'Text5' change to 'TXTCycles'

    search all references of Text5 and replace to TXTCycles.

    Assumming the send button starts the processing work for the form.

    Change the code inside to :

    Code:
    Private Sub cmdSendCommand_Click()
    
        Dim aCommand As String
        Dim itsArgs As String
        Dim Cycles As Integer
    
        aCommand = txtPumpCommand & "/" & txtPumpCommandValue   ' get stuff off of the form
        
        For Cycles = 1 To Val(TXTCycles.Text)
            Call SendCommand(aCommand)
    
            ' sleep 1000*val(txtdelaytime.text) ' delay after each sendcommand
        Next Cycles
        
        ' sleep 1000*val(txtdelaytime.text) ' delay after a full cycle of sendcommands
    End Sub
    for the delay, i would use sleep command, add this line at top of a form or module :

    Code:
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    ' usage : sleep 1000 - sleep for 1000 ms or 1 second
    What we also need is a way to display connection status during loop maybe this will work :
    If we could test if the global ConnectStatus = True

    then we could use a CheckConnection() function

    Code:
    Private Function CheckConnection() as Boolean
     
      CheckConnection = ' code to check status - True connection on (ok) , set false is off
    
      ' update global connection status variable
      ConnectStatus = CheckConnection
    
    End Sub
    We can optionally use the function when the missing data is filled in :

    Code:
    dim ConnectStatus as boolean
    
    Private Sub cmdSendCommand_Click()
    
        Dim aCommand As String
        Dim itsArgs As String
        Dim Cycles As Integer
    
        aCommand = txtPumpCommand & "/" & txtPumpCommandValue   ' get stuff off of the form
        
        For Cycles = 1 To Val(TXTCycles.Text)
    
            ' Check we have a connection before we do anything
    
            if CheckConnection = True then
               Call SendCommand(aCommand)
               ' user requested delay
               ' sleep 1000*val(txtdelaytime.text) ' delay in secs after each sendcommand
    
            else
               ' update error log
               ' try reconnect
            endif
    
        Next Cycles
        
        ' sleep 1000*val(txtdelaytime.text) ' delay in secs after a full cycle of sendcommands
    End Sub
    Last edited by asymetrix; May 23rd, 2013 at 11:06 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width