Forums - C# Corner

Forum guidelines
Suraj Binorkar

Suraj Binorkar

  • 1.4k
  • 49
  • 2k

Serial Port Communication problem

Aug 6 2016 5:16 AM
Hi,<br />I am facing issue with the serial port communication issue, actually it was working till yesterday but now its giving me error, you can see the code in below. Actually the error is generating during calling method.<br /><br /><div id="pastingspan1">this.serialPort1 = new SerialPort("port1", 9600, Parity.None, 8, StopBits.One);</div><div id="pastingspan1"> if (!this.serialPort1.IsOpen)</div><div id="pastingspan1"> {</div><div id="pastingspan1"> this.serialPort1.Open();</div><div id="pastingspan1"> this.serialPort1.DataReceived += new <strong>SerialDataReceivedEventHandler(this.scannerSerialPort_DataReceived);</strong></div><div id="pastingspan1"> }</div><div>&nbsp;</div><div>The method you can see in below :-<br /><br /><div id="pastingspan1">public void scannerSerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)</div><div id="pastingspan1"> {</div><div id="pastingspan1"> frmRegistrationStickerPrinting _frmRegistrationStickerPrinting = this;</div><div id="pastingspan1"> _frmRegistrationStickerPrinting.scan = string.Concat(_frmRegistrationStickerPrinting.scan, this.serialPort1.ReadExisting());</div><div id="pastingspan1"> if (this.scan.EndsWith("\r\n"))</div><div id="pastingspan1"> {</div><div id="pastingspan1"> this.scan = this.scan.Substring(0, this.scan.Length - 2);</div><div id="pastingspan1"> Control activeControl = base.ActiveControl;</div><div id="pastingspan1"> if (activeControl.InvokeRequired)</div><div id="pastingspan1"> {</div><div id="pastingspan1"> activeControl.Invoke(new MethodInvoker(() =&gt; activeControl.Text = this.scan));</div><div id="pastingspan1"> }</div><div id="pastingspan1"> Control nextControl = base.GetNextControl(activeControl, true);</div><div id="pastingspan1"> if (nextControl.InvokeRequired)</div><div id="pastingspan1"> {</div><div id="pastingspan1"> nextControl.Invoke(new MethodInvoker(() =&gt; nextControl.Focus()));</div><div id="pastingspan1"> }</div><div id="pastingspan1"> this.scan = "";</div><div id="pastingspan1"> }</div><div id="pastingspan1"> }</div></div><br /><br /><br />

Answers (2)