Receiving unreliable data using the serial port on Adruino UNO

Hello,
I am trying to establish a serial communication between Arduino UNO and a weighing indicator. I am receiving data but gibbrish, unwanted data. Would like to know what exactly could be the problem. The code is as written below.

int i=0;
void setup()  {
Serial.begin(9600);
Serial.flush();
}

void loop()
{
 int mySerialData[100];
 //send data only when you receive data:
 if (Serial.available() > 0)
 {
   // read the incoming byte:
   mySerialData[i] = Serial.read();
   // say what you got:
   Serial.print(mySerialData[i],DEC);
   Serial.print(",");
   i = i+1;
   if (i == 12)
   {
     Serial.println(" ");
     i=0;
   }
 }

Thanks & Regards
Harshad Karkera

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Compare this statement:

   mySerialData = Serial.read();

with what is in your code. Not the same are they?

If you read How to post code properly and post your code in code tags, your code would not suddenly turn into italics and make it hard to read.

Your coding problem is that this statement:

 int mySerialData[100];

needs to be global, otherwise the array will be created each time the loop function is called, and destroyed when you return from the function.

Pete

How do you know when all of the data has been received? It appears that 12 is the magic number. Is that correct, or is there some form of packet marker that tells you the End of Data (EOD) has been reached?

I am trying to establish a serial communication between Arduino UNO and a weighing indicator. I am receiving data but gibbrish, unwanted data.

Is the weighing device serial I/O TTL or rs232 format? Do you have the baud rate set correctly?

I am trying to establish a serial communication between Arduino UNO and a weighing indicator. I am receiving data but gibbrish, unwanted data.

What weighing indicator? Datasheet?

What gibberish? Please post it.

What baud rate does this unknown device use?

How is the device connected to the Arduino?

How to use this forum

My arduino is connected via the serial port on pin 0 (on arduino uno board) to weighing indicator transmitting data on (baud rate of 9600. Parity = None, No. of Stop bits = 1, No. of bits =8 ). The Weighing indicator is model A1, make Yaohua from China. I have checked the data being transmitted on my hyper terminal and the same is as per the declared protocol which is in the attached image.

Yaohua Scale Serial String.png

The examples in Serial Input Basics may be useful.

...R

Could it be that your communication is interfering with the USB communication?

You write to USB serial (or at least you hope so, see Serial.print(...)),
but you read from your device AND the USB serial.

How about trying SoftwareSerial on an different (unused) pin?

Your setup uses Serial.flush(); right after starting Serial.
This function only flushes the OUTPUT,
since nothing was sent,
nothing can be pending in the write buffer.
Just drop it, it does not work as you expect(ed).

Your protocol seems to be quite easy,
you have a startbyte and an endbyte.

So you could:

wait for startbyte,
if found start collecting
(with or without startbyte does not really matter, its implicit)
collect (counting the length) until endbyte or message too long
if endbyte was found and the message has the correct length,
verify packet content (checksums?),
if valid, extract/format information.

harshadkarkera:
My arduino is connected via the serial port on pin 0 (on arduino uno board) to weighing indicator transmitting data on (baud rate of 9600. Parity = None, No. of Stop bits = 1, No. of bits =8 ). The Weighing indicator is model A1, make Yaohua from China. I have checked the data being transmitted on my hyper terminal and the same is as per the declared protocol which is in the attached image.

You never responded to reply #3. That is important, because RS-232 and TTL signal levels are inverted. In that case, all you will see is garbage, even if your software is correct.

I think you have both a hardware and software problem. You can separate the two, or at least confirm which one it is (or both), but running an example sketch instead of your sketch, that simply echos the scale data to the serial terminal.

Also, if you are connecting raw RS-232 signals to the Arduino pins, you can damage the processor.