Hi,
I'd like to send a constant string from serial1 to serial2 but I can't understand where my code is wrong, because nothing is received on serial2. Can someone help me?
Thank you.
Marco
PS: the code is attached
ask_to_forum.pde (1 KB)
Hi,
I'd like to send a constant string from serial1 to serial2 but I can't understand where my code is wrong, because nothing is received on serial2. Can someone help me?
Thank you.
Marco
PS: the code is attached
ask_to_forum.pde (1 KB)
incomingByte = Serial2.available();
This does not read a serial byte, but rather returns the number of bytes that may be available to read.
If a receive character is ready to be read, meaning Serial2.avalible >=1, you then use the incomingByte = Serial2.read(); to actual read the character from the buffer.
Here is an example from the arduino reference:
// send data only when you receive data:
if (Serial2.available() > 0) {
// read the incoming byte:
incomingByte = Serial2.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
Hi retrolefty,
thanks for your help. I have correct the test on serial2.available and now it run correctly, but nothing is read from serial2. My code could be send a constant string from serial1 to serial2. After the Serial1.print(mia_trasm), I was expecting the same string by reading the serial2, but nothing has arrived, in other words the Serial.println(mia_rice), seems return null.
Any suggestions?
Thanks
Marco
ask_to_forum.pde (1.03 KB)
Posting small amounts of code as an attachment is less likely to get you help. Post the code in a text box, instead.
Serial1.flush();
Do you know what this does? If not, why are you using it? If you do, why are you using it?
Throwing away random amounts of unread serial data is rarely a good thing.
UCSR1A=UCSR1A |(1 << TXC1);
What do you think this is doing?
Or this:
while (!(UCSR1A & (1 << TXC1)));
if(Serial2.available() > 0)
{
incomingByte = Serial2.available();
Serial.println (incomingByte,DEC);
for(int n=0; n<22; n++) //22? 23? incomingbyte?
mia_rice[n] = Serial2.read();
If there is a byte in the serial buffer to be read, read all 23 of them. No! No! No!
Hi PaulS,
here my replies to your questions:
Serial1.flush()
Flushes the buffer. So any call to Serial.read() will return only data received after all the most recent call to Serial.flush(). My scope is to send (serial.print) only my constant string, the serial.flush clear the buffer and the next serial.print will send the right data.
UCSR1A=UCSR1A |(1 << TXC1);
while (!(UCSR1A & (1 << TXC1)));
The Serial1 send the data (my constant string) and I want be sure that until the last character is completely send, the program execute a loop endless (Wait for the transmision to complete).
if(Serial2.available() > 0)
{
incomingByte = Serial2.available();
Serial.println (incomingByte,DEC);
for(int n=0; n<22; n++) //22? 23? incomingbyte?
mia_rice[n] = Serial2.read();
I don't understand your comment. I send a constant string through the serial1 and I'd like read it through the serial2. The problem is that the program never execute the "for" statement because the test of incomingbyte is alway false. I have used the array, in the "for" statement, to read, "character after character", the serial buffer but I don't know if this is the right way to read a constant string from serial2.
If you running the program (copied) the Serial2 never receive data.
Marco
int incomingByte;
void setup()
{
Serial2.begin(4800);
Serial1.begin(4800);
Serial.begin(9600);
}
void loop()
{
char mia_trasm [ ] = "9199019801969696967704";
Serial.println("Trasmetto");
Serial.println(mia_trasm);
Trasmetti(mia_trasm);
delay(1000);
char mia_rice [ ] = " ";
Ricevi(incomingByte, mia_rice);
Serial.println("Ricevo");
Serial.println(mia_rice);
delay(1000);
}
void Trasmetti(char mia_trasm[])
{
Serial1.flush();
delay(5);
UCSR1A=UCSR1A |(1 << TXC1);
Serial1.print(mia_trasm);
while (!(UCSR1A & (1 << TXC1)));
}
void Ricevi (int incomingByte, char mia_rice[ ])
{
if(Serial2.available() > 0)
{
incomingByte = Serial2.available();
Serial.println (incomingByte,DEC);
for(int n=0; n<22; n++) //22? 23? incomingbyte?
mia_rice[n] = Serial2.read();
}
else
{
Serial.println ("ERRORE");
}
}
Serial1.flush()
Flushes the buffer. So any call to Serial.read() will return only data received after all the most recent call to Serial.flush(). My scope is to send (serial.print) only my constant string, the serial.flush clear the buffer and the next serial.print will send the right data.
There is a receive buffer and a transmit buffer. The transmit buffer is exactly one character long. The Serial.print() blocks if the transmit buffer is full.
The flush() function has nothing to do with the transmit buffer. It only affects the receive buffer. Throwing away random amounts of unread data just seems like a bad idea to me.
UCSR1A=UCSR1A |(1 << TXC1);
while (!(UCSR1A & (1 << TXC1)));
The Serial1 send the data (my constant string) and I want be sure that until the last character is completely send, the program execute a loop endless (Wait for the transmision to complete).
I don't know what these commands do. What I do know is that Serial.print blocks until the last character has been placed in the transmit register. The actual sending of that character happens nano-seconds later.
Since you are having difficulties sending on one serial port and receiving on another serial port on the same Arduino, it looks to me like these commands are not doing exactly what you think that they are doing. So, I'd get rid of them. At least as a test.
I don't understand your comment. I send a constant string through the serial1 and I'd like read it through the serial2.
You can't read what hasn't arrived. If you are not getting the first byte, then the for loop will never be executed. If you do start getting data, the if block will be true, and 23 characters will be read in the time it takes before another character has arrived. The data stored in mia_rice will be -1 in each element.
How are the Serial and Serial2 pins connected?
PaulS,
thank for your help. Your explain has been very clear for me. Now I understand how the serial.print and serial.read works. Your last question forced me to review the pin connection (were wrong!) and now the program work fine. Here the last version.
Marco
char mia_rice[22];
int incomingByte;
void setup()
{
Serial2.begin(4800);
Serial1.begin(4800);
Serial.begin(9600);
}
void loop()
{
char* mia_trasm= {"9199019801969696967704"};
Serial.println("Trasmetto");
Serial.println(mia_trasm);
Trasmetti(mia_trasm);
delay(1000);
Ricevi(incomingByte, mia_rice);
Serial.println("Ricevo");
Serial.println(mia_rice);
delay(1000);
}
void Trasmetti(char* mia_trasm)
{
Serial1.print(mia_trasm);
}
void Ricevi (int incomingByte, char mia_rice[])
{
incomingByte = Serial2.available();
Serial.println (incomingByte,DEC);
if(incomingByte)
{
for(int n=0; n<22; n++)
mia_rice[n] = Serial2.read();
}
else
{
Serial.println ("ERRORE");
}
}