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 !!!

bug in serialport.close() ?
groovieblonde
Does anyone have others suggestions
Thanks a lot ;-)
Humpty
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
RichardMGreen
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...
Jeff Michaud
Thanks !!
Thanes
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 !
@@@
Emmy008
Eleda
Alphadon
Regards,
-chris
YunZhou_MS
Beamer
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
Vinod Kumar Sah
Azim Ghoniem
Thanks a lot.
swdeveloper
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
jon why
Thanks for your help ! I'll give some results later...
Thanks again !!!