C# dll for programming Microchip PIC’s

In the test development environment, the need often arises where a microcontroller needs to be programmed before the testing can be completed. This can either be done with the development environment or through the command line if this method is provided for by the manufacturer. 
Programming in the IDE is nice and easy, but very slow and needs the user to press the buttons. Often in the test environment, this process needs to be automated with as little user input as possible. Here is where using the command line parameters comes in so handy. You will need this hard-to-find document which outlines the commands to use.
In my application I am programming the PIC18LF26K22 microcontroller from Microchip and using the MPLAB PM3 device programmer. To test the devices I am using LabVIEW and TestStand from National Instruments. 
There is a provision in LabVIEW where you can run command line programs, but I have found this to be a bit slow when interfacing with the PM3 programmer. This made me think of writing a C# dll to perform this task. Now I am no expert at C# programming, but am trying to learn where I can so thought this would be a good exercise. 
I started off be investigating the Process Class and found just what I was looking for. All you need to do is create a new object of the Diagnostics.Process class. Then build the string that you need to send. The make up of this string is explained in pdf attached above. 

public bool LoadImageToPm3(string pm3CmdPath, string port, string hexImagePath)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = pm3CmdPath;
proc.StartInfo.Arguments = "/" + port + " /P18LF26K22 /F" + '"' + hexImagePath + '"';
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
try
{
proc.Start();
strStandardOutput = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
int iSuccessful = strStandardOutput.IndexOf("Operation Succeeded");
if (iSuccessful > 0)
{
return true;
}
else
{
return false;
}
}
catch (SystemException e)
{
return false;
}
}
This is an example of how to load an image onto the PM3. I build the string and then once I start the process, I read the standard output to make sure the the operation was successful.

The dll I wrote can handle loading an image to the programmer, program a device where the image is already on the programmer and erase the device. There are many other processes that can be performed but for the production testing that I am doing, this is all I need.

In the project attached, I also wrote a simple form application and a TestStand sequence to test the dll. You can find those files here.

By using a dll, I decreased my programming time from 60sec in LabVIEW to 15sec. When the device being tested only takes 30sec to test, an extra 45sec programming time is huge.

I hope this helps and if you need anything explained further, please leave a comment and I will try help where I can.

Greg

LabVIEW: Simulating keyboard events

Another useful trick in LabVIEW using the user32.dll is to simulate keys being pressed on the keyboard. I haven’t come across the need very often, however when using console applications, it can be necessary.
I ran into this problem while running a console application that runs a GPS simulator. Getting the application to run was easy and when the simulated trip is complete, the console closes automatically. The problem came in when I wanted to stop the controlling LabVIEW application. When I stopped the LabVIEW application and if the console was still running, I needed to close it and wanted to do it all in one step, instead of forcing the user to stop the LabVIEW application AND close the console window.
After some research I found out that the user32.dll has a keyboard event method so I decided to use that. I first had to make the console window active, find out how here, and then I would run this application as a sub-vi to simulate a Ctrl+C which was needed to stop the GPS simulation and close the console. 

What happens is that the two integers need to be passed to the dll. The first element is the decimal representation of the ASCII character that needs to be simulated and the second is what motion the button makes.
EG: 162 is represents the Ctrl key and 67 is the decimal representation for the ASCII uppercase C. The second integer that is passed to the dll is used to decide what the button is doing. 0 Simulates that the button is being pressed and 2 simulates that the button is being released.
Therefore, by writing each cluster to the dll in a for loop with 50ms delay between loops, Ctrl+C is written to the active console window which will stop the GPS simulation and close the window.
To download the example VI, use this link.
Greg

LabVIEW: Making a window active

A small issue that I have been trying to resolve is when a LabVIEW application runs and multiple windows are open, the need is often there to bring a window to the front as some stages of the application. 

Now I am sure there are many ways to perform this task, but the easiest and most reliable way that I found is to use the user32.dll that is part of the windows installation. This dll is easily accessed by using Call Library Function Node. This can be found in Connectivity >> Libraries & Executables.


The three methods that are used are FindWindowA, SetForegroundWindow and ShowWindow. By wiring these us and adding the correct window name that you want to bring forward, when the program is run whatever window name is selected will be brought to the front. 

There are many other tasks that can be performed by using the user32.dll. I have used it in a few other places in my programs and will add some more posts in the future of where it can be used.

To download the example VI, use this link.

Greg