2

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:

enter image description here

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

  • Please, post a testable code, with setup() and loop(), so we can reproduce your problem. I tried the obvious and got “41”, as expected, which proves you have a problem in a piece of code you are not showing to us. How do you expect us to help you with that? – Edgar Bonet Jul 11 '16 at 9:01
  • I have fix it, i think perhapes it is an already known problem. – sniffi Jul 11 '16 at 9:08
2

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);
  • Ok i have delete the while(!Serial & !Serial1 & !Serial2) . I have changed the ringbuffer to 16bit (my short) because i want handle flags at the high byte. Can i send a short and a long over Serial.print? – sniffi Jul 11 '16 at 9:34
  • Yes, you can print() numbers from 8 to 32 bits, signed or unsigned. – Edgar Bonet Jul 11 '16 at 9:37
  • Ok i have change all now the bug is fixed thanks for all – sniffi Jul 11 '16 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.