user21214: Hi i have an Arduino mega 2560 i send data from the Arduino over USB to PC. That works when i test a simple Serial.print("Hello"); but that is not that what i need. I send a timestamp as a longe Value and the databyte as a short Value. When i send the timestamp as BIN like that:
      //Initialisisation

const int RX = 0;
const int TX = 1;
const int RX1 = 19;
const int TX1 = 18;
unsigned long currentMillis;

void setup()
{
  pinMode(RX, INPUT);   //Readpin for USB
  pinMode(TX, OUTPUT);  //Writepin for USB
  pinMode(RX1, INPUT);  //Readpin for bus
  pinMode(TX1, OUTPUT); //Writepin for für bus

  Serial.begin(38400);   //open interface for USB
  Serial1.begin(4800);   //open interface for bus

  while (!Serial & !Serial1 &! Serial2)
  {
    ;//wait for serial port connect.
  }
}

void loop()
{
  //Ask if the timestamp over 8 hours, when yes,
  //send the time to the pc and reset currentMillis.
  currentMillis = millis();
  if (currentMillis >= 28800000)  //milli() 8 hours is a 4 byte  timestamp 28.800.000 millisec.
  {
    unsigned long timestamp = currentMillis;
    sendBinaryTime(timestamp);
    currentMillis = 0;
  }//end if

  //if the businterface available, read a byte
  //and send it with the timestamp to the PC.
  //If the businterface not available send an error.
  //short incomingByte = Serial1.read();
  //sendBinaryValue(incomingByte);
  if(Serial1.available()>0) //if the bus (serial1) available?
    {
      short incomingByte = Serial1.read(); //read the next byte from _rx_buffer
      sendBinaryValue(incomingByte);
      sendBinaryTime(currentMillis);   //sendBinary send a long over the bus
    }//end if 
}//end Loop()


//Methods for sending long values

void sendBinaryValue(short incomingByte)
  {
    int temp = (incomingByte & 0xFF00)>>8;
    Serial.println(temp, HEX);
    temp = (incomingByte & 0x00FF);
    Serial.println(temp, HEX);
    return;
  }

void sendBinaryTime(unsigned long currentMillis)//send milliseconds after every byte
{
  int temp = (currentMillis & 0xFF000000)>>24; //send 16 bit of the low value
  Serial.print(temp, BIN);
  temp = (currentMillis & 0x00FF0000)>>16;      //send 16 bit of the high value
  Serial.print(temp, BIN);
  temp = (currentMillis & 0x0000FF00)>>8;
  Serial.print(temp, BIN);
  temp = (currentMillis & 0x000000FF);
  Serial.print(temp, BIN);
  return;
}

All is fine i haven´t any error. But i want the timestamp as DEC so i have changed the code to:
       void sendBinaryValue(short incomingByte)
 {
   int temp = (incomingByte & 0xFF00)>>8;
   Serial.println(temp, HEX);
   temp = (incomingByte & 0x00FF);
   Serial.println(temp, HEX);
   return;
 }

void sendBinaryTime(unsigned long currentMillis)
{
 int temp = (currentMillis & 0xFF000000)>>24;
 Serial.print(temp, DEC);
 temp = (currentMillis & 0x00FF0000)>>16;
 Serial.print(temp, DEC);
 temp = (currentMillis & 0x0000FF00)>>8;
 Serial.print(temp, DEC);
 temp = (currentMillis & 0x000000FF);
 Serial.print(temp, DEC);
 return;
}

Now when i look on the serial monitor i get an error.
Here you can see it:

I send an A as ASCII sign that must be Hex 41 and i receive a Hex 5 but why? When i send the timestamp as BIN i get the 41 and haven´t any problem.
I hope someone can help me with friendly wishes sniffi

user7508: I have found several problems with your program, although they may not
be related with your primary problem:
while (!Serial & !Serial1 &! Serial2)

You mean while (!Serial || !Serial1 || !Serial2), although this is
only useful with virtual serial ports, like on the Leonardo. On the
mega, all of these ports are real hardware ports.
void sendBinaryValue(short incomingByte)

The way you name the parameter is misleading: a short is not a byte,
it's two bytes.
int temp = (incomingByte & 0xFF00)>>8;

You are bit-shifting a signed number, which is going to do a sign
extension. This is probably not what you want.
Serial.print(temp, DEC);

You are printing several numbers one after the other with no separator.
This is going to cause ambiguities: you cannot know whether "345" means
"3 45" or "34 5".
Finally, I don't get the purpose of your functions. Why don't you just
print the numbers like this?
Serial.println(incomingByte, HEX);
Serial.println(currentMillis);


21214: <p>Hi i have an Arduino mega 2560 i send data from the Arduino over USB to PC. That works when i test a simple Serial.print("Hello"); but that is not that what i need. I send a timestamp as a longe Value and the databyte as a short Value. When i send the timestamp as BIN like that:</p>

<pre><code>      //Initialisisation

const int RX = 0;
const int TX = 1;
const int RX1 = 19;
const int TX1 = 18;
unsigned long currentMillis;

