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. Ports.SerialPort freezes

Ports.SerialPort freezes

Scheduled Pinned Locked Moved C#
csharpquestion
3 Posts 2 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.
  • S Offline
    S Offline
    sjembek
    wrote on last edited by
    #1

    Hi I'm using the SerialPort class (.net 2.0) for rs232 communications. Unfortunately, an it freezes up my program when I try to set a parameter for it after it's been opened. First I initialize the port:

    SerialPort port = new SerialPort(comname, 19200);
    port.Open();
    port.ReceivedBytesThreshold = 63;
    port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

    Then I do some communications stuff. So far so good. The freeze occurs when I try to use the port for some other process.

    if (port.IsOpen)
    {
    port.DiscardInBuffer(); // will freeze here
    port.DiscardOutBuffer(); // here
    port.Close(); // here
    }

    port.ReceivedBytesThreshold = 1; // or here
    port.Open();

    Any of the commented line freeze up my program as if in a blocking state, and the code doesn't throw an exception of any kind, it just freezes each and any thread currently running. Does anyone have any clues what's happening here?

    S 1 Reply Last reply
    0
    • S sjembek

      Hi I'm using the SerialPort class (.net 2.0) for rs232 communications. Unfortunately, an it freezes up my program when I try to set a parameter for it after it's been opened. First I initialize the port:

      SerialPort port = new SerialPort(comname, 19200);
      port.Open();
      port.ReceivedBytesThreshold = 63;
      port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

      Then I do some communications stuff. So far so good. The freeze occurs when I try to use the port for some other process.

      if (port.IsOpen)
      {
      port.DiscardInBuffer(); // will freeze here
      port.DiscardOutBuffer(); // here
      port.Close(); // here
      }

      port.ReceivedBytesThreshold = 1; // or here
      port.Open();

      Any of the commented line freeze up my program as if in a blocking state, and the code doesn't throw an exception of any kind, it just freezes each and any thread currently running. Does anyone have any clues what's happening here?

      S Offline
      S Offline
      sjembek
      wrote on last edited by
      #2

      Ok, I already found the problem, for some strange reason the port needs to be reconstructed:

      port = new SerialPort(port.PortName, port.Baudrate);

      Weird...

      D 1 Reply Last reply
      0
      • S sjembek

        Ok, I already found the problem, for some strange reason the port needs to be reconstructed:

        port = new SerialPort(port.PortName, port.Baudrate);

        Weird...

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Not Wierd. You'll actually find that this happens throughout the Framework. Try looking at the Font class for another example of immutable, or partially immutable, objects.

        Dave Kreskowiak Microsoft MVP - Visual Basic

        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