dmicha16 / simple_serial_port

Simple C++ serial port communication with an Arduino for Windows

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Idea for initialization.

Felix-Brodmann opened this issue · comments

commented

I changed the initialization in my project and I think the way I handle it would help some people.
Here is the change:
Instead of calling the initializer
SimpleSerial(char* com_port, DWORD COM_BAUD_RATE)
I added a
void init(char* com_port, DWORD COM_BAUD_RATE)
function which alows me to initialize the function at the beginning and start the connection later on.

My example:
I wanted to let the user decide which Com-Port to use and for that reason I needed to make the connection later on (with the specifiy Com-Port; inside a switch-case-statement). But I couldn't initialize a SimpleSerial-object inside the switch-case statement. So I made the change stated above and called the init function in the switch-case statement.

Great suggestion @Felix-Brodmann. For my scenario I'm giving the user the option to specify the COM port at runtime (via a command-line argument). Since I have some functions referencing the serial port object outside of main, it is necessary to create the object as a global variable. However, re-defining the object within main, doesn't seem to make make it accessible to the functions outside of main.

Creating a global object using a default constructor (no arguments) and calling an "init()" on the already existing global object seems to be the best way out.

Hi guys,

Thanks for your suggestions, if you can make PR with your changes, I'd be more than happy to review&merge them!

David

commented

Just made a PR. Feel free to check it out, I also provided a little code sketch for using the new enhancement.

commented

Is it possible that you have deleted the previous constructor?
SimpleSerial(char* com_port, DWORD COM_BAUD_RATE);
Because I'm getting an error with the parameters passed as there is no constructor accepting paremeters anymore.

@Pgbofias Could you make a PR with a fix?

commented

Not completely sure if I know how to make that, I'll try.

commented

I don't know if I'm doing something wrong but doesn't let me push the branch, I'm sorry.
Anyways you only have to add this line in the header as another public method
SimpleSerial Serial(char* com_port, DWORD COM_BAUD_RATE);
And then copy the init() declaration but changing it's header to
SimpleSerial::SimpleSerial(char* com_port, DWORD COM_BAUD_RATE)

I guess you should also add something like
if (connected_ = true) cout << "Warning: COM port already connected" << endl; else { io_handler_ = CreateFileA(static_cast<LPCSTR>(com_port), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
etcetera, in the init() declaration to avoid some conflict.

Yes, there's branch restrictions, so you won't be able to push to this repo. First, you'll have to fork the repo and then you can make a PR, which then I can approve. Here's a guide.

I can't myself edit and compile this anymore as I'm not on windows, sorry!

commented

I think that's it

I merged your PR @Pgbofias, so I'm closing this issue. Thanks!