Fez while(true) loop ends before i tell it to

So basically in my code, I have a while(true) loop that constantly reads a port looking for new information. However, after about 15 seconds of idleness, it crashes/the thread exits. Any insight to why this is happening or how to fix it?

There is almost never a “crash” on managed systems. Instead, you get an exception with information on what went wrong.

well, the way i have the code work, it starts the CDC with debugging, then it runs a while(true) loop and constantly reads 8 bytes off the port.

my interface is very simple. Its just a windows form with a button that writes 8 bytes to the virtual COM port. It works fine, but once you stop sending it data, after about 15 seconds it says that COM4 doesn’t exist anymore, even though the device manager still shows COM4 as a port.

That is what windows says but what your FEZ? What do you see on the debugger? That will be the answer for all this actually :slight_smile:

hahaha right, well, the code is not right in front of me now, but from what i recall, it says
thread has exited - and then a bunch of numbers which look like a memory address… Any guess?

It would help us (and you in the end) if you would post your code.

II agree with Eric there’s a bunch of reasons your thread could exit; exception, destroying the thread else where, out of memory, etc. When you’re in front of your code post it up and we’ll have a look

ah, finally back at my main machine so I can post the snippet.

This is the form in its most condensed form.


OutputPort solenoid1 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di30, false);
OutputPort safety = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di31, false);
USBC_CDC cdc = USBClientController.StandardDevices.StartCDC_WithDebugging();

while (true)
            {
               //read from buffer with a fresh variable every time
                byte[] buffer = new byte[8];
                cdc.Read(buffer, 0, buffer.Length);
                char[] output = UTF8Encoding.UTF8.GetChars(buffer);
                string outputstring = "";
                
               //convert it to a string
                for (int i = 0; i < output.Length; i++)
                {
                    outputstring += output[i];
                }

                //echo
                if (outputstring != "")
                {
                    cdc.Write(buffer, 0, buffer.Length);
                }
               //if you send the proper command, turn on a digital for two seconds
                if(outputstring.Equals("solenoid".Trim()))
                {                   

                    solenoid1.Write(true);
                    System.Threading.Thread.Sleep(2000);
                    solenoid1.Write(false);
                }
            }

and thats it!

You say it happens when you stop sending data to it. My guess would be that the cdc.Read command throws a TimeoutException or something like that.

Put a try { } catch ( Exception e) { } around the while and set a breakpoint in the catch clause.

I love being able to step in code and being able to attach debugger. Do that and you will see the exception and know the cause of the problem…as suggested already.

right, and i love being able to do that too as my career is in software development. However I am new to the panda and its nuances. I am also the author of the post that was having problem using the debugger at the same time as the CDC. when i start the CDC, the com port opens, but I lose the panda as a debugable interface, so Im kind of debugging in the dark, and am mostly debugging by raising a bunch of events in the program code and echoing them to the client side. Gus, you did suggest just getting a shield and setting up rs232, but I had found a decent solution until this problem, and was content for the time being haha.

The exception timeout is the most plausible , but the CDC doesnt immediately exit the thread when it hits an exception. are there events that are raised when the CDC receives data? maybe similar to how there is a dataReceived event for COM ports?

I would put that out of the loop.

See if there is string constructor that accepts char array.

you don’t need to trim it

[quote]//echo
if (outputstring != “”)
{
cdc.Write(buffer, 0, buffer.Length);
}[/quote]

Check Length of the string. In general I would check the number of bytes returned right after cdc.Read and avoid all that string construction code earlier.

oh wow. I wasn’t setting the read timeout property haha