Serial Port BytesToRead always 0
-
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); } }
}
-
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); } }
}
-
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.
-
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.
-
It's impossible to guess why your receiver thinks there is no data to read, if it is being transmitted.
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