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.
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.
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...".
After that, go to the "Macro Settings" page and enable the "Enable all macros" radio button.
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".
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 : 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.
Then go to the "Tools" tab an click on the "Additional Controls".
Scroll down until you see a disabled checkbox "Microsoft Communication Control, version 6" and enable it.
There you go.
In your Toolbox you'll have a new icon, a phone, to be able to communicate through the COM ports.
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.
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
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
Add this next piece of code:
Code: Select all
Private Sub CommandButton1_Click()
MSComm1.Output = TextBox1.Text
End Sub
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 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
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 !
Best regards,
Nicolas L. F., aka Spanish_dude / pyroesp