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

Adding source code to Blogger

I have been trying for quite some time to add source code to my blog. Just copying and pasting the code works fine, but there is no proper formatting or colours used in normal source code editors. After browsing the internet and reading many examples, I finally came across this website that helped me get it done.
I have used the SyntaxHighlighter script and from what I have seen, this seems to be the most used method. It supports a number of different programming languages and once set up, very easy to implement.
Please back up your blog before trying this as you need to edit the html template. From your Dashboard, navigate to Template, and then select Edit HTML. A warming will pop up so select Proceed. The HTML template will now be displayed.
Navigate down in the template to find </head>
Copy the code below and paste it above </head>


































Preview your template to ensure that it still looks ok. If all is fine, then save the template and close the editor.
The next step is to make the script work. You do this by using the <pre> and </pre> HTML tag. In between these tags is where you add your code. You also need to specify which script to use depending on what language your source code is in. Here is the example I used to display C# source code.

private void UpdateCounter()

{

if (iCounter.Value < 50000)

{
iCounter.Value++;

{

}
else

iCounter.Value = 1;
}
}
You need to add the tags and code in the HTML editor. Once the source code has been added, then you can go back the the text editor to complete the post.
That’s about it. Once the template has been edited and your set up is correct, adding source code it pretty easy.
Greg