PIC16f877a Serial Communication with VB6

Thread Starter

lloydi12345

Joined Aug 15, 2010
103
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:

Rich (BB code):
void main()
{
UART1_init(9600); // initialize USART module
// (8 bit, 9600 baud rate, no parity bit...
      while (1){
      UART1_Write_text("Test"); // send data via USART
      Uart1_write(10);
      Uart1_write(13);
      Delay_ms(4000);
      }
}
VB6 code is:

Rich (BB code):
Private Sub Form_Load()
    MSComm1.PortOpen = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
    MSComm1.PortOpen = False
End Sub

Private Sub Text1_Change()

End Sub

Private Sub Timer1_Timer()
    Text1.Text = Text1.Text + MSComm1.Input
End Sub
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.
 

Attachments

CDRIVE

Joined Jul 1, 2008
2,219
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.
Rich (BB code):
    Text1.Text = Text1.Text + MSComm1.Input
Should be changed to..
Rich (BB code):
    Text1.Text = Text1.Text & MSComm1.Input
Consider what happens when executing the following code.
Rich (BB code):
Option Explicit
Dim intNum1 As Integer
Dim intNum2 As Integer
Dim intNum3 As Integer
 
Private Sub Form_Load()
   intNum1 = 5
   intNum2 = 4
   intNum3 = intNum1 + intNum2
      MsgBox intNum3                    ' returns 9
   intNum3 = intNum1 & intNum2
      MsgBox intNum3                    ' returns 54
End Sub
 
Last edited:

Thread Starter

lloydi12345

Joined Aug 15, 2010
103
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:

CDRIVE

Joined Jul 1, 2008
2,219
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.
 

Thread Starter

lloydi12345

Joined Aug 15, 2010
103
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.
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

Rich (BB code):
      Uart1_write(10);
      Uart1_write(13);
which means an enter.
 

nerdegutta

Joined Dec 15, 2009
2,684
Rich (BB code):
Uart1_write(10);
Uart1_write(13);
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.:)
 

Thread Starter

lloydi12345

Joined Aug 15, 2010
103
Rich (BB code):
Uart1_write(10);
Uart1_write(13);
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.:)
: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?
 

CDRIVE

Joined Jul 1, 2008
2,219
: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?
Like this but your TextBox Multiline property must be set to true at design time. I checked yours and it is.
Rich (BB code):
Text1.Text = Text1.Text & MSComm1.Input & VbCrLf
 

Thread Starter

lloydi12345

Joined Aug 15, 2010
103
Like this but your TextBox Multiline property must be set to true at design time. I checked yours and it is.
Rich (BB code):
Text1.Text = Text1.Text & MSComm1.Input & VbCrLf
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:

CDRIVE

Joined Jul 1, 2008
2,219
to a new line CDRIVE.
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.
 

Thread Starter

lloydi12345

Joined Aug 15, 2010
103
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.
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?
 

CDRIVE

Joined Jul 1, 2008
2,219
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?
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. ;)
 

Thread Starter

lloydi12345

Joined Aug 15, 2010
103
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. ;)
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.
 

CDRIVE

Joined Jul 1, 2008
2,219
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.
 

Thread Starter

lloydi12345

Joined Aug 15, 2010
103
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.
Thank you CDRIVE for the help, you're such a good guy ;)
 
Top