Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Interacting VB6 and PIC16F877A using UART serial Communication (Duplex)

Status
Not open for further replies.

lloydi12345

Member
Hi, I'm having problem in uart communication. The set up here is that I'm doing half duplex communication. When I click a command button, it will send a character and the PIC also will send the same character back to VB6. There's no problem in communicating from vb6 to pic because I am just sending single characters (which I think is not advisable) to be compared in the if/case statements inside the pic. For example, If I click a command button then it send "x" letter/character to the microcontroller through the MScomm1's abilities then the microcontroller checks if the character received is "x" then it will do a specific function for the "x".

Here's a sample piece of my code:
Code:
case 'x':
            uart1_write_text("x");     <--   sends back data to VB6
            delay_ms(100);
            break;

The problem is when the microcontroller sends the data to VB6. It is continuous.
When the PIC sends back "x" to Vb6 the textbox shows "x". When I click the command button again it goes "xx" then "xxx" and so on until many x are placed on my textbox.

I have something like this on my vb6:
Code:
Private Sub Timer1_Timer()
    Text2.Text = MSComm1.Input & Text2.Text
End Sub

I would like also to have case select statements or if statements on my VB6, but I can't compare them with these continuous data sent. I can't make it like

Code:
if textbox.text = "x" then
do this
else if textbox.text = "xx" then
do this
else if

Do you know what's the best way to interact PIC and VB6?
 
Last edited:
Hi,
Why cant you compare the characters with the continous data?

It may be easier to use the OnComm event of the MSCOMM control, trigger an event each time a character is received rather than use the "poll" method with the timer.

Instead of looking at the textbox text, which I presume you are using as a way of logging what data has come in? Why dont you do it like this:
Code:
Private Sub Timer1_Timer()
    Dim inputBuffer as String
    inputBuffer = MSComm1.Input
    textbox.text = textbox.text & inputBuffer 
    if(lcase$(inputBuffer) = "x") then dosomething
End Sub
 
Last edited:
Hi,
Why cant you compare the characters with the continous data?

It may be easier to use the OnComm event of the MSCOMM control, trigger an event each time a character is received rather than use the "poll" method with the timer.

Instead of looking at the textbox text, which I presume you are using as a way of logging what data has come in? Why dont you do it like this:
Code:
Private Sub Timer1_Timer()
    Dim inputBuffer as String
    inputBuffer = MSComm1.Input
    textbox.text = textbox.text & inputBuffer 
    if(lcase$(inputBuffer) = "x") then dosomething
End Sub

Hi Wilksey thanks for the immediate reply. I don't know exactly how to compare it. But if I ever managed to compare it with my own way it would be a mess since I might receive another characters (If I added more command buttons that will send different characters like a, b, c, etc.) which may be different so I would have to write a lot of if statements there.

Yes I'm just using a textbox text to check if I'm receiving characters. Your code is very exact and I think it will satisfy my needs. I'll try it right away.

You said an OnComm event? is this code you've provided the OnComm event or just to help me check the characters accurately?
 
Last edited:
The code posted is for the existing way of doing it, with a timer that polls.

Do you know how to set up a OnComm event? You can basically set the MSCOMM control so that everytime 1 character is recieved it goes to the OnComm event there you can process like you are with the timer, kind of like an interrupt with the PIC's, it "knows" theres something on the serial buffer without you having to "look" for it.

If that makes sense?

Wilksey
 
hi Lloyd.

This is a subr I use in VB5.
You could drop off the Errmsg control if not required.

Set up the Commport to suit.

