Serial.print(Str)

Hi,

I'm trying to send string for example "FIN" from arduino and read the same string at another Arduino via TX and RX pins. Could anyone help me on this?

Transmitter

void setup()
{
  Serial.begin(38400);
}

void loop()
{
  Serial.print("FIN");
}

Receiver:

int c = 0;


void setup()
{
  Serial.begin(38400);
}

void loop()
{
  if(Serial.available() > 0) {
    c = Serial.read();
    Serial.print(byte(c));
  }
}

thanks!

Could anyone help me on this?

Help you do what?

I'm trying to send indication to the receiver by sending "FIN" string. and if the receiver received the the same string "FIN", then it will do something. i've tried to run the code i posted earlier and it gave me this "%9" instead of "FIN". even when i sent only "F", the receiver read as " instead of "F". Am i done wrong somewhere?

thanks

You are not accumulating the characters into an array, so you won't be able to determine if "FIN" was sent.

But that can be added after you get the receive part working. You are sending bytes. The bytes are the ascii representation of the letters F, I, and N (70, 73, and 78, respectively).

You are reading these as integers on the receiver. Then, you are casting the value to a byte and printing that. Why?

Since you are sending characters, why not read them and print them as characters?

char c;

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

yui,

I notice that in the receiving Arduino, you a printing out the characters on the same Serial interface from which you are receiving the characters. Are you using an Arduino where the the TX and RX pins are connected to a USB interface?

Regards,

-Mike

PaulS, thank you for replying.

Yes, it might seen weird why i'm using integers rather than char.It is because I'm building a device as a network interface,dealing with TCP and UDP packet. therefore i need integer to transmit the buffer of the packet to the receiver since it will take thousand of bytes. Everything else (Socket programming) is working except for this one. I'm having a problem to convert the string. I'm using Ethernet Shield and Duemilanove. And this is the first time i'm doing programming in C. If you do have any suggestion, much much appreciated if you could share with me.

thanks

My point was that everything that is being sent (by your sending sample) is being sent as character data. There is no reason not to receive it as such. At least for a test.

mike,

yes, exactly. Is it why the problem happened?

thanks

edit:

I'm sorry. please ignore the above post. I'm connecting TX & RX pins using serial cable i built.

PaulS,

That was why I'm getting confused cause last 2 weeks I could see 'F' at the receiver if i send 'F', but unfortunately not this time. or there is something not right with the boards?

If you are receiving data from one board using TX/RX, you can't use those same pins to communicate with the serial monitor. You can, however, use NewSoftSerial and any other pair of pins to communicate with the other Arduino, and use the RX/TX connection to talk to the Serial Monitor.

Paul,

Exactly. The USB issue is what I was trying to get at.

-Mike

What if the transmitter is only for transmit, and the receiver is for receive only. I mean for 1-way communication only. I think that shouldn’t be a problem right, if I’m not using NewSoftSerial?

Once the code is known to work, you can switch back to hardware serial (TX/RX). Until then, it's hard to separate the input from the (debug) output. The debug output goes back to the sender, and can't be distinguished from a valid reply.

Thre is some work on connecting two devices with a serial link at....

http://sheepdogguides.com/arduino/ahttoc.htm

(The "Level 4" section)

Perhaps the best is....

http://sheepdogguides.com/dt4t.htm

(You don't need to be able to program in Delphi to benefit from the material.)

Hello again..

thank you so much for the replies.

I went through to test sending 'F' to the receiver & manage to see it at the receiver. As PaulS said, it might be confused to look which one is the input/output.So, what I did was only connecting RX Arduino with the Serial monitor.

Now, I'm trying to get the first byte of data receive & the length of the buffer. But, unfortunately it didn't work. Here again I post the codes.

TX:

char myChar[2] = {'F'};

void setup()
{
   Serial.begin(38400);
}

void loop()
{
   Serial.write(myChar);
}

RX:

int c = 0;
int e = 148;


void setup()
{
  Serial.begin(38400);
}

void loop()
{
  if(Serial.available() > 0) {
    c = Serial.read();
    int len = sizeof(c);

    if((c == 148) && (len == 2)) {
      Serial.print("YES");
    }
    else {
      Serial.print("NO");
    }
  }
}

when I test only (c ==148) -->Printed 'YES' only (len == 2) --> Printed 'YES' but, when I combined both comparison, it gave me 'NO'

Am I missing something here?

thanks

When you declared an array on the sender, the non-explicitly initialized positions are filled with NULLS.

On the sender, you are sending the single letter F over and over again, with no pause between letters. In nothing flat, the receiver's serial buffer is going to fill up.

On the receiver, you are reading a character (a byte sized value) and storing it into an integer (a 2 byte sized space).

Then, you measure the size of the variable that you stored the value in. There is no buffer on the receiver, in the sense that you do not have an array that you are filling with data that you have read from the serial port.

when I test only (c ==148) -->Printed 'YES' only (len == 2) --> Printed 'YES' but, when I combined both comparison, it gave me 'NO'

Am I missing something here?

The code that you showed should have printed YES. But, there are issues. Measuring the size of a variable is not telling you what you seem to think it is. Second, (c == 148) could just as easily be written (c == 'F'). While 'F' and 148, mean exactly the same thing to the computer, the computer is not writing and debugging the program. Some human is, and 'F' and 148 definitely do not have the same meaning to humans.

I got your point. :)

