Sreail.read(); and Serial.write();

Is it possibe to use Serial.read(); function and Serial.write(); in loop ?

I just tried this simple code below. The Serial.write(); is working perfectly. But the serial .read(); is not working. Can I know, why it is not working in my case?

void setup() {
  Serial.begin(115200);
  while(!Serial);

}

void loop() {
  Serial.println("start");
  int w = Serial1.availableForWrite(); // storing the number of bytes available for write.
  int e = Serial1.available(); // storing the number of bytes available for read.
  int r; // created this variable to store the value from Serial1.read(); function.
  Serial.println(w); // Printing the current value of w in a serial monitor.  
                     //The first expected value is 63 and 62 and so on.
  Serial.println(e); // Printing the current value of e in a serial monitor. 
                     //The first expected value is 0 and 1 and so on.
  Serial.println(r); // Printing the current value of r in a serial monitor. 
                     //The first expected value is 0 then the value read by the Serial1.read(); funtion.
  Serial1.write(rand()); // this will write the random integer to the serial 1 buffer.
  w = Serial1.availableForWrite(); // Here this will again check and store the number of bytes available for write.
  e = Serial1.available(); // Here this will again check and store the number of bytes available for read.
  r = Serial1.read(); // Here this will read the single byte which written by Serial1.write(); function and store it in.
  Serial.println(w); // Now, the expected output is 62 and 61 and so on. // here this works as expected.
  Serial.println(e); // Now, the expected output is 0 and 1 and so on. 
                     // here this does not works as expected. The output is always 0.
  Serial.println(r); // Now, the expected output is to show the value generated by rand(); function
                     // and written by Serial1.write(); funtion
                     // and which has to be read by serial1.read(); function in r variable.
                     // here this does not works as expected. The output is always -1.
  Serial.println("end");
  delay(500);
}

The output is showing like this. below.

start
63
0
0
62
0
-1
end
start
62
0
0
61
0
-1
end
start
61
0
0
60
0
-1
end
start
60
0
0
59
0
-1
end
start
59
0
0
58
0
-1
end
start
58
0
0
57
0
-1
end
start
57
0
0
56
0
-1
end
start
56
0
0
55
0
-1
end
start
55
0
0
54
0
-1
end
start
54
0
0
53
0
-1
end
start
53
0
0
52
0
-1
end
start
52
0
0
51
0
-1
end
start
51
0
0
50
0
-1
end
start
50
0
0
49
0
-1
end
start
49
0
0
48
0
-1
end
start
48
0
0
47
0
-1
end
start
47
0
0
46
0
-1
end
start
46
0
0
45
0
-1
end
start
45
0
0
44
0
-1
end
start
44
0
0
43
0
-1
end
start
43
0
0
42
0
-1
end
start
42
0
0
41
0
-1
end
start
41
0
0
40
0
-1
end
start
40
0
0
39
0
-1
end
start
39
0
0
38
0
-1
end
start
38
0
0
37
0
-1
end
start
37
0
0
36
0
-1
end
start
36
0
0
35
0
-1
end
start
35
0
0
34
0
-1
end
start
34
0
0
33
0
-1
end
start
33
0
0
32
0
-1
end
start
32
0
0
31
0
-1
end
start
31
0
0
30
0
-1
end
start
30
0
0
29
0
-1
end
start
29
0
0
28
0
-1
end
start
28
0
0
27
0
-1
end
start
27
0
0
26
0
-1
end
start
26
0
0
25
0
-1
end
start
25
0
0
24
0
-1
end
start
24
0
0
23
0
-1
end
start
23
0
0
22
0
-1
end
start
22
0
0
21
0
-1
end
start
21
0
0
20
0
-1
end
start
20
0
0
19
0
-1
end
start
19
0
0
18
0
-1
end
start
18
0
0
17
0
-1
end
start
17
0
0
16
0
-1
end
start
16
0
0
15
0
-1
end
start
15
0
0
14
0
-1
end
start
14
0
0
13
0
-1
end
start
13
0
0
12
0
-1
end
start
12
0
0
11
0
-1
end
start
11
0
0
10
0
-1
end
start
10
0
0
9
0
-1
end
start
9
0
0
8
0
-1
end
start
8
0
0
7
0
-1
end
start
7
0
0
6
0
-1
end
start
6
0
0
5
0
-1
end
start
5
0
0
4
0
-1
end
start
4
0
0
3
0
-1
end
start
3
0
0
2
0
-1
end
start
2
0
0
1
0
-1
end
start
1
0
0
0
0
-1
end
start
0
0
0

Can I How can I achieve what I am trying to do?

You do use Serial.available to get the number of bytes available in the receive buffer but you do not check to make sure that the number of bytes is more than 0 before you read. Serial.read will return -1 for an empty serial receive buffer. You need to make sure that there is something to read before you read,

