Streigth serial question

Hi all.

I have this code

 if (Serial.available() > 3) {

      v1 = Serial.read();
      v2 = Serial.read();
      v3 = Serial.read();
      v4 = Serial.read();

It reads when it gets 4 numbers.

Insted of sending 4 numbers at the time Im sending 8, 12, 16 etc..

It works fine but I dont know if this is a correct procedure...

Thanks.

Are you really sending a comma between numbers?

What are you trying to do?

Are you sending the value 12 as one byte or two ascii characters ?

You could study Serial Input Basics

No.

I send 4 numbers ,8 or 12 from Max/Msp software

Is a music sequencer for Ableton Live

I send it as ascci

It would be useful to see the rest of your code.

I presume that after this piece of code you process the 4 values, v1-4, then loop around again and get the next 4 if there are 4 available?

There is always more than one way to code something. Bit hard to say of there is a better way to do it without seeing the rest of your code.

You mean digits? Because, that is the only ASCII that will fit in a single byte.

The only big problem I can think of other than that, is synchronization. What if you start receiving in the middle of a transmission? Then all the values will be in the wrong place, and on the last set, it will hang forever waiting because the sender is finished, and the quota of 4 hasn't been met yet.

It's far better to employ start and end markers... at least a start marker. Then the receiver can be sure that a sequence is starting and can capture the data in the correct order and know by counting when it is finished.

A noise spike could also destroy sync by producing a rogue character. Using the input buffer hides all the data until it's read and so prevents you from catching problems like that.

If you send as ascii the text "8, 12, 16" and do

if (Serial.available() > 3) {
      v1 = Serial.read();
      v2 = Serial.read();
      v3 = Serial.read();
      v4 = Serial.read();
…

then
v1 will be the ascii code of ’8’
v2 the ascii code of the comma ',',
v3 the ascii code of the space ' '
v4 the ascii code of '1'

You won’t have read the 4 numbers at all….

@J-M-L I don't think that's what the OP means...

I think they mean that they're reading 4 ASCII characters at a time (as per the above code)... but the sending system is actually sending more characters than 4 at a time. They might send 8, or 12 characters.

Op said

And later said numbers are sent as ascii

We need to know exactly what is sent, if there are commas, if there is a end marker like a end of line (CR/LF) after the 4/8/12 numbers

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.