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. Com port c#, windows form

Com port c#, windows form

Scheduled Pinned Locked Moved C#
helpcsharpwinformscom
7 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.
  • J Offline
    J Offline
    Jan 1947
    wrote on last edited by
    #1

    Hi Guys,
    I am not expert and will be glad to get some help. I wrote software, where I am using Barcode reader, com port, in number of Windows forms. I am switching receive data on Form Activation and disabling it on Form deactivated. it has worked well for number of months, but now I am occasionally get error, Com port is not accessible. Received I use Invoke.

        private void CheckForUsbDevice()
        {
    
            string\[\] ports = SerialPort.GetPortNames();
    
            // Display each port name to the console.
            foreach (string port in ports)
            {
                if ((port == "COM3") || (port == "COM4"))
                {
                    if (AcdStart.mySerialPort == null)
                    {
                        AcdStart.mySerialPort = new SerialPort(port)
                        {
                            BaudRate = 9600,
                            Parity = Parity.None,
                            StopBits = StopBits.One,
                            DataBits = 8,
                            Handshake = Handshake.None,
                            RtsEnable = true
                        };
                    }
                         AcdStart.mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                    if (!AcdStart.mySerialPort.IsOpen)
                    {
                        AcdStart.mySerialPort.Close();
                        AcdStart.mySerialPort.Open();
                    }
                        PortTextBox.Text = (string.Format("Port {0}", port));
                    break;
                }
            }
        }
    
        private void RxPurchaseOrder\_Deactivate(object sender, System.EventArgs e)
        {
            if ((AcdStart.mySerialPort != null) && (AcdStart.mySerialPort.IsOpen))
                AcdStart.mySerialPort.DiscardInBuffer();
    
            if (AcdStart.mySerialPort != null)
            {
                AcdStart.mySerialPort.DataReceived -= new SerialDataReceivedEventHandler(DataReceivedHandler);
                AcdStart.mySerialPort.Close();
            }
            AcdStart.mySerialPort = null;
            PauseNow = true;
            this.BackColor = Color.LightSalmon;
        }
    
        private void RxPurchaseOrder\_Activated(object sender, EventArgs e)
        {
            if ((AcdStart.mySerialPort != null) && (AcdStart.mySerialPort.IsOpen))
            {
                AcdStart.mySerialPort.DiscardInBu
    
    L 2 Replies Last reply
    0
    • J Jan 1947

      Hi Guys,
      I am not expert and will be glad to get some help. I wrote software, where I am using Barcode reader, com port, in number of Windows forms. I am switching receive data on Form Activation and disabling it on Form deactivated. it has worked well for number of months, but now I am occasionally get error, Com port is not accessible. Received I use Invoke.

          private void CheckForUsbDevice()
          {
      
              string\[\] ports = SerialPort.GetPortNames();
      
              // Display each port name to the console.
              foreach (string port in ports)
              {
                  if ((port == "COM3") || (port == "COM4"))
                  {
                      if (AcdStart.mySerialPort == null)
                      {
                          AcdStart.mySerialPort = new SerialPort(port)
                          {
                              BaudRate = 9600,
                              Parity = Parity.None,
                              StopBits = StopBits.One,
                              DataBits = 8,
                              Handshake = Handshake.None,
                              RtsEnable = true
                          };
                      }
                           AcdStart.mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                      if (!AcdStart.mySerialPort.IsOpen)
                      {
                          AcdStart.mySerialPort.Close();
                          AcdStart.mySerialPort.Open();
                      }
                          PortTextBox.Text = (string.Format("Port {0}", port));
                      break;
                  }
              }
          }
      
          private void RxPurchaseOrder\_Deactivate(object sender, System.EventArgs e)
          {
              if ((AcdStart.mySerialPort != null) && (AcdStart.mySerialPort.IsOpen))
                  AcdStart.mySerialPort.DiscardInBuffer();
      
              if (AcdStart.mySerialPort != null)
              {
                  AcdStart.mySerialPort.DataReceived -= new SerialDataReceivedEventHandler(DataReceivedHandler);
                  AcdStart.mySerialPort.Close();
              }
              AcdStart.mySerialPort = null;
              PauseNow = true;
              this.BackColor = Color.LightSalmon;
          }
      
          private void RxPurchaseOrder\_Activated(object sender, EventArgs e)
          {
              if ((AcdStart.mySerialPort != null) && (AcdStart.mySerialPort.IsOpen))
              {
                  AcdStart.mySerialPort.DiscardInBu
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      What is the exact error message, and where is it generated?

      J 1 Reply Last reply
      0
      • J Jan 1947

        Hi Guys,
        I am not expert and will be glad to get some help. I wrote software, where I am using Barcode reader, com port, in number of Windows forms. I am switching receive data on Form Activation and disabling it on Form deactivated. it has worked well for number of months, but now I am occasionally get error, Com port is not accessible. Received I use Invoke.

            private void CheckForUsbDevice()
            {
        
                string\[\] ports = SerialPort.GetPortNames();
        
                // Display each port name to the console.
                foreach (string port in ports)
                {
                    if ((port == "COM3") || (port == "COM4"))
                    {
                        if (AcdStart.mySerialPort == null)
                        {
                            AcdStart.mySerialPort = new SerialPort(port)
                            {
                                BaudRate = 9600,
                                Parity = Parity.None,
                                StopBits = StopBits.One,
                                DataBits = 8,
                                Handshake = Handshake.None,
                                RtsEnable = true
                            };
                        }
                             AcdStart.mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                        if (!AcdStart.mySerialPort.IsOpen)
                        {
                            AcdStart.mySerialPort.Close();
                            AcdStart.mySerialPort.Open();
                        }
                            PortTextBox.Text = (string.Format("Port {0}", port));
                        break;
                    }
                }
            }
        
            private void RxPurchaseOrder\_Deactivate(object sender, System.EventArgs e)
            {
                if ((AcdStart.mySerialPort != null) && (AcdStart.mySerialPort.IsOpen))
                    AcdStart.mySerialPort.DiscardInBuffer();
        
                if (AcdStart.mySerialPort != null)
                {
                    AcdStart.mySerialPort.DataReceived -= new SerialDataReceivedEventHandler(DataReceivedHandler);
                    AcdStart.mySerialPort.Close();
                }
                AcdStart.mySerialPort = null;
                PauseNow = true;
                this.BackColor = Color.LightSalmon;
            }
        
            private void RxPurchaseOrder\_Activated(object sender, EventArgs e)
            {
                if ((AcdStart.mySerialPort != null) && (AcdStart.mySerialPort.IsOpen))
                {
                    AcdStart.mySerialPort.DiscardInBu
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You have a "number of forms", and imply that each form is opening and closing the port on activation / deactivation. I maintain the serial port should not be dependent on "forms"; and should be part of the app's start and stop process (and "interval" / wait processing). The port should be "wrapped" in such a way that it can be shared by the various forms / processes.

        "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

        J 1 Reply Last reply
        0
        • L Lost User

          You have a "number of forms", and imply that each form is opening and closing the port on activation / deactivation. I maintain the serial port should not be dependent on "forms"; and should be part of the app's start and stop process (and "interval" / wait processing). The port should be "wrapped" in such a way that it can be shared by the various forms / processes.

          "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

          J Offline
          J Offline
          Jan 1947
          wrote on last edited by
          #4

          Hi Garry thanks for your reply. As I was unable to find some example, this is how I have created. I feel it is not good way, as I done it, as there is a lot of areas which is unpredictable. I will try to see. what I can do. If you have some link to give me some link to example, will be very much appreciated. I fully agree with Napoleon and like the internet and Code Project, to learn. thanks for great help. Regards John

          L 1 Reply Last reply
          0
          • L Lost User

            What is the exact error message, and where is it generated?

            J Offline
            J Offline
            Jan 1947
            wrote on last edited by
            #5

            Hi Richard The error is, "Access to com port denied". as it is very hard to debug it but it happens on Open port call. I have done some changes, leave com port assigned and only remove or add SerialDataRceivedEventHandler. I feel it is still not the good way, but is working. regards John

            1 Reply Last reply
            0
            • J Jan 1947

              Hi Garry thanks for your reply. As I was unable to find some example, this is how I have created. I feel it is not good way, as I done it, as there is a lot of areas which is unpredictable. I will try to see. what I can do. If you have some link to give me some link to example, will be very much appreciated. I fully agree with Napoleon and like the internet and Code Project, to learn. thanks for great help. Regards John

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

              One picks what's most comfortable after looking around. [https://stackoverflow.com/questions/2799856/c-sharp-serial-port-driver-wrapper-class-code-and-concept-quality\](https://stackoverflow.com/questions/2799856/c-sharp-serial-port-driver-wrapper-class-code-and-concept-quality)

              "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

              J 1 Reply Last reply
              0
              • L Lost User

                One picks what's most comfortable after looking around. [https://stackoverflow.com/questions/2799856/c-sharp-serial-port-driver-wrapper-class-code-and-concept-quality\](https://stackoverflow.com/questions/2799856/c-sharp-serial-port-driver-wrapper-class-code-and-concept-quality)

                "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                J Offline
                J Offline
                Jan 1947
                wrote on last edited by
                #7

                thank you Gerry regards John

                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