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
setup()
andloop()
, 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?