LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert .HEX to .BIN file

Hi guys,

 

Basically Im designing a Bootloader Interface over CAN application. The previous software took a .HEX file and sent it down the CAN network. This worked almost perfectly but unfortunately we had problems on the recieveing device (PIC 18F). So the decision was taken to now send a .BIN file down the CAN, which was a lot easier as it only needed to setup the memory start point once, then stream down the contents of the file.

 

The problem I have is converting a .HEX file into .BIN file using the application. At the moment im using the HEX2BIN.exe (http://www.tech-tools.com/d_hx2bin.htm) application, which uses a command line interface to convert the .HEX file into a .BIN.

 

I have used the following code to do this:

 

int CVICALLBACK hiddenFileBrowserCB (int panel, int control, int event,void *callbackData, int eventData1, int eventData2)
{
	int selectedIndex=0;
	char * file = malloc(sizeof(char) * 200);
	char h2bPath[200] = "..\\HEX2BIN.exe ";
	char h2bTarget[200] = "..\\bin\\h2b.bin ";
	char h2bSource[200] = {0};
	char size, len, iloop = 0, validHexFile = 0;
	char dotHexPos = 0;
	
	switch (event)
	{
		case EVENT_SELECTION_CHANGE:
			
			GetCtrlAttribute (panelHandleFileBrowse, hiddenFileBrowserIndex, ATTR_CTRL_INDEX, &selectedIndex);
			
			GetValueFromIndex(panelHandleFileBrowse, hiddenFileBrowserIndex, selectedIndex, file);			
			
			if(file == NULL)
			{
				SetCtrlVal(panelHandle, PANEL_HEXFILE, " ");
			}
			else
			{
				SetCtrlVal(panelHandle, PANEL_HEXFILE, file);
				HidePanel (panelHandleFileBrowse);
			
				SetCtrlAttribute(panelHandle,PANEL_CMDWRITEFLASH,ATTR_DIMMED, 0);
				
				SetCtrlAttribute(panelHandle,PANEL_PROGRESS_WRITE,ATTR_VISIBLE, 1);  
				SetCtrlAttribute(panelHandle,PANEL_PROGRESS_WRITE,ATTR_FILL_COLOR,VAL_GREEN); 
				
				SetCtrlAttribute(panelHandle,PANEL_PROGRESS_READ,ATTR_VISIBLE, 1);
				SetCtrlAttribute(panelHandle,PANEL_PROGRESS_READ,ATTR_FILL_COLOR,VAL_BLUE);
				//SetCtrlAttribute(panelHandle,PANEL_PROGRESS_WRITE,ATTR_FRAME_COLOR,VAL_DK_GREEN);   
				
				
				/*Release Code for Selecting Hex file, Checking it then Converting into .Bin*/
				len = strlen(file);
				
				if(strstr(file,".hex") != NULL)
				{
					if((file[len-3] == 'h') && (file[len-2] == 'e') && (file[len-1] == 'x'))
					{
						validHexFile = 1; //true
					}
				}
	
				if(validHexFile)
				{
					//Build up command to convert .hex to .bin file
					memcpy(h2bSource, file, strlen(file));		   
					
					size = strlen(h2bSource);
					h2bSource[size] = ' ';
				
					size = strlen(h2bPath);
					memcpy(&h2bPath[size],h2bSource, strlen(h2bSource));
				
					size = strlen(h2bPath);
					memcpy(&h2bPath[size],h2bTarget, strlen(h2bTarget));
				
					LaunchExecutable(h2bPath);
				
					ReadHexFile("..\\bin\\h2b.bin");
					
					//Activate Write Flash Button
				}
				else
				{
					SetCtrlVal(panelHandle, PANEL_HEXFILE, " ");
					//Keep write flash button deactivated
				}
			}
			
			break;
	}
	return 0;
}

 

Now this code does do it's job however its extremely touch and go, for example if the .BIN file doesn't exist it may cause an error, or when run as an executabable LabWindows displays a System IO error although the conversion still works. And when using it on another computer it doesn't work at all.

 

Anybody have any ideas or alternatives or know of any C library that could do similar functions without the headaches?

 

Much appreciated

 

 

0 Kudos
Message 1 of 4
(6,687 Views)

Work update: Well I have worked around the flakiness of the system by copying the .hex file to a local folder to the application, and only running it from an address without no spaces. It works fine. However im still getting a "Standard Input/Ouptut Unable to open file ..\bin\bootload.bin" message box popping up.....even though the Hex2Bin application has run successfully converting the. .hex to the .bin. I have included the the error image.

0 Kudos
Message 2 of 4
(6,676 Views)

Hi ApeKind,

 

I'm John McLaughlin, an applications engineer here at National Instruments and I'll be looking into the error message you have been experiencing. Sometimes Windows based software has trouble opening up files the have a path of length longer than 256 characters, could this be the case?

 

Many Thanks,

John McLaughlin
Academic Account Manager
National Instruments UK & Ireland
0 Kudos
Message 3 of 4
(6,649 Views)

Remember that LaunchExecutable() doesn't wait for the command to complete before returning, so if you have:

 

                                        LaunchExecutable(h2bPath);
				
					ReadHexFile("..\\bin\\h2b.bin");

then it is unlikely that the .bin file will have been created by the time you execute ReadHexFile(). Try using system(), which waits until the command is completed, instead of LaunchExecutable().

--
Martin
Certified CVI Developer
0 Kudos
Message 4 of 4
(6,642 Views)