Page 1 of 1

PC to PIC communication: Part 1

Posted: Fri Dec 31, 2010 9:27 am
by Spanish_dude
Hi everyone,

This is the first part of the article : PC to PIC communication

In this article I'll talk about:
- Setting up VBA in Excel
- Adding the COM port icon to VBA
- Basic VBA program using the COM port

Requires:
- COM port or USB to Serial
- RS232 to TTL converter (I use a MAX232)

Setting up VBA in Excel:

Open up Excel and go to the Excel Options.

Image

Once the option window is open you may want to check if your in the "Popular" page. If you aren't then click on it.
Next you'll need to enable the "Show Developer tab in the Ribbon" checkbox.

Image

When that's done, you'll need to enable Excel to run macros.
To do this, go to the "Trust Center" page in the Excel Options and click on the "Trust Center Settings...".

Image

After that, go to the "Macro Settings" page and enable the "Enable all macros" radio button.

Image

Note: I know it is not recommended. If you often download Excel files on the internet I recommend you setting it back to "Disable all macros with notification" after you're done with your program.

If the checkbox underneath isn't enabled, you might want to enable it.

You can now go to the "Developer" tab in the "Ribbon" and click on the Visual Basic icon.
Then go to the "Tools" tab and click on "Options...".
Next you'll have to enable the "Require Variable Declaration".

Image

You're done here.

Note: When saving your Excel file containing a program, you need to sve it as a "Excel macro-enabled Workbook" (*.xlsm).


Adding the COM port icon to VBA:

First you'll need to download the attached zip file :
The attachment MSComm.zip is no longer available

32-bit OS:
Extract the "mscomm32.ocx" file to your system32 folder (C:\windows\system32\).
Next open up a MSDOS command.
Type in "regsvr32.exe c:\windows\system32\mscomm32.ocx" and press enter.

64-bit OS:
Extract the "mscomm32.ocx" file to your sysWOW64 folder (C:\windows\sysWOW64\).
Next open up a MSDOS command.
Type in "regsvr32.exe c:\windows\sysWOW64\mscomm32.ocx" and press enter.

When it's done, double click on the "mscomm32.reg" file.

Note: I don't think this is dangerous for your PC. I did it without a problem. All the PCs at school has this feature.
But just in case... I won't be responsable for any damage occured.

When all that's done, open up Excel.
Go to the Developer tab in the Ribbon and open up your click on the VBA icon.
Insert a "Userform" by clicking on the "Insert" tab and then on the "Userform" option.

Image

Then go to the "Tools" tab an click on the "Additional Controls".

Image

Scroll down until you see a disabled checkbox "Microsoft Communication Control, version 6" and enable it.

Image

There you go.
In your Toolbox you'll have a new icon, a phone, to be able to communicate through the COM ports.

Image


Basic VBA program using the COM port:

Okay, now we'll start coding.
First of all open up the VBA editor.
Insert a UserForm; drag and drop 1 TextBox, 1 CommandButton and 1 MSComm.
Note: The MSComm is invisible (when running the program) so you can place it wherever you want.

Image

Press F7 to view your code.
Next add the following functions to it:

When the program is runned it will ask the user on which COM port the PIC is connected

Code: Select all

Private Sub UserForm_Initialize()
    Dim COM As Integer
   
    COM = InputBox("Set COM Port :")
    Call InitializePort(COM)
End Sub


The InitialiezePort function is a function to open the COM port and setting the right data to it.
It sets the input length to 1 byte and the settings are; 9600 bps, No parity, 8 bits receive/send, 1 stopbit.

Code: Select all

Function InitializePort(COM As Integer)
    MSComm1.CommPort = COM
    MSComm1.Settings = "9600,N,8,1"
    MSComm1.InputLen = 1
    MSComm1.PortOpen = True
End Function


Next we want to be able to send the text written in the TextBox through the COM port when we press on the CommandButton.
Add this next piece of code:

Code: Select all

Private Sub CommandButton1_Click()
    MSComm1.Output = TextBox1.Text
End Sub


VoilĂ , that's all you need to do to send data through a COM port.

Reading from the COM port requires a little function.
Add this to your code:

Code: Select all

Function ReceiveAnswer(end_char As String) As String
    Dim buffer As String
    Dim char As String
    Dim out_of_time As Boolean
    Dim time As Single
   
    buffer = ""
    time = Timer()
   
    Do
        char = MSComm1.Input
        buffer = buffer & char
        out_of_time = Timer() > time + 2
    Loop Until char = end_char Or out_of_time
   
    If out_of_time <> False Then
        ReceiveAnswer = buffer
    Else
        ReceiveAnswer = "Out Of Time"
    End If
End Function


This function returns either the received string from the COM port or an error string ("Out Of Time").

This function will read bytes from the comport until it reads the end char.
If no end char is readed within 2 seconds, then there will be an "out of time" error.
Else it will read the data and store it in a string until it reaches the end char and then it'll return that string.

If the microcontroller is programmed to return an answer when receiving some data, you could use this function like this:

Code: Select all

Private Sub CommandButton1_Click()
    Dim buffer As String

    MSComm1.Output = TextBox1.Text
    buffer = ReadAnswer(";")

    MsgBox(buffer)
End Sub


The answer sent by the PIC microcontroller needs to have a ";" at the end of it, else there will be an error.
MsgBox(buffer) will open up a window displaying the text written in the string buffer.

That's it.
Now you know how to read/write date from/to a COM port.
What's left to do is learning basic VBA programming.
There are countless of webpages explaining how to program in VBA on google ;).

Let's go to part 2 now :mrgreen: !

Best regards,

Nicolas L. F., aka Spanish_dude / pyroesp

Re: PC to PIC comunication: Part 1

Posted: Fri Feb 04, 2011 11:30 am
by Steve

Re: PC to PIC comunication: Part 1

Posted: Fri Feb 04, 2011 4:10 pm
by Spanish_dude
Thanks Steve, I forgot to add the link :roll:

Re: PC to PIC communication: Part 1

Posted: Wed Mar 23, 2011 1:45 pm
by achillis1
Hello,

I am trying the routine and I get error, can you help?
The file exists in system32.

Thank you in advance,

Best Regards,
Andreas Achilleos

Re: PC to PIC communication: Part 1

Posted: Wed Mar 23, 2011 4:07 pm
by JohnCrow
This is the same problem I have been getting.
I've not had chance to really investigate further, but it could be down to my system running 64 bit windows(Windows 7SP1)
Does the same with or without SP1installed. Think the file is only compatible with 32bit OS.

Re: PC to PIC communication: Part 1

Posted: Wed Mar 23, 2011 6:28 pm
by Spanish_dude
I never had that error :/

As JohnCrow mentioned it, it could be that the file is only 32bit OS compatible.
I "installed" that file on a Windows XP 32 bit OS.

EDIT:

You wrote "regsvr32.exe c:\windows\system32\mscomm32.osx" instead of "regsvr32.exe c:\windows\system32\mscomm32.ocx" !

Hope this solves the problem ;)

Re: PC to PIC communication: Part 1

Posted: Thu Mar 24, 2011 7:41 am
by achillis1
Hello,

I have corrected that one and the error stiil's the same.

Must be the 64bit Win7, I got the same OS!!!!


Best Regards,

Andreas Achilleos

Re: PC to PIC communication: Part 1

Posted: Thu Mar 24, 2011 10:54 am
by Steve
You might need to launch the cmd window with elevated privileges (i.e. find cmd.exe, right-click and select "run as admin...").

Re: PC to PIC communication: Part 1

Posted: Thu Mar 24, 2011 11:12 am
by achillis1
Hello,

That did not help, stil the same error!

Best Regards,
Andreas Achilleos

Re: PC to PIC communication: Part 1

