Brixi69

Hi !

Here is my problem ...

I developped in C# a class heriting from SerialPort. It contains functions structured like this :
serialPort.open();
....write....read.... ans so on;
serialPort.close();

I use a timer to run theses functions every miliseconds.

After a time, the application freeze. When i debug using "break all", i see a green arrow pointing serialPort.close(). When i press F11 to go to the next instruction, i can't see any yellow arrow. The green one disappear and my application stay freezed. When i break all a second time, the green arrow reappear pointing the serialPort.close() function etc.... The only solution is to stop debugging and run my application again.

Could anybody tell me where does it come from

Thanks for all !!!





Re: .NET Base Class Library bug in serialport.close() ?

Chris Vega

I have moved this thread here from Windows Forms forum. Hopefully you'll get a response from your question.

Regards,

-chris




Re: .NET Base Class Library bug in serialport.close() ?

nobugz

This is probably caused by the serial port still having bytes to send and the device has turned off the handshake. Check the BytesToWrite property value. Use DiscardOutBuffer() to throw away any unsent characters.






Re: .NET Base Class Library bug in serialport.close() ?

Brixi69

I thought it wa something like that too but i tried everything ans the problem's stil there. First, i thought it was due to multitasking problems but then i noticed i had the problem only with a single loop. Here is the code.

while(true)
{
read_FDP();
}

public Int16 read_FDP(byte adresse)
{
try { Close(); }
catch { };

if (!IsOpen) //If port closed
{

try { Open(); }
catch (Exception) { }

//If port is opened
if (IsOpen)
{
DiscardInBuffer(); //discard the buffer

try { Write(SOF_string); } //start of frame
catch(Exception){return (-1);}

trame_out[0] = 13; //command type
trame_out[1] = adresse; //adress

try{Write(trame_out, 0, 2);}
catch(Exception){ return (-1);}

try { Write("!"); } //end of frame
catch (Exception) { return 1; }

//on verifie si ca a fonctionne
try { Read(trame_in, 0, 3); } // Waiting for 3 chars
catch (Exception)
{
DiscardInBuffer();
DiscardOutBuffer();

try { Close(); }
catch { }; //Close the port
return (-1); //no response
}
try { Close(); }                         edit :                //GENERALLY THIS ONE IS THE PROBLEM
catch { }; //Close the port

if (trame_in[0] == '!' && trame_in[1] == 13)
{
return trame_in[2]; // Return the read data
}
else
return (-3);
}
}
return (-2);
}

Impossible to make it loop more than a few minutes.

Does anyone have a suggestion

Thanks for your help !

@@@





Re: .NET Base Class Library bug in serialport.close() ?

nobugz

A bunch of Close() calls, which one does it hang on You are also silently discarding exceptions, at least use Debug.Print to know what is going wrong. Also show us the code that initializes the SerialPort instance.






Re: .NET Base Class Library bug in serialport.close() ?

Brixi69

Yes you're right for the exceptions but this soft is only for a demonstration. I normally don't need them.

I will make some tries with printing.

I initialize the serialport just with that :

BaudRate = 921600; //Not a problem it uses a cdc driver
ReadTimeout = 300;
WriteTimeout = 300;

Thanks for your help








Re: .NET Base Class Library bug in serialport.close() ?

cn_sean

The same problem, waiting solution...



Re: .NET Base Class Library bug in serialport.close() ?

Brixi69

are we the only ones to have that problem and not the solution

Does anyone have others suggestions

Thanks a lot ;-)







Re: .NET Base Class Library bug in serialport.close() ?

nobugz

Which Close() does it hang on






Re: .NET Base Class Library bug in serialport.close() ?

Brixi69

I edited the sample code...

Thanks !!





Re: .NET Base Class Library bug in serialport.close() ?

nobugz

OK, it is not DiscardOutBuffer(). The documentation for the Close() method specifically warns that we should "wait for some amount of time" before opening the port again. The SerialPort class no doubt needs some time for threads to exit.

Well, let's punt and avoid opening and closing the port over and over again. Call Open() when you initialize the port and Close() when your program exits. This is the normal use for a SerialPort object and might avoid some obscure deadlock inside the class.

If you want a better answer, you'll need to tell us what exceptions are being raised...






Re: .NET Base Class Library bug in serialport.close() ?

Brixi69

Yes i saw that in the docs. Another person told me the same. I will have to change my code.

Thanks for your help ! I'll give some results later...

Thanks again !!!







Re: .NET Base Class Library bug in serialport.close() ?

Brixi69

Everything works...

Thanks a lot.





Re: .NET Base Class Library bug in serialport.close() ?

arnulfoPerez

I have the same issue. I have a pocket pc application that uses the IR port in raw mode using the serialport component from .net. It works most of the time but sometimes the port stops working
At the beginning i opened the port when opening the windows form and closed the port when leaving the form but then i tried to open and close the port after getting time outs on the comunication with a custom device and it seems to improve somehow the stablility of the code. However, now it hangs sometimes on the close call as described in the previous posts
No exceptions are thrown and when stoping the process in the debugger it is at the close call.

what is the correct sequence to flush and reset the port




Re: .NET Base Class Library bug in serialport.close() ?

arnulfoPerez

I have the same issue. I have a pocket pc application that uses the IR port in raw mode using the serialport component from .net. It works most of the time but sometimes the port stops working
At the beginning i opened the port when opening the windows form and closed the port when leaving the form but then i tried to open and close the port after getting time outs on the comunication with a custom device and it seems to improve somehow the stablility of the code. However, now it hangs sometimes on the close call as described in the previous posts
No exceptions are thrown and when stoping the process in the debugger it is at the close call.

what is the correct sequence to flush and reset the port