I'd rewrote the codes for TX and RX, & now I could see any string that I like to see. Thanks a lot PaulS!!

Anyway, I have another question to ask, if you are OK with that.

How could I read the 1st byte, 2nd byte and so on from the buffer, then do a comparison using &&? for example:

if (buf[1] == 'A' && buf[2] == 'B' && buf[3] == 'C') {

//do something here

}

Is it possible to do it? I've tried but I always get error when trying to compile.

thanks

Of course, it's possible.

char inData[10]; // Or however many you need
byte index = 0;

void loop()
{
   while(Serial.available() > 0)
   {
      inData[index] = Serial.read();
      index++;
      inData[index] = '\0'; // NULL terminate the array
   }

   // inData is now a string read from the serial port.
   // Do something with it

   index = 0;  // Reset for next pass
}

There is no error checking here, to make sure that there is room in the array for the character being added. There is also no checking (since the sender is not sending any) for start of packet markers or end of packet markers, so what's in the string is just whatever was available in the serial port when loop is called. It may, or may not represent a complete packet.

Search for some of my other posts on this same subject, to find more robust serial data handling. The more robust methods rely on sending start-of- and end-of-packet markers.

You might want to try code like below on the rx arduino. The WString.h allows one to skip dealing with the array issues when a string is sent.

//zoomkat 8-6-10 serial I/O string test
//type a string in serial monitor. then send or enter
//http://www.arduino.cc/en/Tutorial/TextString for WString.h

#include <WString.h>
String readString = String(100);

void setup() {
      Serial.begin(9600);
        }

void loop() {

        while (Serial.available()) {
        delay(10);  
          if (Serial.available() >0) {
        char c = Serial.read();
        readString.append(c); }
        }
        
      if (readString.length() >0) {
      Serial.println(readString);
      
      readString="";
      } 
   }

I have tried the following codes, & I could see the result.

TX

char myChar[9] = {'<', 'F', 'I', 'N', 'A', 'C', 'K', '>'};

void setup()
{
   Serial.begin(9600);
}

void loop()
{
   Serial.print(myChar);
   delay(200);
}

RX:

int incomingByte;
char buffer[101];
int serialIn = 0;
int serialOut = 0;

void loop()
{
   if(Serial.available()) {
         
     while (Serial.available()){
       incomingByte = Serial.read();
       buffer[serialIn] = incomingByte;
       serialIn++;
       buffer[serialIn] = '\0';
     }
    }
 
   if(serialIn > 0) {
     for(serialOut=0; serialOut < serialIn; serialOut++) {
       Serial.print(buffer[serialOut],BYTE);
     }
   serialOut = 0;
   serialIn  = 0;
   buffer[serialIn] = '\0';
   }    
}

but, when I tried to read byte by byte like this, it keeps on giving me “No”.Why? I thought that the received char are there in the buffer.Could someone help me to figure out the cause?

RX2:

int incomingByte;
char buffer[101];
int serialIn = 0;
//int serialOut = 0;

void loop()
{
   if(Serial.available()) {
         
     while (Serial.available()){
       incomingByte = Serial.read();
       buffer[serialIn] = incomingByte;
       serialIn++;
       buffer[serialIn] = '\0';
     }
    }
   if(serialIn > 0){
     if(buffer[1] == 'F' && buffer[2] == 'I' && buffer[3] == 'N') {
       Serial.print("YES");
     }
     else {
       Serial.print("No");
     }
  }
   //serialOut = 0;
   serialIn  = 0;
   //buffer[serialIn] = '\0';
   }    
}

zoomkat,

i’m not so sure whether i could use string, because the entire of my project is dealing with TCP and UDP packet, which is known thousands of byte need to send. The one that I posted here just part of it. But, if i could, i shall use it. i’m sorry cause this is the first time i do programming, & i wish i could learn more.
thanks