Having trouble closing and reopening of communication port RS232
-
hi I'm trying to get the weight attached to a computer using RS232 com port. I can read the weighing, but when I walk out the form , and came again - My program and computer freezing. this is my code: private void Main_Load(object sender, EventArgs e) { port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); port.DataReceived += new s system.IO.Ports.SerialDataReceivedEventHandler(Recepcion); if (port.IsOpen == false) { try { port.Open(); } catch (Exception oex) { MessageBox.Show(oex.ToString()); } } } private void Actualizar(object s, EventArgs e) { //I try this lblMSG.Text = ExtractDecimalFromString(port.ReadLine()).ToString(); port.DiscardInBuffer(); //Also I try this lblMSG.Text = ExtractDecimalFromString(port.ReadExisting()).ToString(); port.DiscardInBuffer(); } private void Recepcion(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { try { this.Invoke(new EventHandler(Actualizar)); } catch { } } and when i close the form i do this: port.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(Recepcion); GC.Collect(); //also i try to remark this port.Close(); port.Dispose(); this.Close(); It is not always freezes, sometime yes...sometime no.... I searched the entire network still does not answer and solution. I'm really despair. I wish to find a solution here
-
hi I'm trying to get the weight attached to a computer using RS232 com port. I can read the weighing, but when I walk out the form , and came again - My program and computer freezing. this is my code: private void Main_Load(object sender, EventArgs e) { port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); port.DataReceived += new s system.IO.Ports.SerialDataReceivedEventHandler(Recepcion); if (port.IsOpen == false) { try { port.Open(); } catch (Exception oex) { MessageBox.Show(oex.ToString()); } } } private void Actualizar(object s, EventArgs e) { //I try this lblMSG.Text = ExtractDecimalFromString(port.ReadLine()).ToString(); port.DiscardInBuffer(); //Also I try this lblMSG.Text = ExtractDecimalFromString(port.ReadExisting()).ToString(); port.DiscardInBuffer(); } private void Recepcion(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { try { this.Invoke(new EventHandler(Actualizar)); } catch { } } and when i close the form i do this: port.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(Recepcion); GC.Collect(); //also i try to remark this port.Close(); port.Dispose(); this.Close(); It is not always freezes, sometime yes...sometime no.... I searched the entire network still does not answer and solution. I'm really despair. I wish to find a solution here
From where I can see this, I believe you are facing the UI freezing effect (there are various threads that explain what it is in technical aspect). The effect is that, when your application is doing a task that takes some time (if I remember correctly, it was more than 100ms or 1 second) then you need to run that task on a background thread, otherwise your application will freeze and user might think that the application is stuck, because the UI thread doesn't get enough time to update its components or respond to UI interaction; although it is still working properly. The way you wrote the code, it is a bit tough to consider where you might put a background thread to execute. But the nearest of it to cover the
try...catch
block in theRecepcion
function. There are various ways to solve this problem, and various ways to increase the pain, so I will leave that in your hand to change the code in order to minimize the UI thread to wait for the updating. Read more here, [Asynchronous Programming with Async and Await (C# and Visual Basic)](https://msdn.microsoft.com/en-us/library/hh191443(v=vs.110).aspx) [wpf - C# UI freezing when serial port attempts to connect - Stack Overflow](http://stackoverflow.com/questions/15635862/c-sharp-ui-freezing-when-serial-port-attempts-to-connect) [c# - What causes my UI to freeze when closing a serial port? - Stack Overflow](http://stackoverflow.com/questions/12913936/what-causes-my-ui-to-freeze-when-closing-a-serial-port)The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
From where I can see this, I believe you are facing the UI freezing effect (there are various threads that explain what it is in technical aspect). The effect is that, when your application is doing a task that takes some time (if I remember correctly, it was more than 100ms or 1 second) then you need to run that task on a background thread, otherwise your application will freeze and user might think that the application is stuck, because the UI thread doesn't get enough time to update its components or respond to UI interaction; although it is still working properly. The way you wrote the code, it is a bit tough to consider where you might put a background thread to execute. But the nearest of it to cover the
try...catch
block in theRecepcion
function. There are various ways to solve this problem, and various ways to increase the pain, so I will leave that in your hand to change the code in order to minimize the UI thread to wait for the updating. Read more here, [Asynchronous Programming with Async and Await (C# and Visual Basic)](https://msdn.microsoft.com/en-us/library/hh191443(v=vs.110).aspx) [wpf - C# UI freezing when serial port attempts to connect - Stack Overflow](http://stackoverflow.com/questions/15635862/c-sharp-ui-freezing-when-serial-port-attempts-to-connect) [c# - What causes my UI to freeze when closing a serial port? - Stack Overflow](http://stackoverflow.com/questions/12913936/what-causes-my-ui-to-freeze-when-closing-a-serial-port)The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
hi I'm trying to get the weight attached to a computer using RS232 com port. I can read the weighing, but when I walk out the form , and came again - My program and computer freezing. this is my code: private void Main_Load(object sender, EventArgs e) { port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); port.DataReceived += new s system.IO.Ports.SerialDataReceivedEventHandler(Recepcion); if (port.IsOpen == false) { try { port.Open(); } catch (Exception oex) { MessageBox.Show(oex.ToString()); } } } private void Actualizar(object s, EventArgs e) { //I try this lblMSG.Text = ExtractDecimalFromString(port.ReadLine()).ToString(); port.DiscardInBuffer(); //Also I try this lblMSG.Text = ExtractDecimalFromString(port.ReadExisting()).ToString(); port.DiscardInBuffer(); } private void Recepcion(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { try { this.Invoke(new EventHandler(Actualizar)); } catch { } } and when i close the form i do this: port.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(Recepcion); GC.Collect(); //also i try to remark this port.Close(); port.Dispose(); this.Close(); It is not always freezes, sometime yes...sometime no.... I searched the entire network still does not answer and solution. I'm really despair. I wish to find a solution here
1. Check that there are bytes to read (
port.BytesToRead > 0
) 2. If you cannot be sure that every message will be terminated by a new line, useport.Read(buffer, 0, count);
instead ofport.ReadLine()
. -
hi I'm trying to get the weight attached to a computer using RS232 com port. I can read the weighing, but when I walk out the form , and came again - My program and computer freezing. this is my code: private void Main_Load(object sender, EventArgs e) { port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); port.DataReceived += new s system.IO.Ports.SerialDataReceivedEventHandler(Recepcion); if (port.IsOpen == false) { try { port.Open(); } catch (Exception oex) { MessageBox.Show(oex.ToString()); } } } private void Actualizar(object s, EventArgs e) { //I try this lblMSG.Text = ExtractDecimalFromString(port.ReadLine()).ToString(); port.DiscardInBuffer(); //Also I try this lblMSG.Text = ExtractDecimalFromString(port.ReadExisting()).ToString(); port.DiscardInBuffer(); } private void Recepcion(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { try { this.Invoke(new EventHandler(Actualizar)); } catch { } } and when i close the form i do this: port.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(Recepcion); GC.Collect(); //also i try to remark this port.Close(); port.Dispose(); this.Close(); It is not always freezes, sometime yes...sometime no.... I searched the entire network still does not answer and solution. I'm really despair. I wish to find a solution here
In my experience, "ports" need to be somewhat "stable". You can't arbitrarily open and close them without expecting a few "connection" fails. I typically create a single instance for a port in a "longer running" app (including "weighing"), and share that instance; I don't keep opening and closing the port. Weigh scales also need "wait" times to "center" and respond, etc. At least 100 ms. I can't picture your scenario as being realistic: repeatedly loading a form to perform a "weighing".
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal