Vb6 serial communication
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Vb6 serial communication

  1. #1
    Join Date
    Mar 2014
    Posts
    1

    Vb6 serial communication

    I am doing a project on vb6. I need to take some data from serial port and display it using a text box and label in vb6. But the problem is that the while loop does not run. It just executes every time on command click instead of changing continuously on itself. In my project the input to the serial port keeps changing continuously so how do I modify this code so that i can continuously read the input and change the display.
    Private Sub Command_Click()
    Dim sbuffer As String
    while (1)
    MSComm1.PortOpen=True
    If MSComm1.CommEvent=comEvReceive then sbuffer=sbuffer & MSComm1.Input
    Label1.Caption=MSComm1.Input
    Text1.Text=sbuffer
    MSComm1.PortOpen=False
    Wend
    End Sub

    Private Sub Form_Load()
    MSComm1_OnComm
    End Sub

    Private Sub MSComm1_OnComm()
    With MSComm1
    If MSComm.PortOpen=True Then MSComm1.PortOpen=False
    MSComm1.RThreshold=1
    MSComm1.Settings="9600,n,7,1"
    MScomm1.InputLen=0
    End With
    End Sub

    Please help...

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,813

    Re: Vb6 serial communication

    You should not be using a loop

    MSComm control fires an event when something happens and it is there you should place your code for reading data
    Typically you would use a select case in this event to see what the event is and take the proper action Like what you did with your if statement above

    Alternately you can use a timer and check the buffer to see if any data is there and read it if so but the event is the recommended method here.
    There should be an example in your online help and there are many on the net

    btw you also should not have the code in the on comm event that is closing and reopening the port as that would throw away anything that is received
    You should not be calling the On_Comm event from code either

    The code to open the port should go in the command click
    The code to read from the port should be in the on_comm
    On_comm fires automatically when data is received provided you have the RThreshold set properly. I think the default is 1 which would be fine
    Last edited by DataMiser; March 29th, 2014 at 01:26 AM.
    Always use [code][/code] tags when posting code.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center