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. Managed C++/CLI
  4. Serial Port BytesToRead always 0

Serial Port BytesToRead always 0

Scheduled Pinned Locked Moved Managed C++/CLI
graphicsjsonhelpquestionworkspace
5 Posts 3 Posters 16 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.
  • Y Offline
    Y Offline
    yehiga1467
    wrote on last edited by
    #1

    Hello everyone, I made a class that will do some additional configuration to the serial port. (Mainly to handle received data). My issue is that SerialPort_DataReceived() is successfully triggered when it received data from device, however the BytesToRead is always 0. What could the reason be? I am sure the device is sending data because when I haven't use this ConfigSerialPort, everything works fine. ConfigSerialPort.h

    #pragma once
    using namespace System::Windows::Forms;

    ref class ConfigSerialPort: public System::Windows::Forms::Control
    {

    private: System::IO::Ports::SerialPort^ \_serialPort;
    
    public:
    	ConfigSerialPort(System::IO::Ports::SerialPort^ serialPort) {
    		\_serialPort = serialPort;
    		
        //handle received data
    		\_serialPort->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &ConfigSerialPort::SerialPort\_DataReceived);
    	
    	}
    	
    private: 	System::Void SerialPort\_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e)
    {
        unsigned int test = 0;
    
        test = \_serialPort->BytesToRead; **// BytesToRead is ALWAYS Zero**
    }
    

    }

    MyForm.h

    #pragma once

    #include #include #include #include #include "ConfigSerialPort.h"

    namespace SerialPortGUI {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    
    using namespace System::IO;
    using namespace System::Runtime::Serialization::Formatters::Binary;
    
    
    
    
    
    
    /// /// Summary for MyForm
    /// 
    public ref class MyForm : public System::Windows::Forms::Form
    {
    	public:
    		MyForm(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: Add the constructor code here
    			//
    		}
    		.
    		.
    		.
    		
    		
    		void InitializeComponent(void)
    		{
    			.
    			.
    			.
    			this->Load += gcnew System::EventHandler(this, &MyForm::MyForm\_Load);
    		   this->MyForm\_SerialPort = (gcnew System::IO::Ports::SerialPort(this->components));
    		   
    			.
    			.
    			.
    
    		}
    	private: ConfigSerialPort^ test;
    	private: System::Void MyForm\_Load(System::Object^ sender, System::EventArgs^ e) {
    	
    	// initialize Serial port (already done via the designer)
    	test = gcnew ConfigSerialPort(MyForm\_SerialPort);
    	
    	}
    }
    

    }

    L 1 Reply Last reply
    0
    • Y yehiga1467

      Hello everyone, I made a class that will do some additional configuration to the serial port. (Mainly to handle received data). My issue is that SerialPort_DataReceived() is successfully triggered when it received data from device, however the BytesToRead is always 0. What could the reason be? I am sure the device is sending data because when I haven't use this ConfigSerialPort, everything works fine. ConfigSerialPort.h

      #pragma once
      using namespace System::Windows::Forms;

      ref class ConfigSerialPort: public System::Windows::Forms::Control
      {

      private: System::IO::Ports::SerialPort^ \_serialPort;
      
      public:
      	ConfigSerialPort(System::IO::Ports::SerialPort^ serialPort) {
      		\_serialPort = serialPort;
      		
          //handle received data
      		\_serialPort->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &ConfigSerialPort::SerialPort\_DataReceived);
      	
      	}
      	
      private: 	System::Void SerialPort\_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e)
      {
          unsigned int test = 0;
      
          test = \_serialPort->BytesToRead; **// BytesToRead is ALWAYS Zero**
      }
      

      }

      MyForm.h

      #pragma once

      #include #include #include #include #include "ConfigSerialPort.h"

      namespace SerialPortGUI {

      using namespace System;
      using namespace System::ComponentModel;
      using namespace System::Collections;
      using namespace System::Windows::Forms;
      using namespace System::Data;
      using namespace System::Drawing;
      
      using namespace System::IO;
      using namespace System::Runtime::Serialization::Formatters::Binary;
      
      
      
      
      
      
      /// /// Summary for MyForm
      /// 
      public ref class MyForm : public System::Windows::Forms::Form
      {
      	public:
      		MyForm(void)
      		{
      			InitializeComponent();
      			//
      			//TODO: Add the constructor code here
      			//
      		}
      		.
      		.
      		.
      		
      		
      		void InitializeComponent(void)
      		{
      			.
      			.
      			.
      			this->Load += gcnew System::EventHandler(this, &MyForm::MyForm\_Load);
      		   this->MyForm\_SerialPort = (gcnew System::IO::Ports::SerialPort(this->components));
      		   
      			.
      			.
      			.
      
      		}
      	private: ConfigSerialPort^ test;
      	private: System::Void MyForm\_Load(System::Object^ sender, System::EventArgs^ e) {
      	
      	// initialize Serial port (already done via the designer)
      	test = gcnew ConfigSerialPort(MyForm\_SerialPort);
      	
      	}
      }
      

      }

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

      What is the serial port connected to, and how can you tell there is data to be read?

      C 1 Reply Last reply
      0
      • L Lost User

        What is the serial port connected to, and how can you tell there is data to be read?

        C Offline
        C Offline
        cibec75455 frnla com
        wrote on last edited by
        #3

        Hello, my other device is an Arduino that connects to PC as Com port 8. Every time I press a button on Arduino, it will transmit a text over serial to the PC. I have verified the received text using Termite software.

        L 1 Reply Last reply
        0
        • C cibec75455 frnla com

          Hello, my other device is an Arduino that connects to PC as Com port 8. Every time I press a button on Arduino, it will transmit a text over serial to the PC. I have verified the received text using Termite software.

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

          It's impossible to guess why your receiver thinks there is no data to read, if it is being transmitted.

          C 1 Reply Last reply
          0
          • L Lost User

            It's impossible to guess why your receiver thinks there is no data to read, if it is being transmitted.

            C Offline
            C Offline
            cibec75455 frnla com
            wrote on last edited by
            #5

            The only think I could think of is inside

            ConfigSerialPort.h

            I should use gcnew for _serialPort

            _serialPort = gcnew (System::IO::Ports::SerialPort);

            , and then assign it the parameter

            _serialPort = serialPort

            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