Serial2.available

Status
Not open for further replies.

kirbizz

Member
Hello, I am using HC-06 and want to send the message. It is connected to Serial 2(9 and 10 pins). The code I am using is listed. Why I don't have Serial2.available?
Code:
void setup()  
{
  // set digital pin to control as an output
  // set the data rate for the SoftwareSerial port
  Serial2.begin(9600);
  Serial.begin(9600);
  // Send test message to other device
}

void loop() {
  // put your main code here, to run repeatedly:
  //Serial2.println("LEd on, in AT mode; input AT commands");
  if (Serial2.available()>0)
  {
  Serial.println("habrahabr");
  Serial2.print("123456");
  Serial2.print ("\r\n");
  }
  delay(50);
}
 
What is your question asking? Not sure I follow. If you're wondering if the current code compiles with the "Serial2.available()" line, then the answer is yes. If you're asking something else then please clarify.
 
What is your question asking? Not sure I follow. If you're wondering if the current code compiles with the "Serial2.available()" line, then the answer is yes. If you're asking something else then please clarify.
I am asking, why Serial2.Available is not true everytime? The Rx of bluetooth is connected to Tx of teensy and Tx to Rx. If I delete Serial2.available, then in bluetooth monitor I can see lots of times E0.
 
Are you echoing what you're receiving on the bluetooth module? Or sending other data from the bluetooth? If not, it's not returning true because you're only sending data in one direction on the Serial2 line (Tx line from the Teensy into the Rx line of the bluetooth).

It might help to post a schematic.
 
Are you echoing what you're receiving on the bluetooth module? Or sending other data from the bluetooth? If not, it's not returning true because you're only sending data in one direction on the Serial2 line (Tx line from the Teensy into the Rx line of the bluetooth).
It might help to post a schematic.
No, I am'not doing echoing. Please, can you say, what should I do in this situation. In truth, I need only to send the data to my phone.
 
it is not working correct on my teensy(((
Code:
void setup()
{
  Serial2.begin(9600); //set baud rate
  Serial.begin(9600);
}
 
void loop()
{
  while(Serial2.available())
  {//while there is data available on the serial monitor
    Serial.println("read data");
    Serial2.println(Serial2.read(), DEC);
  }
}
It is sending the message, but in console I see "ааааааааа".
 
@kirbizz, you code is trying to print to serial2 what you are reading from serial2.

Code:
Serial2.println(Serial2.read(), DEC);
 
If you want to see what you received from the Serial2 in the terminal (Serial), you should be printing that to Serial.println and not Serial2.println.
 
If you want to see what you received from the Serial2 in the terminal (Serial), you should be printing that to Serial.println and not Serial2.println.
Code:
void setup()
{
  Serial2.begin(38400); //set baud rate
  Serial.begin(38400);
  Serial2.println("initialising");
}
 
void loop()
{
  while(Serial2.available())
  {//while there is data available on the serial monitor
    Serial.println("read data");
    int incomingbyte =  Serial2.read();
    //int incomingbyte = 1;
    Serial.println(incomingbyte,DEC);
    Serial2.clear();
  }
}

I opened Teraterm with my bluetooth serial port, pressed "1" and I saw in monitor arduino "236", why? I want to see "1".
 
might be a data type issue. perhaps try casting something like:
char myincoming = (char)Serial2.read();
Serial.print(myincoming);
 
I opened Teraterm with my bluetooth serial port, pressed "1" and I saw in monitor arduino "236", why? I want to see "1".
I would expect to see 49 which is the decimal value of the "1" character in ASCII and ISO 8859-1/646.

Now, if somehow you were connected to something that used EBCDIC, which was the character set used on the original IBM mainframes (which came from translating Hollereth card columns into binary), the value of "1" as a byte would be 241.
 
The good news is this is very likely "working", once you get the correct baud rate and correct code to print the byte as a character instead of a number.
 
@kirbizz, below is the simplest serial receive and print to monitor sketch that I have used and it works well. It just reads incoming and prints to monitor, it is set up for text / characters rather than data:

Code:
String receivedText = "";
 
void setup() {
  Serial1.begin(9600); //rx1 and tx1 = pins 0 and 1 on Teensy 3
  delay(200);
}
 
void loop() {
   if (Serial.available()) Serial1.write(Serial.read());
  while(Serial1.available() > 0) { // While there is more to be read, keep reading.
    receivedText += (char)Serial1.read();
    delay(10);  
  }
  if(receivedText != "") Serial.println(receivedText);
  receivedText = "";}
 
Last edited:
The good news is this is very likely "working", once you get the correct baud rate and correct code to print the byte as a character instead of a number.
I ried to use @mortonkopf code, but I don't see the text, I am sending to teensy. I am taking one symbol "я", but I am sending numbers and symbols. The baudrate in windows is 9600. All the screenshots is attached. I can't understand what is wrong the second day.
1.PNG
2.PNG
3.PNG
4.PNG
 
I ried to use @mortonkopf code, but I don't see the text, I am sending to teensy. I am taking one symbol "я", but I am sending numbers and symbols. The baudrate in windows is 9600. All the screenshots is attached. I can't understand what is wrong the second day.
execuse me, the default speed of my bluetooth module is 57600, if i use this speed everything works correctly. Thank you very much for everybody.
 
The good news is this is very likely "working", once you get the correct baud rate and correct code to print the byte as a character instead of a number.
what is the maximum tested baud rate, that I can use on UART Serial 1? I want in truth 1382400bps, but 460800bps is ok, is it possible? Bluetooth module as i understand support these baudrates.
 
Status
Not open for further replies.
Back
Top