Results 1 to 19 of 19

Thread: Mscomm1.output problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Mscomm1.output problem

    Hi All

    I need help with MScomm1.output and times

    i want to send multi Command Lines Via RS232 thing is only the first or Second line works and i want to add 2 second delay to them.

    Example of what i want to send:-

    Mscomm1.output = chr(2) & "C_1" & chr(3) & chr(2) & "P_1" & chr(3) & chr(2) & "X_1" & chr(3) & chr(4)

    2 Sec Delay

    Mscomm1.output = chr(2) & "S_1" & chr(3) & chr(4)

    2 Sec Delay

    Mscomm1.output = chr(2) & "C_1" & chr(3) & chr(2) & "P_2" & chr(3) & chr(2) & "X_1" & chr(3) & chr(4)

    2 Sec Delay

    Mscomm1.output = chr(2) & "S_1" & chr(3) & chr(4)

    ETC ETC

    Kind Regards

    Steve

  2. #2
    Member sgarv's Avatar
    Join Date
    Jul 2012
    Posts
    34

    Re: Mscomm1.output problem

    There are ways to create a delay, I can think of two:

    The old brute force method:

    Dim dateFut as date


    Mscomm1.output = chr(2) & "C_1" & chr(3) & chr(2) & "P_1" & chr(3) & chr(2) & "X_1" & chr(3) & chr(4)

    dateFut = dateadd("s",2,now)
    Do
    DoEvents
    Loop Until Now > dteFut


    Mscomm1.output = chr(2) & "S_1" & chr(3) & chr(4)

    dateFut = dateadd("s",2,now)
    Do
    DoEvents
    Loop Until Now > dteFut

    etc.

    A second more "elegant" way of doing this is using a Timer. To do this put a timer on your form and add this statement to the form's declarations:

    private intCount as integer

    Set the timer's Enabled property to False and Interval property to 2000.

    In the timer event add this code:

    Code:
    Private Sub Timer1_Timer()
    
      select case intCount
        case 0
           Mscomm1.output = chr(2) & "C_1" & chr(3) & chr(2) & "P_1" & _
              chr(3) & chr(2) & "X_1" & chr(3) & chr(4)
        case 1
            Mscomm1.output = chr(2) & "S_1" & chr(3) & chr(4)
        case 2
            Mscomm1.output = chr(2) & "C_1" & chr(3) & chr(2) & "P_2" & _
               chr(3) & chr(2) & "X_1" & chr(3) & chr(4)
        case 3
            Mscomm1.output = chr(2) & "S_1" & chr(3) & chr(4)
    
            'This statement must be in the last case, move from here
            'if you have more strings to send out the COMM port.
            Timer1.Enabled = False
      end select
    
      intCount = intCount + 1
    End Sub
    
    
    'To send the strings call the following proc.
    Private Sub SendCommand()
    
      'Initialize the counter.
      intCount = 0  'Points to first command
    
      'Start the timer.
      Timer1.Enabled = True
    
    End Sub
    The above are just ideas and have not been tested. Regards, Sgarv

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

    Re: Mscomm1.output problem

    I would use a timer rather than a busy loop.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Mscomm1.output problem

    Hi

    I tried the following but disabled mscomm for moment and put print 1 , print 2, print 3 after each case but after 2 sec it keeps doing print 1 not 2 3 4

    Regards

    Steve


    Private Sub Sendcommand_Click()


    'Initialize the counter.
    intCount = 0 'Points to first command

    'Start the timer.
    Timer1.Enabled = True

    End Sub

    Private Sub Timer1_Timer()

    Select Case intCount
    Case 0
    ' Mscomm1.output = Chr(2) & "C_1" & Chr(3) & Chr(2) & "P_1" & _
    Chr(3) & Chr(2) & "X_1" & Chr(3) & Chr(4)
    Print "1"
    Case 1
    ' Mscomm1.output = Chr(2) & "S_1" & Chr(3) & Chr(4)
    Print "2"
    Case 2
    ' Mscomm1.output = Chr(2) & "C_1" & Chr(3) & Chr(2) & "P_2" & _
    Chr(3) & Chr(2) & "X_1" & Chr(3) & Chr(4)
    Print "3"
    Case 3
    ' Mscomm1.output = Chr(2) & "S_1" & Chr(3) & Chr(4)
    Print "4"

    'This statement must be in the last case, move from here
    'if you have more strings to send out the COMM port.
    Timer1.Enabled = False
    End Select

    intCount = intCount + 1
    End Sub

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

    Re: Mscomm1.output problem

    You need top dim your var intCount in the form declarations section so it will retain its value for future timer cycles.

    You should also turn on Option Explicit in all programs you do and always dim all variables

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Mscomm1.output problem

    Hi do you mean put this in the Private Sub Timer1_Timer() section


    I have tried these:-

    Dim intCount As Variant

    Dim intCount As Long

    Dim intCount As Integer

    but keeps printing 1 on screen after 2sec

    Regards

    Steve
    Last edited by sbarber007; Jul 7th, 2012 at 11:20 AM.

  7. #7
    Member sgarv's Avatar
    Join Date
    Jul 2012
    Posts
    34

    Re: Mscomm1.output problem

    No, like this:


    Code:
    'Way at top of code module of form.
    Option Explicit
    
    'Form's (or module's) declaration section
    Private intCount As Integer
    
    'Code follows
    Private Sub Sendcommand_Click()
    
      'etc
    
    End Sub
    Humble opinion: Never leave out the option explicit statement at the top of the code module. Debugging a simple problem like this would have been easy. Do you know what option explicit is for? If not:

    http://msdn.microsoft.com/en-us/library/y9341s4f%28v=vs.80%29.aspx

    If option explicit is not automatically included when you add a new form or module do this:

    In the VB6 IDE click Tools menu option
    In Tools menu click Options...
    Check "Require variable declaration" check box - note that by default it is unchecked.

    Hope that helps, SGarv

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

    Re: Mscomm1.output problem

    Yes, always use Option Explicit

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Mscomm1.output problem

    Thanks very much for your help that works great but i just want to go 1 step further, by making case 0 have a 3 sec delay before it gets to case 1 then case 1 to case x run for 2 sec like it is now then for it to repeat the whole process again in a loop. until i click the stop button.

    thnaks for your help.

    Kind Regards

    Steve

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

    Re: Mscomm1.output problem

    In that case you should use an interval of 1000 so the timer fires every second and case the numbers on the case statements to the number of seconds needed.

    So your first case would be 2 and the next one would be 4 and so on.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Mscomm1.output problem

    thanks for that how do i repeat it until i click stop with commad button

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

    Re: Mscomm1.output problem

    Think about it a little, it is very easy. Hint:It has to do with the value of your intCount variable

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Mscomm1.output problem

    Sorry can not think what it is to repeat the process

    'Way at top of code module of form.
    Option Explicit

    'Form's (or module's) declaration section
    Private intCount As Integer




    Private Sub Command1_Click()
    End
    End Sub

    Private Sub Sendcommand_Click()

    Dim intCount As Integer



    'Initialize the counter.
    intCount = 0 'Points to first command i tried changing this

    'Start the timer.
    Timer1.Enabled = True

    End Sub

    Private Sub Timer1_Timer()





    Select Case intCount
    Case 0
    ' Mscomm1.output = Chr(2) & "C_1" & Chr(3) & Chr(2) & "P_1" & _
    Chr(3) & Chr(2) & "X_1" & Chr(3) & Chr(4)
    Print "1"
    Case 4
    ' Mscomm1.output = Chr(2) & "S_1" & Chr(3) & Chr(4)
    Print "2"
    Case 6
    ' Mscomm1.output = Chr(2) & "C_1" & Chr(3) & Chr(2) & "P_2" & _
    Chr(3) & Chr(2) & "X_1" & Chr(3) & Chr(4)
    Print "3"
    Case 8
    ' Mscomm1.output = Chr(2) & "S_1" & Chr(3) & Chr(4)
    Print "4"
    Case 12
    ' Mscomm1.output = Chr(2) & "S_1" & Chr(3) & Chr(4)
    Print "5"

    Form1.Cls


    'This statement must be in the last case, move from here
    'if you have more strings to send out the COMM port.
    Timer1.Enabled = False
    End Select

    intCount = intCount + 1 and i tried changing this

    End Sub


    regards

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

    Re: Mscomm1.output problem

    Simply add another case at whatever number of seconds and change intCount=0 or add intcount=0 in your case 12 depending on when you want the count to start over.

    It is very simple if you just think about what it is actually doing.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Mscomm1.output problem

    Hi
    I added CASE 14 with intCount = 0 but wont restart


    'Way at top of code module of form.
    Option Explicit

    'Form's (or module's) declaration section
    Private intCount As Integer




    Private Sub Command1_Click()
    End
    End Sub

    Private Sub Sendcommand_Click()

    Dim intCount As Integer

    ' MessageBox.Show ("Counter is currently: " + CStr(intCount))



    'Initialize the counter.
    intCount = 0 'Points to first command

    'Start the timer.
    Timer1.Enabled = True

    End Sub

    Private Sub Timer1_Timer()



    Form1.Cls

    Select Case intCount
    Case 0
    ' Mscomm1.output = Chr(2) & "C_1" & Chr(3) & Chr(2) & "P_1" & _
    Chr(3) & Chr(2) & "X_1" & Chr(3) & Chr(4)
    Print "1"
    Case 4
    ' Mscomm1.output = Chr(2) & "S_1" & Chr(3) & Chr(4)
    Print "2"
    Case 6
    ' Mscomm1.output = Chr(2) & "C_1" & Chr(3) & Chr(2) & "P_2" & _
    Chr(3) & Chr(2) & "X_1" & Chr(3) & Chr(4)
    Print "3"
    Case 8
    ' Mscomm1.output = Chr(2) & "S_1" & Chr(3) & Chr(4)
    Print "4"
    Case 12
    ' Mscomm1.output = Chr(2) & "S_1" & Chr(3) & Chr(4)
    Print "5"
    Case 14
    ' Mscomm1.output = Chr(2) & "S_1" & Chr(3) & Chr(4)

    intCount = 0 I added this line but wont restart


    'This statement must be in the last case, move from here
    'if you have more strings to send out the COMM port.
    Timer1.Enabled = False
    End Select

    intCount = intCount + 1

    End Sub

    regards

    steve
    Last edited by sbarber007; Jul 8th, 2012 at 11:49 AM.

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

    Re: Mscomm1.output problem

    You are stopping the timer

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

    Re: Mscomm1.output problem

    Another method would be to create a queue and send each element of the queue at 2 second intervals
    Code:
    Option Explicit
    
    Private colQueue As Collection
    
    Private Sub Command1_Click()
    Set colQueue = New Collection
    colQueue.Add Chr(2) & "C_1" & Chr(3) & Chr(2) & "P_1" & Chr(3) & Chr(2) & "X_1" & Chr(3) & Chr(4)
    colQueue.Add Chr(2) & "S_1" & Chr(3) & Chr(4)
    colQueue.Add Chr(2) & "C_1" & Chr(3) & Chr(2) & "P_2" & Chr(3) & Chr(2) & "X_1" & Chr(3) & Chr(4)
    colQueue.Add Chr(2) & "S_1" & Chr(3) & Chr(4)
    MSComm1.Output = colQueue.Item(1)
    colQueue.Remove (1)
    Timer1.Enabled = True
    End Sub
    
    Private Sub Form_Load()
    Timer1.Enabled = False
    Timer1.Interval = 2000
    '
    ' Put the MSComm1 initialisation code here
    '
    End Sub
    
    Private Sub Timer1_Timer()
    If colQueue.Count > 0 Then
        MSComm1.Output = colQueue.Item(1)
        colQueue.Remove (1)
    Else
        Timer1.Enabled = False
        MsgBox "All data sent"
    End If
    End Sub

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Mscomm1.output problem

    @DataMiser

    Thanks for that i swear i turned of the timer but it works now but it dont go to case 0 it starts at case 4???

    regards

    Steve

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

    Re: Mscomm1.output problem

    That's because you are adding 1 after you set it to 0 so it will not be equal to 0 when it comes back around. In your case 14 set intCount=-1 instead

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