PIC16f877a Serial Communication with VB6

Discussion in 'Embedded Systems and Microcontrollers' started by lloydi12345, Dec 19, 2010.

  1. lloydi12345

    Thread Starter Member

    Aug 15, 2010
    103
    0
    Hi, I am doing a project where I will control devices from VB6. But before I go to the top part, I would like to start from the bottom part first. I am trying to display the text sent by the PIC16f877a to the program but it won't display it. I'm using com2.

    Here are the programs I used:
    MikroC PRO for PIC
    Proteus
    Virtual Serial Ports Emulator
    HyperTerminal 7 Private Edition.
    Visual Basic 6.0

    My mikroC code is:

    Code ( (Unknown Language)):
    1. void main()
    2. {
    3. UART1_init(9600); // initialize USART module
    4. // (8 bit, 9600 baud rate, no parity bit...
    5.       while (1){
    6.       UART1_Write_text("Test"); // send data via USART
    7.       Uart1_write(10);
    8.       Uart1_write(13);
    9.       Delay_ms(4000);
    10.       }
    11. }
    VB6 code is:

    Code ( (Unknown Language)):
    1.  
    2. Private Sub Form_Load()
    3.     MSComm1.PortOpen = True
    4. End Sub
    5.  
    6. Private Sub Form_Unload(Cancel As Integer)
    7.     MSComm1.PortOpen = False
    8. End Sub
    9.  
    10. Private Sub Text1_Change()
    11.  
    12. End Sub
    13.  
    14. Private Sub Timer1_Timer()
    15.     Text1.Text = Text1.Text + MSComm1.Input
    16. End Sub
    17.  
    18.  
    I tried everything on Hyperterminal and everything works fine. But I have to run Virtual Serial Ports Emulator and create COM2 device for it to be visible on Hyperterminal. Without Hyperterminal, I'm using Virtual Terminal from Proteus and it displays the text "Test" which is sent by the PIC.
    I've attached files below.

    I hope you can help me. Thank you in advance.
     
  2. nerdegutta

    Moderator

    Dec 15, 2009
    2,513
    784
    Yes, I think you need something like this between the PC and the PIC:

    [​IMG]
     
    lloydi12345 likes this.
  3. CDRIVE

    Senior Member

    Jul 1, 2008
    2,223
    99
    This has nothing to do with your issue but you should break your self of the bad practice of using the '+' math operator to concatenate in VB6.
    Code ( (Unknown Language)):
    1.  
    2.     Text1.Text = Text1.Text + MSComm1.Input
    3.  
    Should be changed to..
    Code ( (Unknown Language)):
    1.  
    2.     Text1.Text = Text1.Text & MSComm1.Input
    3.  
    Consider what happens when executing the following code.
    Code ( (Unknown Language)):
    1.  
    2. Option Explicit
    3. Dim intNum1 As Integer
    4. Dim intNum2 As Integer
    5. Dim intNum3 As Integer
    6.  
    7. Private Sub Form_Load()
    8.    intNum1 = 5
    9.    intNum2 = 4
    10.    intNum3 = intNum1 + intNum2
    11.       MsgBox intNum3                    ' returns 9
    12.    intNum3 = intNum1 & intNum2
    13.       MsgBox intNum3                    ' returns 54
    14. End Sub
    15.  
    16.  
     
    Last edited: Dec 19, 2010
  4. lloydi12345

    Thread Starter Member

    Aug 15, 2010
    103
    0
    Oh wait! I'm using a simulator and I'm not yet doing it on actual PIC. Do I still have to add max232 on the simulation? I've seen examples of COMPIM(RS232) connected directly to PIC microcontrollers. Thank you for your replies. I will try your advice CDRIVE.



    Edit: CDRIVE it still won't show the text sent by the PIC on my visual basic program. I don't know why it doesn't show anything on the textbox.
     
    Last edited: Dec 19, 2010
  5. CDRIVE

    Senior Member

    Jul 1, 2008
    2,223
    99
    Firstly, I told you that using '+' had nothing to do with your problem but if you keep using it I can assure you that some day it will eventually give you a coding problem that will be difficult to track down.
    Did I understand you to say that you can successfully transmit from Proteus (COMPIM) to Hyperterminal through a NullModem? I've read and re-read your first post to no avail. I simply can't understand if COMPIM is set to COM2 or MSComm is set to COM2. I opened your VBP and MSComm is set to COM1. I have no experience with your particular PIC or its language but I have worked with COMPIM and, of course, many years of VB.
     
    lloydi12345 likes this.
  6. lloydi12345

    Thread Starter Member

    Aug 15, 2010
    103
    0
    Thanks CDRIVE! and everyone for the reminder. I didn't saw that there's "Commport" property below "(Name)" property and it was set to "1" so I changed it to "2". I thought I only have to adjust the property "Settings" of the MSComm button. Thanks again. Do you have any idea how I can make the text do "Enter"? Because on my PIC code I used

    Code ( (Unknown Language)):
    1.       Uart1_write(10);
    2.       Uart1_write(13);
    3.  
    which means an enter.
     
  7. nerdegutta

    Moderator

    Dec 15, 2009
    2,513
    784
    Code ( (Unknown Language)):
    1.  
    2. Uart1_write(10);
    3. Uart1_write(13);
    4.  
    Actually, I think it means "Cartrigde return" and "Line feed", from time we used matrix printers. :rolleyes:

    Don't remeber which is which, though...

    Correct me if I'm wrong.:)
     
  8. lloydi12345

    Thread Starter Member

    Aug 15, 2010
    103
    0
    :D I think Line feed was the 10 and 13 was cartridge return. How can I do then an enter on textbox? Have idea how to do it?
     
  9. CDRIVE

    Senior Member

    Jul 1, 2008
    2,223
    99
    Like this but your TextBox Multiline property must be set to true at design time. I checked yours and it is.
    Code ( (Unknown Language)):
    1.  
    2. Text1.Text = Text1.Text & MSComm1.Input & VbCrLf
    3.  
     
  10. lloydi12345

    Thread Starter Member

    Aug 15, 2010
    103
    0
    Uhm okay, I sorta get it. Is it hard to send data from VB to serial PORT?:( Can you help me start about it. I can't understand a thing from other websites. I hope you can help me with simpler codes.
     
    Last edited: Dec 21, 2010
  11. CDRIVE

    Senior Member

    Jul 1, 2008
    2,223
    99
    What does that mean? Is it placing your TextBox input on a new line or not?
     
  12. lloydi12345

    Thread Starter Member

    Aug 15, 2010
    103
    0
    I'm thinking about placing for example a text "Test" on a textbox and when I click the command button, it will send it on rs232 serial port. Can you help me out here?
     
  13. CDRIVE

    Senior Member

    Jul 1, 2008
    2,223
    99
    You didn't answer my question.
     
  14. lloydi12345

    Thread Starter Member

    Aug 15, 2010
    103
    0
    to a new line CDRIVE.
     
  15. CDRIVE

    Senior Member

    Jul 1, 2008
    2,223
    99
    OK, so is this topic resolved or do you have other questions? If so, what are they?

    FYI, the best and easiest way to gain proficiency with MSComm is by using a simple Loop-Back Tester or by using two Serial Ports and a Null Modem. This is how I schooled myself.
     
  16. lloydi12345

    Thread Starter Member

    Aug 15, 2010
    103
    0
    After learning how to receive data from Proteus, since the topic is all about Serial Communication in VB6 I would like to learn too on sending data to the PIC. I can try also your suggestion. In Loop-Back Tester, do you mean that I will send data from VB6 to a virtual serial port then receive it back to VB6?
     
  17. CDRIVE

    Senior Member

    Jul 1, 2008
    2,223
    99
    Not exactly. A LoopBack Tester simply loops All TX signal and control pins back to their RX counterparts. You can make one with a spare D connector. Here's a link to an 'in-line' RS232 tester that is very useful, when used with or without a loopback tester. They also make these testers with the loopback wired in but it's more useful to have your LoopBack plug separate. You can also make an LED tester like this your self but I've never found it worth the effort over the cost of the commercial units.

    http://www.serialgear.com/Serial-Cables-DB9-TERS.html

    Anyway, the idea is that a LoopBack enables you to send from VB and loop it back to the same port and VB program.

    You must already be using a NullModem between Proteus and VB but you can also use it between two MSComm Controls. They both are learning tools which will build your confidence with MSComm. ;)
     
    lloydi12345 likes this.
  18. lloydi12345

    Thread Starter Member

    Aug 15, 2010
    103
    0
    Oh okay I now get the idea how to do it though I don't know yet the codes for sending data from VB, all I know is just receiving from PIC. Can you suggest a link or something else? I've researched alot from web but they're so complicated for a beginner like me.
     
  19. CDRIVE

    Senior Member

    Jul 1, 2008
    2,223
    99
    You can learn MSComm on line at..
    http://www.vbforums.com/forumdisplay.php?f=1

    Do a search (in the VB6 section) for MSComm or Serial Port or RS232. You will be reading for a long time. This is the premier VB6 forum on our planet with some of the best MSMVP's you'll ever find. Despite this, MSComm is known well by very few on VBForums. There are only three MSComm Gurus.. 'Doogle', 'Dick Grier' and myself but they're better versed than me.

    Also consult your VB6 MSDN library and your 'Object Browser' for MSComm.

    Nothing will gain you more proficiency than simply playing with it and that's what a LoopBack is for.
     
    lloydi12345 likes this.
  20. lloydi12345

    Thread Starter Member

    Aug 15, 2010
    103
    0
    Thank you CDRIVE for the help, you're such a good guy ;)
     
Loading...