if(Serial.available > 0)
{
     r = Serial.read();
}

or

e = Serial1.available();
if(e > 0)
{
    r = Serial1.read(); 
}

Just an FYI. Using single letter variable names is bad practice. Variable names should describe the variable's use. And if you need to debug, doing a search for e is going to make finding that particular variable a real pain in the patootie.

1 Like

Could you please explain, what you are really trying to do?
Which Board do you use?

Still, the condition is always going to be false. because the serial available always gives 0 bytes available for reading. Anyway, this does not help. I would like to read the data that I have written on the Serial1.write(); function using Serial1.read(); function.

I am using the STM32F103C8T6 board with the STM32 official Arduino core in it. Which works similar to Arduino.

I would like to read the data that I have written on the Serial1.write(); function using Serial1.read(); function.

Serial1 works the same.

Good luck.

Maybe I might be lacking in some basic knowledge could you please explain in detail?

First of all, I hope I made my question very clear!

Connect the TX pin to the RX pin.

You don't have a Serial1.begin(). So Serial1 will never work.

Really? I could not compile your sketch with this core. Serial1 was not declared by default with this core. I had to use the core of Roger Clark to get in compiled unchanged.
And as @sterretje already mentioned, the Serial1.begin() is missing.

And some of your assumptions are false:

  Serial.println(r); // Printing the current value of r in a serial monitor. 
                     //The first expected value is 0 then the value read by the Serial1.read(); funtion.

Local variables are not initialized. So your 'r' variable can have an arbitrary value. It may be 0 by chance.

If your sketch works correctly ( with the 'begin' ) than this is always '63', because there is never more than 1 byte in the transmit buffer ( because of your delay(500); it doesn't fill up )

Yes, I used the official Stm32 core. That worked and it was compiling. Even I have posted my entire output which was printed on Serial monitor.

Yes, you are right, this is missing. Even I am noticing that. I will try this tomorrow.

Makes a valid point let me try this as well. But still, I have one more question is possible to read the from Rx while we transfer through Tx? Because I tested this simple program to transfer data between 4 MCUs. So, It must be possible to read and write data simultaneously!

It does, you can see my output it fills up after a certain point.

I hope that what I have written in the comment section in code on my question of this post is understandable.

That's very suprising for me. This is from the Wiki of the STM-core:

By default, only one Serialx instance is available mapped to the generic Serial name.

To use a second serial port, a HardwareSerial object should be declared in the sketch before the setup() function:

That means there is no Serial1 in the official core unless you explicitly declare it. And this is what I noticed when I tried to compile your sketch.

Of course this is possible.

I think it's because of the missing begin(). Your data will not be transfered because of that, and that's why your buffer fills up.

It seems to be depended of the selected board if Serial 1 is active by default or not. Now I managed to compile your sketch unchanged. But there is still a difference in behaviour:
Without the 'begin()' it stops in the second loop cycle:

09:39:06.101 -> end of setup
09:39:07.094 -> start
09:39:07.094 -> 63
09:39:07.094 -> 0
09:39:07.094 -> 0
09:39:07.094 -> 62
09:39:07.094 -> 0
09:39:07.094 -> -1
09:39:07.094 -> end
09:39:07.603 -> start
09:39:07.603 -> 62
09:39:07.603 -> 0
09:39:07.603 -> 0


But with begin() it works as expected ( with rx1 and tx1 connected ):

09:41:15.495 -> end of setup
09:41:16.484 -> start
09:41:16.484 -> 63
09:41:16.484 -> 0
09:41:16.484 -> 0
09:41:16.484 -> 62
09:41:16.484 -> 0
09:41:16.484 -> -1
09:41:16.484 -> end
09:41:16.998 -> start
09:41:16.998 -> 63
09:41:16.998 -> 1
09:41:16.998 -> 0
09:41:16.998 -> 62
09:41:16.998 -> 1
09:41:16.998 -> 45
09:41:16.998 -> end
09:41:17.510 -> start
09:41:17.510 -> 63
09:41:17.510 -> 1
09:41:17.510 -> 0
09:41:17.510 -> 62
09:41:17.510 -> 1
09:41:17.510 -> 207
09:41:17.510 -> end
09:41:17.987 -> start
09:41:17.987 -> 63
09:41:17.987 -> 1
09:41:17.987 -> 0
09:41:17.987 -> 62
09:41:17.987 -> 1
09:41:17.987 -> 70
09:41:17.987 -> end
09:41:18.498 -> start
09:41:18.498 -> 63
09:41:18.498 -> 1
09:41:18.498 -> 0
09:41:18.498 -> 62
09:41:18.498 -> 1
09:41:18.498 -> 41
09:41:18.498 -> end
...
...