Hi,
Assume that two values of 5 bytes each come at your serial port almost every 30 seconds.
So the procedure to read serial is as follows..
const byte Chars = 20;
char receivedChars[Chars];
void setup() {
Serial.begin(9600);}
void loop() {
if (Serial.available()>=5){
for(int i=0;i<5;i++){
receivedChars*=Serial.read();*
-
}*
-
Serial.println(receivedChars);*
-
}*
}
After a while the things get worse when the 2 values arrive all at once because of the different sending time.
Then i have 10 bytes which i have to read and split and print
How can i treat serial in this case?
Thanks in advance
It seems that you did not read this before posting a programming question and hence your code as posted has italics in it.
Please post your code in code tags to prevent that happening
As to the problem, have you got control of the format in which the data is sent ?
Have a look at Serial Input Basics.
If your input includes an end-of-line character you are going to get out of synchronization rapidly, because you are always reading five bytes at a time from the input, regardless of how many characters are in the buffer. If there is no end-of-line character, you really need a way to determine where the five bytes of data start and end, or a single missed character will throw off all the data from that point forward. Another problem you may be having is that if you are using any delay() instructions, printing a lot of text to the serial monitor, or doing anything else that delays program execution for an extended amount of time, you will be accumulating input in the serial buffer, which can lead to either not having enough time to actually process the input data, or processing one set of input data and then another immediately afterwards, possibly causing problems if you are depending on the delay between inputs.
There are two blocks of data; each block is composed 5 bytes ASCII formatted data. The transmitter should form a 'transmission frame' of the following/similar type so that the receiver is never confused to grasp the frame.
<1-byte binary coded start mark (must be outside 0x30 - 0x39, 0x41 - 0x46)> <5-byte ASCII coded data blcok-1> <data separator (must be ouiside 0x30 - 0x31, 0x41 - 0x46)> < 5-byte ASCII coded data block-2> <1-byte binary coded end mark (must be outside 0x30 - 0x39, 0x41 - 0x46 and different from start mark and data seperator>.
For example:
<12345,67893>
The transmission frame coveys the following stream:
3C 31 32 33 34 35 2C 36 37 38 39 33 3E (all are in hex base)
The codes to catch the above frame:
void loop()
{
byte n = Serial.available();
if(n = !0)
{
char x = Serial.read();
if(flag1 == false)
{
if (x == '<') //start mark found
{
flag1 = true;
}
}
else
{
Serial.readBytesUntil('>', myData, 20); //declare this global array: char myData[20] = "";
//----process received data from myData[] array -----------------------------
myData[5] = 0x00; //null-byte
int x1 = atoi(myData); //x1 = 12345
int x2 = atoi(myData+6); //x2 = 67893
flag1 = false;
char myData[20] = ""; //reset the array
}
}
}
Thank all for your anwsers.
Excuse me if was not clear about what i wrote.
I have 2 transmitters and 1 receiver
Even if i place to each packet start and stop characters like < and >how can i split the 2 packets when they arrive simultaneously like <22.40><28.30> and other times the packets come <22.40> and other time <28.30> without losing packet?
i would appreciate your help.
What range of values will you be sending and receiving ?
Is the data from both transmitters being received on the same serial port?
Do you have some way of telling which transmitter the data comes from?
It would help tremendously if you could post your code, because processing two packets of data within thirty seconds should not be a problem, even if they arrive simultaneously.
bgalafagas:
how can i split the 2 packets when they arrive simultaneously
If they literally arrive simultaneously on the same RX pin then the data will be garbled.
If they arrive separately but one very shortly after the other then there will be no technical problem.
The simple way to identify the sender is to include an extra char in the message - for example ArduinoA could always send <'A',nn> and ArduinoB could always send <B,nn> where nn is the varying number that is sent
...R