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?
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?
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?
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.
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.
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.
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.
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.
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.
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="";
}
}
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?
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