The program below is supposed to first, open the port, then send a command and wait for the port to say it is done. Then after all the commands are sent, it closes the port and ends the program. After sending four commands to the port, the program errors out and says it is a segfault error. If anyone can figure out what is going on, please let me know.
Code:
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
int configure(void); // function prototype
int run(void); // function prototype
int a; // initializing of variable a
int b; // initializing of variable b
int main()
{
printf("Running Program.\n");
typedef long(*importFunction1)(long, long); //OpenPort signature
typedef long(*importFunction2)(char*); //DriverSendToPort signature
typedef long(*importFunction3)(char*, long); //WaitForChar signature
typedef long(*importFunction4)(void); //ClosePort signature
//Loading all functions at one time.
importFunction1 OpenPort;
importFunction2 DriverSendToPort;
importFunction3 WaitForChar;
importFunction4 ClosePort;
//load DLL file
HINSTANCE hinstLib = LoadLibrary("VxmDriver.dll");
if (hinstLib == NULL) {
printf("ERROR: unable to load DLL\n");
getch();
return 1;
}
// Get OpenPort pointer
OpenPort = (importFunction1)GetProcAddress (hinstLib, "OpenPort");
if (OpenPort == NULL) {
printf("ERROR: unalbe to find OpenPort\n");
getch();
FreeLibrary(hinstLib);
return 1;
}
// Get DriverSendToPort pointer
DriverSendToPort = (importFunction2)GetProcAddress (hinstLib, "DriverSendToPort");
if (DriverSendToPort == NULL) {
printf("ERROR: unalbe to find DriverSendToPort\n");
getch();
FreeLibrary(hinstLib);
return 1;
}
// Get WaitForChar pointer
WaitForChar = (importFunction3)GetProcAddress (hinstLib, "WaitForChar");
if (WaitForChar == NULL) {
printf("ERROR: unalbe to find WaitForChar\n");
getch();
FreeLibrary(hinstLib);
return 1;
}
// Get ClosePort pointer
ClosePort = (importFunction4)GetProcAddress (hinstLib, "ClosePort");
if (ClosePort == NULL) {
printf("ERROR: unalbe to find ClosePort\n");
getch();
FreeLibrary(hinstLib);
return 1;
}
printf("All files have loaded successfully.\nHit any key to enter program.\n");
getch();
//Run all functions and free library/print statement on success...
OpenPort(2,9600);
for ( a = 0 ; a < 10 ; a++ ) {
if ( a > 0 ) {
DriverSendToPort("C,I1M400,R,");
WaitForChar("^",0);
}
for ( b = 0; b < 10; b++ ) {
DriverSendToPort("F,C,I2M400,R,");
WaitForChar("^",0);
}
DriverSendToPort("C,I2M-4000,R,");
WaitForChar("^",0);
}
DriverSendToPort("C,I1M-4000,R,");
WaitForChar("^",0);
DriverSendToPort("Q,");
ClosePort();
FreeLibrary(hinstLib);
printf("successfully completed");
getch();
return 0;
}