I have a c++ program that handles serial communications. At this stage, serial settings (ports, baud rate, settings) are hard-coded into the program.
I'd like to re-build the program so that it will take in options/flags to control each of the settings, such as
the command above will listen on COM5 at rate 115200
c:\user\command.exe -p 5 -r 115200
I've been reading about command line arguments, and I've tried to play with it like this:
butswitch statement doesn't allow checking char data type.
int main(int argc, char **argv)
any insight will be so helpful!!
switch allows switching on a char. What it doesn't necessarily like is switching on a char*. If all your arguments are one letter long, then that letter will be argv[x] (where x is the argument number, and argv[x] is supposed to be the hyphen that precedes it). Note also that you can increment argv to the next argument....
thanks!! I solved the problem!
In my opinion the best way to handle command line arguments is the Boost.ProgramOptions library.
actually, i ran into problem
that is an array of characters. yet, i need to get com port and baud rate, which has been declared as INT16. Is there any way to get around this?
All of my serial-com functions has been written based on INT3 baud rate and com-port.
Do you mean something other than "use atoi or strtol"?
yeah. I tried that but the compiler won't let me
atol: cannot convert parameter 1 from char` to `const car`
Looks like you're trying to pass a single character to a function expecting a string.
based on my main()
arv is char pointer. I'd like to passed something like
int main(int arc, char *arv)
"5" will be parsed into a serial-com function to open corresponding COM ports.
command.exe 5 filename.txt
'filename.txt" will be parsed into another serial-com funciton to stream output from serial line into file.txt
The problem is that serial-com open-port functions is expecting an integer; whereas, "5" is a char.
You were complaining about atol just now, not main, correct? atol expects not a single character (like argv), but a string (like argv).
I see, so "atol" doesn't get a single character.
yes, i was trying to get around "char to int" conversion error, and you brought up "atol" so i tried out and got the error above.