Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Problem with receiving SerialStream #170

Open
Pantau123 opened this issue Sep 29, 2020 · 5 comments
Open

Problem with receiving SerialStream #170

Pantau123 opened this issue Sep 29, 2020 · 5 comments

Comments

@Pantau123
Copy link

Pantau123 commented Sep 29, 2020

Hi, i use the main branch version of Seriallib with SerialStream.
I can not read characters with << from the SerialStream.
I can only read the function SerialStream.read(char*,streamsize), but everytime i receive a leading '\0' and i have to create a char array with receivedbytes +1.
Can anybody help me?
Bests
Pantau
File main.cpp (sender)

#include <cstdio>
#include <libserial/SerialStream.h>

using namespace LibSerial;
using namespace std;

int main()
{
	SerialStream m_serialPort;
	BaudRate defaultBaudRate = BaudRate::BAUD_9600;
	CharacterSize defaultCharacterSize = CharacterSize::CHAR_SIZE_8;
	FlowControl defaultFlowControl = FlowControl::FLOW_CONTROL_NONE;
	Parity defaultParity = Parity::PARITY_NONE;
	StopBits defaultStopBits = StopBits::STOP_BITS_1;
	int defaultBufferSize = 50;
	m_serialPort.SetBaudRate(defaultBaudRate);
	m_serialPort.SetCharacterSize(defaultCharacterSize);
	m_serialPort.SetFlowControl(defaultFlowControl);
	m_serialPort.SetParity(defaultParity);
	m_serialPort.SetStopBits(defaultStopBits);
	m_serialPort.SetVMin(0);
	m_serialPort.SetVTime(0);
	m_serialPort.FlushIOBuffers();
	m_serialPort.Open("/dev/ttyS1");
	std::string my_string = "Hello, Serial Port.";
	m_serialPort << my_string << endl;
	m_serialPort.DrainWriteBuffer();
	m_serialPort.Close();
    return 0;
}

File main.cpp (receiver)

#include <cstdio>
#include <libserial/SerialStream.h>
#include <sstream>
#include <unistd.h>
#include <iostream>
using namespace LibSerial;
using namespace std;
int main()
{
    SerialStream m_serialPort;
	BaudRate defaultBaudRate = BaudRate::BAUD_9600;
	CharacterSize defaultCharacterSize = CharacterSize::CHAR_SIZE_8;
	FlowControl defaultFlowControl = FlowControl::FLOW_CONTROL_NONE;
	Parity defaultParity = Parity::PARITY_NONE;
	StopBits defaultStopBits = StopBits::STOP_BITS_1;
	int defaultBufferSize = 50;
	m_serialPort.SetBaudRate(defaultBaudRate);
	m_serialPort.SetCharacterSize(defaultCharacterSize);
	m_serialPort.SetFlowControl(defaultFlowControl);
	m_serialPort.SetParity(defaultParity);
	m_serialPort.SetStopBits(defaultStopBits);
	m_serialPort.SetVMin(0);
	m_serialPort.SetVTime(0);

	
	
	auto serialPorts = m_serialPort.GetAvailableSerialPorts();
	m_serialPort.Open("/dev/ttyS2");
	m_serialPort.FlushIOBuffers();
	stringstream rcvBuffer;
	
	bool foundString = false;

	while (foundString!=true)
	{
		bool streamGood = false;
		while (m_serialPort.rdbuf()->in_avail() == 0)
		{
			usleep(50);
		}
		int receivingBytes = m_serialPort.GetNumberOfBytesAvailable();
		int read = m_serialPort.rdbuf()->in_avail();
		char character[read+1];//+1 because have a trailling zero
		m_serialPort.read(character, read+1);
		cout << character << endl;
		
	}
	m_serialPort.Close();
    return 0;
}
@crayzeewulf
Copy link
Owner

Are you expecting null bytes or whitespaces in your input. Maybe have a look at #162.

@Pantau123
Copy link
Author

No i don't expect a null byte but it can be a whitespace.

@Pantau123
Copy link
Author

Pantau123 commented Sep 29, 2020

But the problem is where does the leading '\0' come from?

@Pantau123
Copy link
Author

with noskipws i receive the data with <<

@mcsauder
Copy link
Collaborator

Hi @Pantau123 , did that resolve this issue for you?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants