Arduino stoped sending data to processing

Hello, i am having the problem with arduino and processing.

I have a arduino mega with Sharp IR sensor sensing data to Processing connected to a Processing program running on a MacMini.
The serial speed is 115200,
but the problem is that when I touch the board, I think the USB cable isn't tight enough and it looses connection, so that the Arduino stops sending data to the computer.

So the only way to fix it would be re-opening the processing app.

Any way to fix this, maybe somehow to reset connection on the go of something.
Thanks

Any way to fix this

Isn't that obvious? Fix the loose connection. Or, quit touching it.

Really, implementing a software solution to "fix" a hardware problem is not the right answer.

But i'm not sure the problem is this, still trying to figure out the problem.

Could slowing down the transfer speed help?

But is there a way in processing to re-open serial connection if it failed?

Thanks

But is there a way in processing to re-open serial connection if it failed?

Creating a new instance of the Serial class, after deleting the old one, will re-establish the connection, by resetting the Arduino.

But i wont face easy access to the arduino board nor the macmini,
so a way i could fo something like

if(myPort1.available()>0) {}
else{
myPort1 = new Serial(this, serial1, 115200);
myPort1.bufferUntil('\n');

}

then it would try to re-open connection automatically

so a way i could fo something like

No. Don't do that. That would open a new connection, and reset the Arduino, whenever Processing had finished processing all pending serial data.

You'll need to include some serial-call-and-response (see the Processing example) protocol, where you try, from Processing, to talk to the Arduino. If the Arduino doesn't respond within some period of time, THEN do what you propose.