i am trying to read the large file from serial port
expected output
112
212
311
411
but i get new line between some data like
output
112
212
/n
311
/n
411
I got some extra new line .how to solve it
private: System::Void ReadDatabtn_Click(System::Object^ sender, System::EventArgs^ e)
{
if (this->serialPort1->IsOpen == true)
{
Thread^ readThread = gcnew Thread(gcnew ThreadStart(this, &main_form::ReadData));
readThread->IsBackground = true;
readThread->Start();
}
}
private: System::Void ReadData()
{
while (true)
{
const int CHUNK_SIZE = 4096; // Read data in 1 MB chunks
cli::array<Byte>^ buffer = gcnew cli::array<Byte>(CHUNK_SIZE);
StringBuilder^ dataBuilder = gcnew StringBuilder();
if (serialPort1->BytesToRead > 0)
{
// Read data from the serial port into the buffer
int bytesRead = serialPort1->Read(buffer, 0, CHUNK_SIZE);
// add the received character to the data string
String^ data = Encoding::UTF8->GetString(buffer, 0, bytesRead);
dataBuilder->Append(data);
if (dataBuilder->Length >= CHUNK_SIZE)
{
String^ chunkData = dataBuilder->ToString(0, CHUNK_SIZE);
dataBuilder->Remove(0, CHUNK_SIZE); // Remove the processed data from the StringBuilder
this->Invoke(gcnew Action<String^>(this, &main_form::UpdateUI), chunkData);
}
String^ remainingData = dataBuilder->ToString();
while (remainingData->Contains("\n\n"))
{
remainingData = remainingData->Replace("\n\n", "\n");
}
this->Invoke(gcnew Action<String^>(this, &main_form::UpdateUI), remainingData);
}
Thread::Sleep(100);
}
}
private: System::Void UpdateUI(String^ data)
{
richTextBox1->AppendText(data);
}