Arduino Serial.print Error

Hello Everyone,

I have a problem sending values to PC using Serial.print(). I am using arduino pro mini. I get values from RTC with the Wire.h and sending its values to PC the problem that the PC receive wrong data like if the value of variable is '59' the PC reads it as two ASCII bytes and receive "53" and "59". the code I use in ARDUINO

// all variable are bytes
Wire.beginTransmission(0x68);
    Wire.write(0x00);
    Wire.endTransmission();
    Wire.requestFrom(0x68, 7);
    if (Wire.available())
    {
      dsSec = bcdToDec(Wire.read());
      dsMin = bcdToDec(Wire.read());
      dsHr = bcdToDec(Wire.read());
      dsDOW = bcdToDec(Wire.read());
      dsDay = bcdToDec(Wire.read());
      dsMonth = bcdToDec(Wire.read());
      dsYear = bcdToDec(Wire.read());
    }
    Serial.print(dsSec);
    Serial.print(dsMin);
    Serial.print(dsHr);
    Serial.print(dsDay);
    Serial.print(dsMonth);
    Serial.print(dsYear);
    Serial.print(eeDay);
    Serial.print(eeMonth);
    Serial.print(eeYear);

Thanks.

Yes, that's how print works. It sends ascii. write on the other hand will send raw values.

Delta_G:
Yes, that's how print works. It sends ascii. write on the other hand will send raw values.

ok, but Serial.write() doesn't send any data i use it like follow

// all variable are bytes
Wire.beginTransmission(0x68);
    Wire.write(0x00);
    Wire.endTransmission();
    Wire.requestFrom(0x68, 7);
    if (Wire.available())
    {
      dsSec = bcdToDec(Wire.read());
      dsMin = bcdToDec(Wire.read());
      dsHr = bcdToDec(Wire.read());
      dsDOW = bcdToDec(Wire.read());
      dsDay = bcdToDec(Wire.read());
      dsMonth = bcdToDec(Wire.read());
      dsYear = bcdToDec(Wire.read());
    }
    Serial.write(dsSec);
    Serial.write(dsMin);
    Serial.write(dsHr);
    Serial.write(dsDay);
    Serial.write(dsMonth);
    Serial.write(dsYear);
    Serial.write(eeDay);
    Serial.write(eeMonth);
    Serial.write(eeYear);

What is receiving and displaying this data? What does it expect?

mohamedSoliman:
ok, but Serial.write() doesn't send any data i use it like follow

You need to post your complete program. We cannot tell the type of the variables you are trying to send with write().

...R

Here are my complete code

this expect to send time from DS1307 and saved date in arduino pro mini when pressing push button the problem that in my C# Software receiving nothing but by using arduino serial monitor i receive some spaces and one mark i receive something like the following line
" ’ . "

#include <Wire.h>
#include <EEPROM.h>

byte dsSec; //DS1307 Seconds
byte dsMin; //DS1307 Minutes
byte dsHr;  //DS1307 Hours
byte dsDOW;  //DS1307 Day of week
byte dsDay;  //DS1307 Day
byte dsMonth;  //DS1307 Month
byte dsYear;   //DS1307 Year
byte eeDay;  //EEPROM Day
byte eeMonth;  //EEPROM Month
byte eeYear;   //EEPROM Year

byte bcdToDec(byte val)
{
  return (((val / 16) * 10) + (val % 16));
}

byte decToBCD(byte val)
{
  return (((val / 10) * 16) + (val % 10));
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Wire.begin();
  pinMode(8, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(8) == LOW)
  {
    delay(200);
    Wire.beginTransmission(0x68);
    Wire.write(0x00);
    Wire.endTransmission();
    Wire.requestFrom(0x68, 7);
    if (Wire.available())
    {
      dsSec = bcdToDec(Wire.read());
      dsMin = bcdToDec(Wire.read());
      dsHr = bcdToDec(Wire.read());
      dsDOW = bcdToDec(Wire.read());
      dsDay = bcdToDec(Wire.read());
      dsMonth = bcdToDec(Wire.read());
      dsYear = bcdToDec(Wire.read());
    }
    Serial.write(dsSec);
    Serial.write(dsMin);
    Serial.write(dsHr);
    Serial.write(dsDay);
    Serial.write(dsMonth);
    Serial.write(dsYear);
    Serial.write(eeDay);
    Serial.write(eeMonth);
    Serial.write(eeYear);
  }
}

