Trying to receive a message via Softwareserial

Hello,

I developed a small application to communicate via the Softwareserial on a Arduino pro mini with another hardware device (sensor). The sensor was not develeoped by me.

The protocol is as follows every 1000ms I send a request and the sensor answears in a 175 byte message ~5ms after the request was sent.

Sending the request works completely fine and I also get the anwear from the sensor. I checked it with a logic analyzer and everything is fine there. My request is sent and ~5ms later the sensor answears with the 175Byte message.

But if I want to receive the message it only works for the first 1~3 messages for some reason and then it seems like the buffer overlaps. Like for the first messages the complete message is in my 175byte array. but then it seems like half of the previous message is in the first half of the buffer and the start of the next packet is in the rest of the half. So basically every message is split in half. But on the bus everything is sent correctly.

Now my code for receiving looks as follows:

static void ReceivetMessage()
{
	if (softSerial.available() > 0) {
		memset(&buffer, 0x00, sizeof(buffer));
                Serial.print("\nReceive: ");
		for (int i = 0; i < PACKET_SIZE; i++)
		{
			while (!softSerial.available()) {}
			uint8_t c = softSerial.read();

                        Serial.print((char)c);
			buffer[i] = c;
		}

		memcpy(&Packet, buffer, sizeof(buffer));
	}
}

Can anyone help me or tell how to receive correctly via the Softserial interface? Or what am I doing wrong?

Thanks.

Or what am I doing wrong?

Posting only part of your code.

For all we know, it takes forever to get back to that function, and the device has sent three or four more packets in the meantime, resulting in buffer overflow.

If this was the case I would have figured it out already.

But sure here is the complete code:

void loop() {

if (timer > INTERVAL)
timer = 0;
PollSensor(SENSOR_ID);
ReceivetMessage();
}
}

And yes it is definately a buffer overflow of some sorts. But the device is not sending faster than i requesting or whatever. If I set the buffersize for the softserial to e.g. 200. It works fine. But this can’t be the solution…

xorle, PaulS was to the point and helpful. There is even a website for you : http://snippets-r-us.com/

Now I have to guess about the rest of your code to make the sketch complete. Your "char buffer [ 40 ]" (guessing) is an array that means that 'buffer' is already a pointer and you don't need &buffer. Your Packet is a struct (guessing) and there you do indeed need the &Packet. Why fill the buffer with zeros ? You should not rely on a zero to identify the end of the message (guessing). You forgot to add comment when declaring the Packet and buffer that the Packet size must be equeal or larger than the buffer (guessing). To copy the length of the buffer into the Packet instead of the length of the message is not nice and very dangerous (not guessing). Why do you use that editor, instead of the Arduino IDE (guessing) ? Your program flow with the timing is not okay (guessing). The timing of the main sketch does not match with the serial incoming data (guessing). For example, data could still be received when calling the ReceivetMessage(), and you wait forever for data. A single glitch and the communication is out of sync. Your usage of brackets '{' and '}' is not consistant. That means that you are not a teacher and your desk is a mess (guessing). The way you write code, I think your age is between 40 and 55 (guessing) and you are male (guessing), and you have done less than 10 projects with Arduino (guessing).

Maybe I got a "little" carried away ;) , but I think you understand by now that this is very tiresome for us.

Have a look the the examples in Serial Input Basics.

As written the receive array is 32 bytes long but you can easily extend it.

...R

Koepel: Your "char buffer [ 40 ]" (guessing) is an array that means that 'buffer' is already a pointer and you don't need &buffer.

Wrong guess as I have written earlier the messages I receive are always 175 bytes why should the buffer be 40 bytes then?

Koepel: Your Packet is a struct (guessing) and there you do indeed need the &Packet. Why fill the buffer with zeros ? You should not rely on a zero to identify the end of the message (guessing). You forgot to add comment when declaring the Packet and buffer that the Packet size must be equeal or larger than the buffer (guessing).

I am not relying on a zero to identify the end of the message. I rely on the bytes received to identify the end of the message. Again the packets are always 175bytes long. No a comment is not missing. And the packet size actually must be the packet size. Therefore the buffer equals the packet size.

Koepel: To copy the length of the buffer into the Packet instead of the length of the message is not nice and very dangerous (not guessing).

Well still kinda wrong. As the buffer is having the whole message in it.

Koepel: Why do you use that editor, instead of the Arduino IDE (guessing) ?

I am using which editor? Actually I am using Visual Studio cause the Arduino IDE is well... But not sure what that has to do with anything?

Koepel: The timing of the main sketch does not match with the serial incoming data (guessing). For example, data could still be received when calling the ReceivetMessage(), and you wait forever for data. A single glitch and the communication is out of sync.

Actually it can not cause as said we are trying to receive the complete packet. And even more we have that stupid and ugly while (!softSerial.available()) {} to wait for incoming bytes. So the communication can catch up. Because if I am not doing that the first of of the message is fine and the rest of it is just a mess. And yes there is missing a timeout and some nice error handling stuff. But that won't fix the initial problem.

Koepel: Your usage of brackets '{' and '}' is not consistant. That means that you are not a teacher and your desk is a mess (guessing). The way you write code, I think your age is between 40 and 55 (guessing) and you are male (guessing), and you have done less than 10 projects with Arduino (guessing).

Ok, and what has that to do with anything? Btw, you are actually wrong again. Who would have guessed. If it is tiresome for you, just don't reply?

Robin2: Have a look the the examples in Serial Input Basics.

As written the receive array is 32 bytes long but you can easily extend it.

...R

Thank you.

Sorry for being wrong so many times. Perhaps my crystal ball is a counterfeit. At least it was right that you are not using the Arduino IDE editor. If you want serious answers, try showing the complete sketch, and more explanation about the data packet. Did you read the sentence that the 'buffer' is already a pointer ?

Maybe don't rely on any crystal ball then (guessing). Don't worry I read all your sentences, of which noone actually helped on the topic and that's why this is my last reply to you specifically.

If someone is rude or inappropriate, you can report it to the moderators.

But sure here is the complete code: void loop() {

if (timer > INTERVAL) timer = 0; PollSensor(SENSOR_ID); ReceivetMessage(); } }

Nope, that is not the complete code.

Maybe don't rely on any crystal ball then (guessing).

Since you won't post all of your code, we can do nothing but guess. Or ignore you. Which is what I'm going to do as soon as I hit Post. Unless you post ALL of your code.