Posted: Thu Mar 24, 2011 1:53 pm
by Steve
I came up with lots of possible solutions by searching for "mscomm32.ocx x64".

Re: PC to PIC communication: Part 1

Posted: Fri Mar 25, 2011 2:43 pm
by Jan Lichtenbelt
Use the uninstall option:
regsvr32.exe /u
see
http://support.microsoft.com/kb/249873

Regards

Jan Lichtenbelt

Re: PC to PIC communication: Part 1

Posted: Sat Mar 26, 2011 5:19 pm
by Jan Lichtenbelt

Re: PC to PIC communication: Part 1

Posted: Mon Mar 28, 2011 4:24 pm
by Spanish_dude
Okay so I got it to work on my brother's 64-bit Windows 7 PC.

Here's how I did it:

- Copy mscomm32.ocx into C:\Windows\SysWOW64\ (instead of system32)
- Execute the next command : "regsvr32 C:\Windows\SysWOW64\mscomm32.ocx"

That's it ;).

EDIT :
I added how you'd install mscomm32.ocx on a 64-bit OS in the article.

Re: PC to PIC communication: Part 1

Posted: Mon Mar 28, 2011 6:22 pm
by medelec35
Spanish_dude wrote:- Copy mscomm32.ocx into C:\Windows\SysWOW64\ (instead of system32)
- Execute the next command : "regsvr32 C:\Windows\SysWOW64\mscomm32.ocx"

Nicolas,
Your a star!
I No longer get the ""The subject is not trusted for the specified action"
Thanks a lot!
You deserve Valued Contributor status in my eyes :p

Now to continue :)

Re: PC to PIC communication: Part 1

Posted: Mon Mar 28, 2011 6:34 pm
by Spanish_dude
Thanks Martin ;)

Btw, don't forget to double click the reg file.

Re: PC to PIC communication: Part 1

Posted: Mon Mar 28, 2011 7:08 pm
by JohnCrow
Execute the next command : "regsvr32 C:\Windows\SysWOW64\mscomm32.ocx"

Dont forget the above command has to be done as administrator
Works on my windows7 64bit (SP1)

Re: PC to PIC communication: Part 1

Posted: Mon Mar 28, 2011 8:13 pm
by medelec35
Spanish_dude wrote:Thanks Martin ;)

Btw, don't forget to double click the reg file.


JohnCrow wrote:Execute the next command : "regsvr32 C:\Windows\SysWOW64\mscomm32.ocx"

Dont forget the above command has to be done as administrator
Works on my windows7 64bit (SP1)


Yep, did both of the above before I posted about it working.

Thanks anyway Nicolas and John.

Now struggling the the code for receiving, placing and a string variable, but i will get there (hopefully :P )

Re: PC to PIC communication: Part 1

Posted: Tue Mar 29, 2011 6:28 am
by achillis1
Worked!!

Thank you!

Re: PC to PIC communication: Part 1

Posted: Tue Mar 29, 2011 6:38 am
by achillis1
Hello,

I am trying to drop the MScomm button in the user form and get the following error: ''the subject is not trusted for the specified action''.

Got any clue?

Thank you in advance,

Best Regards,

Andreas Achilleos

Re: PC to PIC communication: Part 1

Posted: Tue Mar 29, 2011 6:44 am
by achillis1
Now the TOOLS>ADITIONAL CONTROLS is all greyed out and cannot select it!

Re: PC to PIC communication: Part 1

Posted: Tue Mar 29, 2011 4:20 pm
by Spanish_dude
Hi achillis1,

After a quick search on google I found this forum page : http://www.vbforums.com/showthread.php?t=622730
This should resolve your problem.

PS: Be careful not to change the wrong registry as this may prevent Windows to start up or to work properly.

Re: PC to PIC communication: Part 1

Posted: Fri Apr 15, 2011 6:14 am
by achillis1
Hello,

Thanks for your help but that didi not solved the problem!!!!

Thank you

Best Regards,

Andreas Achilleos