Arduino Serial Port Hanging Issues

Hi everyone,

First and for most let me apologize in advance if I don't know how to use this forum properly. This is the first time I am using it to ask for your assistance and hopefully there maybe someone that could help me with the issues I have been having.

I recently ran into a wall with an intermittent problem I keep having. I am currently use the C# platform to interact with the Arduino Mega 2560.

Application:

Using Arduino as a communication bridge between the computer and the magnetic chip I am using. I am using my custom C# application to run test with the device by polling XYZ data from the chip while a Lexium MDrive motor spins the device the magnets are attached to to see the SNR from the chip data.

Board: Arduino Mega 2560 using a logic translator board from SparkFun BOB-12009 since the chip runs on 3.3V

Communications used: USB native serial port and I2C bus.

Arduino I2C bus set to 100kHz and timeout set to 200ms. External pull-ups set using translator board with 2kOhms resistors. Internal Pull-ups are turned off and I unsoldered the physical resistor package from the board.

C# side:

-Serial number is scanned
-Serial communication settings and open connection is established
-C# program then does a simple "Hello are you there?" command by simply sending a "V/r" to the Arduino so that if the Arduino response with the "I2C Bridge" Message, it will then continue and start polling data knowing the Arduino is present.

Arduino side:

-Takes in data from C# side and takes those sets of commands and interprets them and sends those commands to the chip.
-The chip then returns the result and the Arduino sends that response back to the C# application

Problem:

-The problem I have in short is that I don't know why, but the Arduino never hangs when it polls data during testing. But it hangs when the test is complete and then you plug in a new piece and start the test again. It hangs when you do a Write("V/r");which is the command I send to the Arduino to give me a verification that it is working still to start up the polling process. At this point, I get a timeout exception error and this problem won't let me talk to the Arduino board again unless I cut power to the Arduino and start it up again even when I close the port. I also get a null when I try opening the port even those the COM port is present.

-The problem is intermittent because sometimes it will do it almost immediately just running it one time or it will run 1000's of times a week and then get it sometimes and it only happens when writing this one simple command.

-I am using a third party I2C library from DSS Circuits called I2C.

My question to you all is if there is anything maybe my eyes are missing in the Arduino side that can be causing problems with this. I've used my C# application on other projects and the Arduino sketch has a very similar build as other applications. The only difference is that this one I am polling the data. But I never poll the data unless the Arduino responds to my C# application with the I2C Bridge message to validate it is communicating properly with the Arduino. This is where it has me stumped. It only seems to happen with the serial port. I was thinking maybe a buffer overflow issue but I am using Serial.flush() to wait for data to clear out of the buffer.

These are my current C# serial port settings:

arduinoPort.Close();
Thread.Sleep(200);
if ((arduinoPort.IsOpen == false))
{
arduinoPort.PortName = objData.comport;
arduinoPort.BaudRate = 115200;
arduinoPort.Handshake = Handshake.None;
arduinoPort.Parity = Parity.None;
arduinoPort.DataBits = 8;
arduinoPort.ReadTimeout = 200;
arduinoPort.WriteTimeout = 1000;
arduinoPort.DtrEnable = true;
arduinoPort.Open();

Note: I only open the serial port one time and it stays open. I just pause a backgroundworker using a boolean when the data collection is complete and then it just continues at the beginning of the worker as normal. Only time it closes is is if there is an exception in the try catch.

ComBridge_00.01.08.ino (5.97 KB)