PC to PIC communication: Part 1

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

PC to PIC communication: Part 1

Postby Spanish_dude » Fri Dec 31, 2010 9:27 am

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
Attachments
MSComm.zip
mscomm32.ocx & mscomm32.reg files
(51.34 KiB) Downloaded 567 times
Last edited by Spanish_dude on Mon Mar 28, 2011 4:26 pm, edited 3 times in total.
These users thanked the author Spanish_dude for the post (total 3):
Benj (Thu Jan 06, 2011 1:29 pm) • medelec35 (Mon Jan 24, 2011 9:23 pm) • randomrichard (Fri Mar 11, 2011 11:02 pm)
Rating: 17.65%
 

User avatar
Steve
Matrix Staff
Posts: 3071
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 87 times
Been thanked: 386 times
Contact:

Re: PC to PIC comunication: Part 1

Postby Steve » Fri Feb 04, 2011 11:30 am

These users thanked the author Steve for the post:
Spanish_dude (Fri Feb 04, 2011 4:11 pm)
Rating: 5.88%
 

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: PC to PIC comunication: Part 1

Postby Spanish_dude » Fri Feb 04, 2011 4:10 pm

Thanks Steve, I forgot to add the link :roll:

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 92 times
Been thanked: 8 times
Contact:

Re: PC to PIC communication: Part 1

Postby achillis1 » Wed Mar 23, 2011 1:45 pm

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
Attachments
Untitled.jpg
Untitled.jpg (51.76 KiB) Viewed 15526 times

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 381 times
Been thanked: 713 times
Contact:

Re: PC to PIC communication: Part 1

Postby JohnCrow » Wed Mar 23, 2011 4:07 pm

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.
1 in 10 people understand binary, the other one doesn't !

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: PC to PIC communication: Part 1

Postby Spanish_dude » Wed Mar 23, 2011 6:28 pm

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 ;)

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 92 times
Been thanked: 8 times
Contact:

Re: PC to PIC communication: Part 1

Postby achillis1 » Thu Mar 24, 2011 7:41 am

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

User avatar
Steve
Matrix Staff
Posts: 3071
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 87 times
Been thanked: 386 times
Contact:

Re: PC to PIC communication: Part 1

Postby Steve » Thu Mar 24, 2011 10:54 am

You might need to launch the cmd window with elevated privileges (i.e. find cmd.exe, right-click and select "run as admin...").

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 92 times
Been thanked: 8 times
Contact:

Re: PC to PIC communication: Part 1

Postby achillis1 » Thu Mar 24, 2011 11:12 am

Hello,

That did not help, stil the same error!

Best Regards,
Andreas Achilleos

User avatar
Steve
Matrix Staff
Posts: 3071
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 87 times
Been thanked: 386 times
Contact:

Re: PC to PIC communication: Part 1

Postby Steve » Thu Mar 24, 2011 1:53 pm

I came up with lots of possible solutions by searching for "mscomm32.ocx x64".

User avatar
Jan Lichtenbelt
Posts: 768
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 115 times
Been thanked: 257 times
Contact:

Re: PC to PIC communication: Part 1

Postby Jan Lichtenbelt » Fri Mar 25, 2011 2:43 pm

Use the uninstall option:
regsvr32.exe /u
see
http://support.microsoft.com/kb/249873

Regards

Jan Lichtenbelt


Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: PC to PIC communication: Part 1

Postby Spanish_dude » Mon Mar 28, 2011 4:24 pm

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.
These users thanked the author Spanish_dude for the post (total 4):
Benj (Mon Mar 28, 2011 5:01 pm) • medelec35 (Mon Mar 28, 2011 6:11 pm) • JohnCrow (Mon Mar 28, 2011 7:09 pm) • achillis1 (Tue Mar 29, 2011 6:29 am)
Rating: 23.53%
 

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8236
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2358 times
Been thanked: 3347 times
Contact:

Re: PC to PIC communication: Part 1

Postby medelec35 » Mon Mar 28, 2011 6:22 pm

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 :)
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: PC to PIC communication: Part 1

Postby Spanish_dude » Mon Mar 28, 2011 6:34 pm

Thanks Martin ;)

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

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 381 times
Been thanked: 713 times
Contact:

Re: PC to PIC communication: Part 1

Postby JohnCrow » Mon Mar 28, 2011 7:08 pm

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)
1 in 10 people understand binary, the other one doesn't !

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8236
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2358 times
Been thanked: 3347 times
Contact:

Re: PC to PIC communication: Part 1

Postby medelec35 » Mon Mar 28, 2011 8:13 pm

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 )
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 92 times
Been thanked: 8 times
Contact:

Re: PC to PIC communication: Part 1

Postby achillis1 » Tue Mar 29, 2011 6:28 am

Worked!!

Thank you!

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 92 times
Been thanked: 8 times
Contact:

Re: PC to PIC communication: Part 1

Postby achillis1 » Tue Mar 29, 2011 6:38 am

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

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 92 times
Been thanked: 8 times
Contact:

Re: PC to PIC communication: Part 1

Postby achillis1 » Tue Mar 29, 2011 6:44 am

Now the TOOLS>ADITIONAL CONTROLS is all greyed out and cannot select it!

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: PC to PIC communication: Part 1

Postby Spanish_dude » Tue Mar 29, 2011 4:20 pm

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.

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 92 times
Been thanked: 8 times
Contact:

Re: PC to PIC communication: Part 1

Postby achillis1 » Fri Apr 15, 2011 6:14 am

Hello,

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

Thank you

Best Regards,

Andreas Achilleos