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