We cannot know, if your python code also has a problem. But your Arduino code will send the series of "V" only once.
You are using this while loop:
while(counter < maxnum)
in there you are incrementing the variable counter
. But after you left the while loop, you are never resetting counter
to zero. So the next time, counter
is still equal to maxnum
. Thus the while loop will not run again. To solve that, insert
counter = 0;
directly after your while loop. Though, for such loops its normally easier to just use a for loop.
Also you might have a second problem. Currently the code waits for Serial data to be received. When something is received, the code will enter the
if(Serial.available() > 0)
statement. But this will stay true, unless you are actually reading the data. Currently the code inside the if statement will be executed on every loop()
interation forever. So if you are applying the above fix to the first problem, the code will send "V" forever after you send the first byte to the Arduino. To actually only react to data being received, you need to read the data from the buffer - even if you are throwing the data away:
Serial.read();
What is the difference when an arduino has been unplugged vs when a serial port connection was closed?
For the Arduino itself? Nearly none. Reopening the serial connection will cause the Arduino to reset. The code then starts again from its beginning. Thats also what happens, when you power cycle (replugging) the Arduino. I think there are some little details, where both differ, but that certainly is not the problem here.
For the computer, replugging means, that the USB device (the USB-to-Serial chip on the Arduino to be precise) gets disconnected and again connected. That can lead to the OS allocating a different com-port/device-file for it, than before. Though that doesn't directly do something with the Arduino code (apart from the mentioned power cycle with reset).
For resetting (preserved from comment of OP):
I found that by setting DTR to 0 then 1, then 0 again, right before closing the port, the arduino resets.