COM port DataReceived not firing?

On what device, please?

And polling data work?

Prolific USB-to Serial Comm Port(COM3)
it is directly connected to laptop with weighing scale

I tried this solution alsoā€¦ here serial port is openedā€¦ but ReadExisting get empty result.

SerialPort _serialPort = new SerialPort();
_serialPort.PortName = ā€œCOM3ā€;
_serialPort.BaudRate = 9600;
_serialPort.Parity = Parity.None;
_serialPort.DataBits = 8;
_serialPort.StopBits = StopBits.One;

            //<-- This block ensures that no exceptions happen
            if (_serialPort != null && _serialPort.IsOpen)
                _serialPort.Close();
            if (_serialPort != null)
                _serialPort.Dispose();
            //<-- End of Block


            if (!_serialPort.IsOpen)
            {
                _serialPort.Open();// opened
            }

            string message = _serialPort.ReadExisting(); // empty value in message 
            txt_Netwt.Text = message;

            _serialPort.Close();

            _serialPort.Dispose();

Dat explicitly meant what Fez device are you talking about :slight_smile: But now you come and tell us youā€™re reading from a scale, that is connected to a PC? If thatā€™s your problem, this post is about the .Net Microframework, not any PC related programming, so you may have made your way here by accident.

Sirā€¦ Actually i want to read weight from weighing scale to my software.
here i am working in dot net frameworkā€¦ weighing scale is connected to pc. I need to fetch data from that scale.
My issue is DataReceived Event not firing
This is my code:

                _serialPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);   
                _serialPort.DataReceived += new SerialDataReceivedEventHandler(SerialPortOnDataReceived);
                        _serialPort.Open();

private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
    {
        if (InvokeRequired)     //<-- Makes sure the function is invoked to work properly in the UI-Thread
            BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender, serialDataReceivedEventArgs); }));     
        else
        {
           
            int dataLength = _serialPort.BytesToRead;
            byte[] data = new byte[dataLength];
            int nbrDataRead = _serialPort.Read(data, 0, dataLength);
            if (nbrDataRead == 0)
                return;
            string str = System.Text.Encoding.UTF8.GetString(data);
            txt_Netwt.Text = str.ToString();

           
        }
    }

Sirā€¦Please help me to find why event is not firing
Thanks in advance for supporting me.

So many reasons.

  • Is the setting (baudrate, party, stopbitā€¦) correct?
  • Is comport correct?
  • Connection correct?
  • ā€¦

You need to take a look on weighing scaleā€™ manual or their datasheet for those information to see how it transfers data to PC.

Iā€™ll make my comment again - which you IGNORED.

This forum post is about the .Net MICROframework. This forum is NOT about the .Net Framework. They are different.

What GHI Fez Device are you wanting to interface your scale to?

If you have a general serial port programming question with the .Net Framework, then you might get a helpful response here but more likely, places like stackexchange will be a better place for you to have your question seen by more relevant people.

But as Dat says, the event wonā€™t fire if your scale doesnā€™t actually transmit data. Many devices like this use a request-and-respond model rather than just pumping out data, so you have to send a command to the device which it then responds with the appropriate data. Nobody here knows your scale, nobody here is likely to have the same scale, so we are all shooting in the dark. A very simple approach to debugging this that you can take is to use a serial program (teraterm, putty etc) and connect to the serial port that you have selected in your code with the same settings. If you get ascii characters just spilling to screen, then your serial port works and your code is broken (and we arenā€™t going to help with PC code, as we are not a .Net Framework forum). But more likely you arenā€™t getting any data in the terminal program, so you need to go back to basics and figure out what the scale requires to communicate

4 Likes

What about the DTE versus DCE wiring question?