Go Down

Topic: Serial Problems (Read 639 times) previous topic - next topic

herctrap

Hallo

i have a compass that outputs uart data like that

Quote
$OHPR,21.2,2.3,-2.5,14.2,0.0000,370.48,-298.50,126.78,179.10,1.003,0.040,-0.043,1.002,-0.01*0C
$OHPR,21.2,2.3,-2.5,14.2,0.0000,370.47,-298.50,126.79,179.06,1.003,0.040,-0.043,1.002,-0.01*05


and i wrote this programm to capture them


Code: [Select]
 
while ( Serial.read () != '$' );
  BytesRead = Serial.readBytesUntil('*',SerialBuffer,127);
  for(Position=(BytesRead+1); Position<128; Position++)
  {
    SerialBuffer[Position] = B0;
  }
  for(Position=0; Position<128; Position++)
  {
    OS4000Data[Position] = SerialBuffer[Position];
  }


that works great

but when i copy the above to a bigger scetch

like that

Code: [Select]

void setup()
{
  pinMode(3,OUTPUT);
  digitalWrite(3,HIGH);

  Serial.begin(UartBaudRate); 
  Wire.begin(I2C_Address); 
  Wire.onRequest(requestEvent);
  Wire.onReceive(ReceiveCommand);
}


void loop()
{
  if (command == 'H')
  {
    command = 'N';
    Capture_OS4000();
  }
  else if (command == 'P')
  {
    digitalWrite(3,HIGH);
    command = 'N';
  }
  else if (command == 'p')
  {
    digitalWrite(3,LOW);
    command = 'N';
  }
  else if (command == 'R')
  {
    Serial.write(27);
    delay(20);
    Serial.print("R");
    delay(20);
    CommandLow = Wire.read();
    CommandHigh = Wire.read();
    Command = ( ( (int)CommandLow<<8 ) | ( (int)CommandHigh ) ); 
    Serial.print(Command);
    Serial.write(13);
    command = 'N';
  }
}


void  Capture_OS4000()
{
  while ( Serial.read () != '$' );
  BytesRead = Serial.readBytesUntil('*',SerialBuffer,127);
  for(Position=(BytesRead+1); Position<128; Position++)
  {
    SerialBuffer[Position] = B0;
  }
  for(Position=0; Position<128; Position++)
  {
    OS4000Data[Position] = SerialBuffer[Position];
  }
}

void ReceiveCommand(int howMany)
{
  command = Wire.read();
}

void requestEvent()
{
  Wire.write (OS4000Data, 127); 
}




then the output is like that

Quote
OHPR,21.2,2.3,-2.4,14.6,0.0000,371.$OHPR,21.3,2.3,-2.4,14.6,0.0000,370.59,-298.45,126.93,179.31,1.003,0.041,-0.042,1.002,-0.01.ÿ
OHPR,21.2,2.3,-2.5,14.6,0.0000,370.40,-298.41,126.96,178.9$OHPR,21.2,2.3,-2.5,14.6,0.0000,371.01,-298.50,127.11,179.97,1.003,0.ÿ
OHPR,21.2,2.3,-2.5,14.6,0.0000,371.$OHPR,21.2,2.3,-2.5,14.6,0.0000,370.70,-298.69,126.98,179.11,1.003,0.041,-0.045,1.001,-0.01.ÿ
OHPR,21.2,2.3,-2.5,14.6,0.0000,370.76,-298.72,126.99,179.1$OHPR,21.2,2.3,-2.5,14.6,0.0000,371.11,-298.72,127.07,179.84,1.003,0.ÿ
OHPR,21.2,2.3,-2.5,14.6,0.0000,371.$OHPR,21.2,2.3,-2.5,14.6,0.0000,370.66,-298.63,127.00,179.10,1.003,0.041,-0.043,1.001,-0.01.ÿ
OHPR,21.2,2.3,-2.5,14.6,0.0000,370.73,-298.65,127.01,179.2.4,14.6,0.0000,371.15,-298.74,127.02,179.92,1.003,0.041,-0.042,1.001,ÿ



where i am wrong

thanks a lot

herctrap

i changed the programm

so the slave always updates the OS4000Data

Code: [Select]
void loop()
{
  if (command == 'P')
  {
    digitalWrite(3,HIGH);
    command = 'N';
  }
  else if (command == 'p')
  {
    digitalWrite(3,LOW);
    command = 'N';
  }
  else if (command == 'R')
  {
    Serial.write(27);
    delay(20);
    Serial.print("R");
    delay(20);
    CommandLow = Wire.read();
    CommandHigh = Wire.read();
    Command = ( ( (int)CommandLow<<8 ) | ( (int)CommandHigh ) ); 
    Serial.print(Command);
    Serial.write(13);
    command = 'N';
  }
  Capture_OS4000();
}

void  Capture_OS4000()
{
  while ( Serial.read () != '$' );
  BytesRead = Serial.readBytesUntil('*',SerialBuffer,127);
  for(Position=(BytesRead+1); Position<128; Position++)
  {
    SerialBuffer[Position] = B0;
  }
  for(Position=0; Position<128; Position++)
  {
    OS4000Data[Position] = SerialBuffer[Position];
  }
}

void requestEvent()
{
  Wire.write (OS4000Data, 127); 
}




and the master

Code: [Select]
void Read_Data()
{
  Wire.beginTransmission(2);
  Wire.requestFrom(2,127);
  Wire.endTransmission();
  for ( int i=0; i<128; i++ ) Serial.write(Wire.read());
  Serial.println("");
}


and its working

but how i will make to only do that

Code: [Select]
  Wire.write (OS4000Data, 127); 

if that is completed

Code: [Select]
  for(Position=0; Position<128; Position++)
  {
    OS4000Data[Position] = SerialBuffer[Position];
  }


????

PaulS

The for loop completes after iterating 128 times. If you want to do something after that happens, put the code after the for loop.

Code: [Select]
  for(Position=(BytesRead+1); Position<128; Position++)
  {
    SerialBuffer[Position] = B0;
  }

It takes exactly one NULL to terminate a string.

Code: [Select]
  for(Position=0; Position<128; Position++)
  {
    OS4000Data[Position] = SerialBuffer[Position];
  }

Why do you have two similar buffers? One would be enough. Do you have memory to waste?

herctrap

the wire can't send a char string but only a byte string
and Serial can't buffer on a byte string but on a char

what else can i do?

PaulS

Quote
the wire can't send a char string but only a byte string

Yes, Wire.send()/Wire.write() can send a char array, if you cast it properly.

A char and a byte are exactly the same size.

Code: [Select]
char buff[] = "Send Me!";
Wire.send((byte *)buff, strlen(buff));


Quote
and Serial can't buffer on a byte string but on a char

Yes, it can, with the proper cast.

Code: [Select]
  byte stuff[10];
  int cnt = Serial.readBytesUntil('*', (char *)stuff, 10);

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy