Thanks Migf1. I'll be sure to check it out. The code seems to work. If I have no COM ports, it prints 0 COM ports found, if it finds them, it lists them. I looked at the source code, serialport.c, and saw what happened if something went wrong. I could be wrong though. If I didn't have the * in front of ports (to check the first element), when there where 0 COM ports found, the code didn't work. It think there was a COM port. I'm lucky in the sense that I have a USB to serial adapter on this PC, so I can easily add / remove the serial port for testing purposes.
I tried creating separate functions for the port stuff, like enum_ports, print_ports, free_ports. The compiler didn't like my code at all.
I first tried this:
Code:
int enum_ports(struct sp_port **ports); /* Function to enumerate COM ports on the system */
int print_ports(struct sp_ports **ports); /* Function to print the enumerated ports found on the system */
int free_ports(struct sp_ports **ports); /* Function to free up memory from the libserialport malloc calls */
That didn't work. The compiler complained a lot, with messages like:
Code:
In file included from src/main.c:12:0: src/prototype.h:7:23: warning: 'struct sp_port' declared inside
parameter list int enum_ports(struct sp_port **); /* Function to enumerate COM ports on the system */
Then I tried this:
Code:
int enum_ports(struct sp_port **ports); /* Function to enumerate COM ports on the system */
int print_ports(struct sp_ports **ports); /* Function to print the enumerated ports found on the system */
int free_ports(struct sp_ports **ports); /* Function to free up memory from the libserialport malloc calls */
From what I was reading, this could be caused by the fact that I have the struct defined in main, like this:
Code:
struct sp_port **ports; /* structure to hold COM ports for libserialport library */
Perhaps I need to declare it globally in my main.c file? Before main()... Also, because I'm planning on passing a pointer of type structure ports to the functions, maybe I need an extra asterisk? I guess this is just more proof I still need more reading.
The original forum was nice but a bit confusing at parts because there where questions being asked and it would through me off a bit. Like I was following what was being said, then someone would ask a question with incorrect info, like hey, can I do this? and I'd start confusing what they where trying to do with the correct way. I'm sure this is just a problem that I have. Other people can probably read it just fine. I found some book I started reading, C Programming, A Modern Approach - 2nd Edition, by K.N. King. I've just started from the beginning. Figured I'd fill in the some of the missing caps.