Go Down

Topic: transmitt 16-bit data with a Serial connection (Read 102 times) previous topic - next topic

thor7

Dear all,

i try to transmitt a 2 byte int from an arduino uno towards a duemilanove via a seriell connection.

this is the senders code:
Code: [Select]

//imports
#include <SoftwareSerial.h>

//defines
const int pinRX = 3;
const int pinTX = 4;
const int baudRateSSerial = 19200;
const int maxDim1 = 10;

const int maxValue = 250;
const int startMarker = 251;
const int endMarker = 252;

int interfacedelay = 2000;
uint16_t testArray[maxDim1][3]=
{
  { 1, 1001, 1},
  { 2, 2002, 1},
  { 3, 3003, 1},
  { 4, 4004, 1},
  { 5, 5005, 1},
  { 6, 6006, 1},
  { 7, 7007, 1},
  { 8, 8008, 1},
  { 9, 9009, 1},
  {10,10010, 1}
};

SoftwareSerial mySerial(pinRX,pinTX);

void setup() {
    Serial.begin(9600);
    mySerial.begin(baudRateSSerial);
}

void loop() {
  mySerial.write(startMarker);
  sendCommaArray();
  mySerial.write(endMarker);
  delay(2000);
}

void sendCommaArray(){
  for(int i=0; i<maxDim1; i++){
    if((testArray[i][0]!=0) && (testArray[i][2]==1)){
      uint16_t data = testArray[i][1];
      //Serial.print("data: ");
      //Serial.print(data);
      //Serial.print(" bin: ");
      byte buff[2];
      buff[0]=data & 0xff;
      buff[1]=(data>>8) & 0xff;
      //Serial.print(buff[1],BIN);
      //Serial.print(" ");
      //Serial.println(buff[0],BIN);
      mySerial.write(buff,2);
    }
  }
  Serial.println();
}


and the recivers code:
Code: [Select]
//includes
#include <SoftwareSerial.h>

//defines
const int pinRX = 3;
const int pinTX = 4;
const int baudRate = 19200;
const int baudRateSSerial = 19200;
unsigned long starttime;

bool filled = false;

const int numberBytes = 11; 
const int startMarker = 251;
const int endMarker = 252;

uint16_t receivedBytes[numberBytes];
int rec;

SoftwareSerial mySerial(pinRX,pinTX);

void setup() {
  pinMode(pinTX,OUTPUT);
  pinMode(pinRX,INPUT);
  Serial.begin(baudRate);
  mySerial.begin(baudRateSSerial);
}

void loop() {

  reciveWithMarkers();

  displayData(receivedBytes,numberBytes);
}

void reciveWithMarkers(){
  static int index=0;

  static bool readserial = false;

  while(mySerial.available()>0 && (!filled) ){
    rec = mySerial.read();

    if(rec == startMarker){
      readserial=true;
    }else if(rec == endMarker){
      readserial=false;
      if(index<numberBytes){ 
        for(int i=index; i<numberBytes; i++){
          receivedBytes[i]=0;
        }
      }
      index=0;
      filled=true;
    }else{
      if(readserial){
        byte a = rec;
//        Serial.print("1.: ");
//        Serial.print(a,BIN);
        byte b = mySerial.read();
//        Serial.print(" 2.: ");
//        Serial.print(b,BIN);
        uint16_t data = ((b & 0xFF)<<8) | a;
//        Serial.print(" data: ");
//        Serial.print(data,BIN);
//        Serial.print(" ");
//        Serial.println(data);
        receivedBytes[index]=data;
        index++;
        if(index==numberBytes){
          filled=true;
        }
      }
    }
  }
}

void displayData(uint16_t a[], int a_length){
  if(filled){
    Serial.println("received data:");

    for(int i=0; i<a_length-1; i++){
        Serial.print(a[i]);
        Serial.print(", ");
    }   
    Serial.println();
    filled=false;
  }
}


this is what my reciver displays me at the seriell monitor:
65513, 65283, 65490, 65287, 65467,
65291, 65444, 65295, 65421, 65299,

in binary its this:
1111111111101001, 1111111100000011, 1111111111010010, 1111111100000111, 1111111110111011,
1111111100001011, 1111111110100100, 1111111100001111, 1111111110001101, 1111111100010011

as you can see the first byte is always filled with 1's which looks really odd for me

but when i want to have a deeper look at the received bits and remove the comments in the function reciveWithMarkers() this is what my seriell monitor displays:

1.: 11101001 2.: 11 data: 1111101001 1001
1.: 11010010 2.: 111 data: 11111010010 2002
1.: 10111011 2.: 1011 data: 101110111011 3003
1.: 10100100 2.: 1111 data: 111110100100 4004
1.: 10001101 2.: 10011 data: 1001110001101 5005
1.: 1110110 2.: 10111 data: 1011101110110 6006
1.: 1011111 2.: 11011 data: 1101101011111 7007
1.: 1001000 2.: 11111 data: 1111101001000 8008
1.: 110001 2.: 100011 data: 10001100110001 9009
1.: 11010 2.: 100111 data: 10011100011010 10010
received data:
1001, 2002, 3003, 4004, 5005, 6006, 7007, 8008, 9009, 10010,

which exacly look like it should in my eyes.
The only change between these both results is the add/remove of the Seriell displayment inside the function reciveWithMarkers(). I would be glad if somebody could explain this :)

Thanks for your time and thoughts and sorry my bad english.

pylon

Code: [Select]
        byte b = mySerial.read();


You don't check if the second byte is already arrived. If no byte is available in the buffer this method returns -1, which translates to 0xff for unsigned chars.

thor7

and as Serial.print() lead to a small delay this is enough for the second byte to arrive?
I think i got it :)
thanks for your fast reply

pylon

Quote
and as Serial.print() lead to a small delay this is enough for the second byte to arrive?
In the posted code the Serial.print()s are commented out.

A Serial.print() just writes to an internal buffer and doesn't wait for the stuff to be transfered. Writing to the buffer is done in a few µs but transferring a byte over that connection needs about 500µs.

Go Up