Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Having trouble closing and reopening of communication port RS232

Having trouble closing and reopening of communication port RS232

Scheduled Pinned Locked Moved C#
comsysadmin
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    goldsoft
    wrote on last edited by
    #1

    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

    A B L 3 Replies Last reply
    0
    • G goldsoft

      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

      A Offline
      A Offline
      Afzaal Ahmad Zeeshan
      wrote on last edited by
      #2

      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 the Recepcion 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 !~

      G 1 Reply Last reply
      0
      • A Afzaal Ahmad Zeeshan

        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 the Recepcion 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 !~

        G Offline
        G Offline
        goldsoft
        wrote on last edited by
        #3

        i try this.... still same problem

        1 Reply Last reply
        0
        • G goldsoft

          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

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          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, use port.Read(buffer, 0, count); instead of port.ReadLine().

          1 Reply Last reply
          0
          • G goldsoft

            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

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups