Click here to Skip to main content
15,905,323 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am using this code to read weight from weighing machine.

C#
_serialPort = new SerialPort("COM2", 9600, Parity.None, 8);
   if (!_serialPort.IsOpen)
  {
     _serialPort.Open();
  }
  _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);

  txtQty.Text  = _serialPort.ReadExisting();
  if (_serialPort.IsOpen)
     {
        _serialPort.Close();
     }




This code working fine when I check with Break point. but not working in exe. Sometime getting this " Access to the port 'COM2' is denied"

How to resolve this ?
Posted
Updated 24-Mar-17 19:50pm
Comments
Sergey Alexandrovich Kryukov 16-Jun-12 1:37am    
What is "working in exe". There is no such thing as "working without exe". Do you mean running under VS IDE debugger vs standalong?
--SA
AmitGajjar 16-Jun-12 2:08am    
SA, i think palraj have problem with release version of this code. as he mention that, "working fine with beakpoint" it is working fine in Visual Studio. i can see only one problem is access problem from Firewall. he need to add 9600 in firewall allow list.
OriginalGriff 16-Jun-12 3:25am    
You do realize that "9600" in the Serialport constructor is the Baud rate, not a port number? And that is has nothing at all to do with firewalls?
AmitGajjar 18-Jun-12 0:13am    
Opps.... sorry...
OriginalGriff 18-Jun-12 3:23am    
:laugh:
Everyone makes mistakes!

1 solution

The chance are that it is working in the debugger, because your breakpoint allows enough time for the serial port to actually receive some information.

Serial ports are physical communications devices - data transfer does not happen instantly. In your case, the transfer happens at a rate of 9600 bits per second, which works out at around 960 characters per second. You cannot just open the port, set a handler, read data and close the handler, because in the real world there may or may not be data available, and if a character arrives at the receiver at the wrong time, the receive event handler will be triggered and may get executed after the port is closed by your code sample. This could cause the problem you are describing.

Use the handler event to read the data.
And if you are going to add a handler every time you open the port, you need to remove the handler every time you close it!
 
Share this answer
 
Comments
SoMad 16-Jun-12 4:22am    
Great answer +5.

Soren Madsen
Sergey Alexandrovich Kryukov 29-Jun-12 22:57pm    
This is a good point, a 5.
--SA
[no name] 8-Jul-15 3:54am    
Agree "works in the debugger" is mucho vague.
I would add you need to remove handler before close() (obvious I know) and read this: https://msdn.microsoft.com/en-au/library/system.io.ports.serialport.close.aspx
"The best practice for any application is to wait for some amount of time after calling the Close method before attempting to call the Open method, as the port may not be closed instantly."
That may cause 'access denied'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900