Thanks, before I put the project on hold and I started relearning C, I did try to follow your advice. I setup a switch statement that checks for various error codes. From looking at the source of serialport.c (from the libserialport library), I tried my hardest to see when
ports returns unallocated. If we function
sp_list_ports returns SP_OK, I was always calling
sp_free_ports_list. If
SP_OK didn't get returned, I tried displaying various error messages. I'm worried though that I don't fully understand the code as well as you, hence the reason I'm refreshing myself by reading C books. From what I could tell, if no ports are found, but the call succeeds (ie,
ports gets allocated),
SP_OK gets returned. The only time I don't call
sp_free_ports_list is if one of the pre-defined macro's in libserialport gets called, like in the below code (this was in my
enum_ports function).
Code:
/* Get a list of serial ports on the system, if any, and store them in pointer ports */
x = sp_list_ports(ports);
switch(x) {
case SP_OK: /* Everything's okay, break */
break;
case SP_ERR_ARG: /* Should never happen */
printf("ERROR: Invalid arguments passed to function sp_list_ports.\n");
printf(" Returning control to the operating system.\n");
exit(-1);
case SP_ERR_SUPP: /* Enumeration not supported on this system */
printf("ERROR: We're sorry but serial port enumeration isn't supported on this OS yet.\n");
printf(" Returning control to the operating system.\n");
exit(-1);
case SP_ERR_MEM: /* Not enough memory */
printf("ERROR: Could not allocate enough memory, malloc failed.\n");
printf(" Returning control to the operating system.\n");
exit(-1);
default:
printf("ERROR: Unknown error. Please report this to the author of this program: %d.\n", x);
printf(" Returning control to the operating system.\n");
exit(-1);
}
Once I finish reading, I'll come back to the project and hopefully won't need any help, but if I do, I'll be sure to ask. It's nice that you guys are so friendly and helpful. If it wasn't for this forum, I wouldn't of even tried getting back into C programming. It definitely makes my life a bit more exciting, makes me feel like I'm finally doing something with it again
Thanks!