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. Serial port communication

Serial port communication

Scheduled Pinned Locked Moved C#
questioncsharpcom
11 Posts 3 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.
  • T t_nedelchev

    Hello everybody My project is a C# project on VS2005. My application communicate with external device through Serial Port.I'm using USB to RS convertor whitch add another COM Port in my computer.When I connect with device through this COM Port(transmit and receive data) everythink is OK.When unplug the USB cable from PC and I try to transmit data the program is generate exeption.How can I detect when the COM Port is disappear? Thanks in advance

    M Offline
    M Offline
    Matthew Butler 0
    wrote on last edited by
    #2

    Hi, I beleive you are looking for .IsOpen I've just tried this with a USB-Serial converter and it works. (Goes false on a disconnect). It doesn't cause an event though.

    Matthew Butler

    T 1 Reply Last reply
    0
    • M Matthew Butler 0

      Hi, I beleive you are looking for .IsOpen I've just tried this with a USB-Serial converter and it works. (Goes false on a disconnect). It doesn't cause an event though.

      Matthew Butler

      T Offline
      T Offline
      t_nedelchev
      wrote on last edited by
      #3

      Sorry but it doesn't works. My code is if (sp.IsOpen) { sp.Write(TxBuf, 0, (k + 2 + ofs)); } else { FL_Connected = false; MessageBox.Show("Message"); } When I unplug USB cable (Converter) from my computer the COM Port is still yet open but when the program try to write data to serial port it's generate exception "Access to the Port is denied"

      D 1 Reply Last reply
      0
      • T t_nedelchev

        Sorry but it doesn't works. My code is if (sp.IsOpen) { sp.Write(TxBuf, 0, (k + 2 + ofs)); } else { FL_Connected = false; MessageBox.Show("Message"); } When I unplug USB cable (Converter) from my computer the COM Port is still yet open but when the program try to write data to serial port it's generate exception "Access to the Port is denied"

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #4

        You should always put communication like this in a try/catch block so you can catch exceptions like this. It may be worth starting a timer when you open your port, pausing it when you're about to read/write and stopping it when you close it. That way you can test the port on each Tick event so you always know when it's connected/disconnected. You can also monitor USB devices, get notified when one is inserted/removed and check if it's the one you're intersted in so you can act accordingly. There are some articles on this site if you search.

        Dave

        T 1 Reply Last reply
        0
        • D DaveyM69

          You should always put communication like this in a try/catch block so you can catch exceptions like this. It may be worth starting a timer when you open your port, pausing it when you're about to read/write and stopping it when you close it. That way you can test the port on each Tick event so you always know when it's connected/disconnected. You can also monitor USB devices, get notified when one is inserted/removed and check if it's the one you're intersted in so you can act accordingly. There are some articles on this site if you search.

          Dave

          T Offline
          T Offline
          t_nedelchev
          wrote on last edited by
          #5

          thanks for an answer I try to do this try { sp.Write(TxBuf, 0, (k + 2 + ofs)); } catch { FL_Connected = false; MessageBox.Show("Message"); } And it works.But when I close the program it generate the same exception(Access to the port is denied)

          D 1 Reply Last reply
          0
          • T t_nedelchev

            thanks for an answer I try to do this try { sp.Write(TxBuf, 0, (k + 2 + ofs)); } catch { FL_Connected = false; MessageBox.Show("Message"); } And it works.But when I close the program it generate the same exception(Access to the port is denied)

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #6

            Are you closing the port when you're finished with it? It sounds like you're keeping it open and the program is trying to close the port when it ends - which will generate an exception if it's not available. If you need to keep the port open for the duration, you should close the port manually in the formclosing event - and again, put that inside a try/catch block.

            Dave

            T 1 Reply Last reply
            0
            • D DaveyM69

              Are you closing the port when you're finished with it? It sounds like you're keeping it open and the program is trying to close the port when it ends - which will generate an exception if it's not available. If you need to keep the port open for the duration, you should close the port manually in the formclosing event - and again, put that inside a try/catch block.

              Dave

              T Offline
              T Offline
              t_nedelchev
              wrote on last edited by
              #7

              Thanks DaveyM69 I try to do this protected override void OnClosing(CancelEventArgs e) { try { sp.Close(); } catch { int probe = 33; } } In this case there is no exception when close the program but the program is stoped on the breakpoint in the catch. There is something interesting.When I start the program again the all window is white for a 2 or 3 seconds and then starts normaly.

              D 1 Reply Last reply
              0
              • T t_nedelchev

                Thanks DaveyM69 I try to do this protected override void OnClosing(CancelEventArgs e) { try { sp.Close(); } catch { int probe = 33; } } In this case there is no exception when close the program but the program is stoped on the breakpoint in the catch. There is something interesting.When I start the program again the all window is white for a 2 or 3 seconds and then starts normaly.

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #8

                Hmm... Looks like it's taking a while for it to Dispose. You could try calling Dispose() or Dispose(true) and maybe even do a GC.Collect() to force the clean up. First thing I'd do is see what exceptions if any are occurring

                catch(System.Exception ex)
                {
                System.Windows.Forms.MessageBox.Show(ex.InnerException.Message);
                }

                Dave

                T 1 Reply Last reply
                0
                • D DaveyM69

                  Hmm... Looks like it's taking a while for it to Dispose. You could try calling Dispose() or Dispose(true) and maybe even do a GC.Collect() to force the clean up. First thing I'd do is see what exceptions if any are occurring

                  catch(System.Exception ex)
                  {
                  System.Windows.Forms.MessageBox.Show(ex.InnerException.Message);
                  }

                  Dave

                  T Offline
                  T Offline
                  t_nedelchev
                  wrote on last edited by
                  #9

                  The problem is when I unplug the USB converter from PC and COM Port is disapear any tring of using a method of instance of serial port generate exception i.e. I can't use Sp.Dispose() or SP.Close();

                  D 1 Reply Last reply
                  0
                  • T t_nedelchev

                    The problem is when I unplug the USB converter from PC and COM Port is disapear any tring of using a method of instance of serial port generate exception i.e. I can't use Sp.Dispose() or SP.Close();

                    D Offline
                    D Offline
                    DaveyM69
                    wrote on last edited by
                    #10

                    Maybe check if it's null before doing any operations?

                    public static bool IsPortAvailable(System.IO.Ports.SerialPort portToTest)
                    {
                    return !(null == portToTest);
                    }

                    Dave

                    T 1 Reply Last reply
                    0
                    • D DaveyM69

                      Maybe check if it's null before doing any operations?

                      public static bool IsPortAvailable(System.IO.Ports.SerialPort portToTest)
                      {
                      return !(null == portToTest);
                      }

                      Dave

                      T Offline
                      T Offline
                      t_nedelchev
                      wrote on last edited by
                      #11

                      Thanks DaveyM69 and sorry to loosing your time.In this moment nothing help.Now I will relax and try again later to decide the problem Thanks for all

                      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