void setup()
{
  pinMode(RX, INPUT);   //Readpin for USB
  pinMode(TX, OUTPUT);  //Writepin for USB
  pinMode(RX1, INPUT);  //Readpin for bus
  pinMode(TX1, OUTPUT); //Writepin for für bus

  Serial.begin(38400);   //open interface for USB
  Serial1.begin(4800);   //open interface for bus

  while (!Serial &amp; !Serial1 &amp;! Serial2)
  {
    ;//wait for serial port connect.
  }
}

void loop()
{
  //Ask if the timestamp over 8 hours, when yes,
  //send the time to the pc and reset currentMillis.
  currentMillis = millis();
  if (currentMillis &gt;= 28800000)  //milli() 8 hours is a 4 byte  timestamp 28.800.000 millisec.
  {
    unsigned long timestamp = currentMillis;
    sendBinaryTime(timestamp);
    currentMillis = 0;
  }//end if

  //if the businterface available, read a byte
  //and send it with the timestamp to the PC.
  //If the businterface not available send an error.
  //short incomingByte = Serial1.read();
  //sendBinaryValue(incomingByte);
  if(Serial1.available()&gt;0) //if the bus (serial1) available?
    {
      short incomingByte = Serial1.read(); //read the next byte from _rx_buffer
      sendBinaryValue(incomingByte);
      sendBinaryTime(currentMillis);   //sendBinary send a long over the bus
    }//end if 
}//end Loop()


//Methods for sending long values

void sendBinaryValue(short incomingByte)
  {
    int temp = (incomingByte &amp; 0xFF00)&gt;&gt;8;
    Serial.println(temp, HEX);
    temp = (incomingByte &amp; 0x00FF);
    Serial.println(temp, HEX);
    return;
  }

void sendBinaryTime(unsigned long currentMillis)//send milliseconds after every byte
{
  int temp = (currentMillis &amp; 0xFF000000)&gt;&gt;24; //send 16 bit of the low value
  Serial.print(temp, BIN);
  temp = (currentMillis &amp; 0x00FF0000)&gt;&gt;16;      //send 16 bit of the high value
  Serial.print(temp, BIN);
  temp = (currentMillis &amp; 0x0000FF00)&gt;&gt;8;
  Serial.print(temp, BIN);
  temp = (currentMillis &amp; 0x000000FF);
  Serial.print(temp, BIN);
  return;
}
</code></pre>

<p>All is fine i haven´t any error. But i want the timestamp as DEC so i have changed the code to:</p>

<pre><code>       void sendBinaryValue(short incomingByte)
 {
   int temp = (incomingByte &amp; 0xFF00)&gt;&gt;8;
   Serial.println(temp, HEX);
   temp = (incomingByte &amp; 0x00FF);
   Serial.println(temp, HEX);
   return;
 }

void sendBinaryTime(unsigned long currentMillis)
{
 int temp = (currentMillis &amp; 0xFF000000)&gt;&gt;24;
 Serial.print(temp, DEC);
 temp = (currentMillis &amp; 0x00FF0000)&gt;&gt;16;
 Serial.print(temp, DEC);
 temp = (currentMillis &amp; 0x0000FF00)&gt;&gt;8;
 Serial.print(temp, DEC);
 temp = (currentMillis &amp; 0x000000FF);
 Serial.print(temp, DEC);
 return;
}
</code></pre>

<p>Now when i look on the serial monitor i get an error.
Here you can see it:</p>

<p><a href="https://i.stack.imgur.com/i7ruR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/i7ruR.png" alt="enter image description here"></a></p>

<p>I send an A as ASCII sign that must be Hex 41 and i receive a Hex 5 but why? When i send the timestamp as BIN i get the 41 and haven´t any problem.</p>

<p>I hope someone can help me with friendly wishes sniffi</p>
 7508: <p>I have found several problems with your program, although they may not
be related with your primary problem:</p>



<pre class="lang-c++ prettyprint-override"><code>while (!Serial &amp; !Serial1 &amp;! Serial2)
</code></pre>

<p>You mean <code>while (!Serial || !Serial1 || !Serial2)</code>, although this is
only useful with virtual serial ports, like on the Leonardo. On the
mega, all of these ports are real hardware ports.</p>

<pre class="lang-c++ prettyprint-override"><code>void sendBinaryValue(short incomingByte)
</code></pre>

<p>The way you name the parameter is misleading: a <code>short</code> is not a byte,
it's two bytes.</p>

<pre class="lang-c++ prettyprint-override"><code>int temp = (incomingByte &amp; 0xFF00)&gt;&gt;8;
</code></pre>

<p>You are bit-shifting a signed number, which is going to do a sign
extension. This is probably not what you want.</p>

<pre class="lang-c++ prettyprint-override"><code>Serial.print(temp, DEC);
</code></pre>

<p>You are printing several numbers one after the other with no separator.
This is going to cause ambiguities: you cannot know whether "345" means
"3 45" or "34 5".</p>

<p>Finally, I don't get the purpose of your functions. Why don't you just
print the numbers like this?</p>

<pre class="lang-c++ prettyprint-override"><code>Serial.println(incomingByte, HEX);
Serial.println(currentMillis);
</code></pre>