You just going to ignore my question from #3?

This style

Serial.write(dsSec);

is not really appropriate if you want to see the data on the Serial Monitor because the Serial Monitor will be trying to interpret the byte values as ascii characters. For example 59 will appear as ; (semi-colon).

This looks very much like an XY-problem. Please tells what you are trying to achieve rather than how you think it might be done.

...R

Delta_G:
You just going to ignore my question from #3?

Sorry, I'm not ignoring but as i understand I think I already reply to your question. I am using C# software to receive the data from arduino pro mini and write it as label to know the time and date stored in the DS1307 and the arduino EEPROM.

There are lots of C# programs out there. An infinite number of C# codes that you could write. Just telling me the language you used doesn't tell me anything about how that program is handling serial data.

Robin2:
This style

Serial.write(dsSec);

is not really appropriate if you want to see the data on the Serial Monitor because the Serial Monitor will be trying to interpret the byte values as ascii characters. For example 59 will appear as ; (semi-colon).

This looks very much like an XY-problem. Please tells what you are trying to achieve rather than how you think it might be done.

...R

what I am trying to do is to know what is the time and date stored in the DS1307 using serial communication between arduino and C# windows application just as calendar reminder to know that time the DS1307 holds.

Here are the C# code. I am sorry I am a beginner at using the forums so sorry for the lake of information

private delegate void SetT(string text);
byte[] data = new byte[9];

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            serialPort1.Read(data, 0, 9);
            
                string s;
                s = (data[2]).ToString() + " : " + (data[1]).ToString() + " : " + (data[0]).ToString();
                x = 0;
                editText(s);
                s = (data[3]).ToString() + " / " + (data[4]).ToString() + " / " + (data[5]).ToString();
                x = 1;
                editText(s);
                s = (data[6]).ToString() + " / " + (data[7]).ToString() + " / " + (data[8]).ToString();
                x = 2;
                editText(s);
                x = 0;
        }

        private void editText(string text1)
        {
            if (x == 0)
            {
                if (txtDSTime.InvokeRequired)
                {
                    SetT tt = editText;
                    txtDSTime.Invoke(tt, text1);
                }
                else
                    txtDSTime.Text = text1;
            }
            else if (x == 1)
            {
                if (txtDsDate.InvokeRequired)
                {
                    SetT tt = editText;
                    txtDsDate.Invoke(tt, text1);
                }
                else
                    txtDsDate.Text = text1;
            }
            else if (x == 2)
            {
                if (txtEE.InvokeRequired)
                {
                    SetT tt = editText;
                    txtEE.Invoke(tt, text1);
                }
                else
                    txtEE.Text = text1;
            }
        }

mohamedSoliman:
what I am trying to do is to know what is the time and date stored in the DS1307 using serial communication between arduino and C# windows application just as calendar reminder to know that time the DS1307 holds.

I would do that almost exactly as you have it in the program in your Original Post. The only change I would make would be to put a comma between each item and < > at the start and end. Like this …

Serial.print('<');
Serial.print(dsSec);
Serial.print(',');
Serial.print(dsMin);
Serial.print(',');
Serial.print(dsHr);
Serial.print(',');
// etc
Serial.println('>');

The purpose of the < > is to let the receiving program know when the starts and ends.

The concept is illustrated in the 3rd example in Serial Input Basics

Then it should be straightforward to make your PC program interpret that correctly. This Python - Arduino demo may help.

…R

Robin2:
I would do that almost exactly as you have it in the program in your Original Post. The only change I would make would be to put a comma between each item and < > at the start and end. Like this …

Serial.print('<');

Serial.print(dsSec);
Serial.print(’,’);
Serial.print(dsMin);
Serial.print(’,’);
Serial.print(dsHr);
Serial.print(’,’);
// etc
Serial.println(’>’);




The purpose of the < > is to let the receiving program know when the starts and ends.

The concept is illustrated in the 3rd example in [Serial Input Basics](http://forum.arduino.cc/index.php?topic=396450.0)

thanks, It is good idea and it works.

Then it should be straightforward to make your PC program interpret that correctly. This [Python - Arduino demo](http://forum.arduino.cc/index.php?topic=225329.msg1810764#msg1810764) may help.

...R

thanks it is a good idea and it works