Code:
Private Static Sub MSComm1_OnComm()
    Dim ERMsg$, Buffer
    Dim Data
        
       On Error GoTo comfail
 
   Select Case MSComm1.CommEvent
        ' Event messages.
        
        Case comEvReceive
            Dim Buffer As Variant
            Buffer = MSComm1.Input
        
            Data = (StrConv(Buffer, vbUnicode))
       
             
        Case comEvSend
        
        Case comEvCTS
            ERMsg$ = "Change in CTS Detected"
        Case comEvDSR
            ERMsg$ = "Change in DSR Detected"
        Case comEvCD
            ERMsg$ = "Change in CD Detected"
        Case comEvRing
            ERMsg$ = "The Phone is Ringing"
        Case comEvEOF
            ERMsg$ = "End of File Detected"

        ' Error messages.
        Case comBreak
            ERMsg$ = "Break Received"
        Case comCDTO
            ERMsg$ = "Carrier Detect Timeout"
        Case comCTSTO
            ERMsg$ = "CTS Timeout"
        Case comDCB
            ERMsg$ = "Error retrieving DCB"
        Case comDSRTO
            ERMsg$ = "DSR Timeout"
        Case comFrame
            ERMsg$ = "Framing Error"
        Case comOverrun
            ERMsg$ = "Overrun Error"
        Case comRxOver
            ERMsg$ = "Receive Buffer Overflow"
        Case comRxParity
            ERMsg$ = "Parity Error"
        Case comTxFull
            ERMsg$ = "Transmit Buffer Full"
        Case Else
            ERMsg$ = "Unknown error or event"
    End Select
    
    ' Display messages
    If Len(ERMsg$) Or Len(ERMsg$) Then
    MSComm1.PortOpen = False ' clr uart
    MSComm1.PortOpen = True
    Beep
    End If
Exit Sub

comfail:
    Beep
    MSComm1.PortOpen = False ' clr uart
    MSComm1.PortOpen = True
    
End Sub
 

Attachments

  • 000esp12.gif
    000esp12.gif
    11 KB · Views: 310
The code posted is for the existing way of doing it, with a timer that polls.

Do you know how to set up a OnComm event? You can basically set the MSCOMM control so that everytime 1 character is recieved it goes to the OnComm event there you can process like you are with the timer, kind of like an interrupt with the PIC's, it "knows" theres something on the serial buffer without you having to "look" for it.

If that makes sense?

Wilksey

I see, I used most of your code and placed it on the oncomm event. Thank you Wilksey.

hi Lloyd.

This is a subr I use in VB5.
You could drop off the Errmsg control if not required.

Set up the Commport to suit.

Code:
Private Static Sub MSComm1_OnComm()
    Dim ERMsg$, Buffer
    Dim Data
        
       On Error GoTo comfail
 
   Select Case MSComm1.CommEvent
        ' Event messages.
        
        Case comEvReceive
            Dim Buffer As Variant
            Buffer = MSComm1.Input
        
            Data = (StrConv(Buffer, vbUnicode))
       
             
        Case comEvSend
        
        Case comEvCTS
            ERMsg$ = "Change in CTS Detected"
        Case comEvDSR
            ERMsg$ = "Change in DSR Detected"
        Case comEvCD
            ERMsg$ = "Change in CD Detected"
        Case comEvRing
            ERMsg$ = "The Phone is Ringing"
        Case comEvEOF
            ERMsg$ = "End of File Detected"

        ' Error messages.
        Case comBreak
            ERMsg$ = "Break Received"
        Case comCDTO
            ERMsg$ = "Carrier Detect Timeout"
        Case comCTSTO
            ERMsg$ = "CTS Timeout"
        Case comDCB
            ERMsg$ = "Error retrieving DCB"
        Case comDSRTO
            ERMsg$ = "DSR Timeout"
        Case comFrame
            ERMsg$ = "Framing Error"
        Case comOverrun
            ERMsg$ = "Overrun Error"
        Case comRxOver
            ERMsg$ = "Receive Buffer Overflow"
        Case comRxParity
            ERMsg$ = "Parity Error"
        Case comTxFull
            ERMsg$ = "Transmit Buffer Full"
        Case Else
            ERMsg$ = "Unknown error or event"
    End Select
    
    ' Display messages
    If Len(ERMsg$) Or Len(ERMsg$) Then
    MSComm1.PortOpen = False ' clr uart
    MSComm1.PortOpen = True
    Beep
    End If
Exit Sub

comfail:
    Beep
    MSComm1.PortOpen = False ' clr uart
    MSComm1.PortOpen = True
    
End Sub

Thank you ericgibbs! I will add this one in the future :)
 
No Worries.

I would use Eric's code as it is more complete and has the individual event handlers if you plan on making a "proper" project from your code.

Wilksey
 
vb 2010 express is free... why not use that?

I see tons of people using VB6 still and wonder why... I still have the original VB6 CDs heh... Came in a bulky 3 cd case :) I still like it tho but prefer newer tools because heh its free and simple to use...
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top