0

I'm trying to send through radio apc220 from a C-xenomai program to and from an arduino Mega.

I have one task created for sending, and one for reading in the xenomai program. The arduino side reads and the writes to serial.

The baud-rates are both equal and tried 9600 and 19200, but the data recieved is random data, and not the values sent.

C code (This is in a task with "rt_task_set_periodic(NULL, TM_NOW, TM_INFINITE);" and priority in task creation of 99). Values of bufferMotorX are constantly changing.

strcpy(buffer,bufferMotor1);
strcat(buffer,bufferMotor2);
strcat(buffer,bufferMotor3);
strcat(buffer,bufferMotor4);

fprintf(stderr,"sending...%s\n",buffer);

write(apcFd, buffer, CONTROLLER_BUFFER_SIZE);

Arduino side:

      if (Serial.available() > 0) {
            // read the incoming byte:
            for (int i=0; i < 12; i++)
             {
                inputByte=Serial.read();
                inputBuffer[i]=inputByte;

             }  

            bufferMotor1[0]=inputBuffer[0];
            bufferMotor1[1]=inputBuffer[1];
            bufferMotor1[2]=inputBuffer[2];

            /*bufferMotor2[0]=inputBuffer[3];
            bufferMotor2[1]=inputBuffer[4];
            bufferMotor2[2]=inputBuffer[5];

            bufferMotor3[0]=inputBuffer[6];
            bufferMotor3[1]=inputBuffer[7];
            bufferMotor3[2]=inputBuffer[8];

            bufferMotor4[0]=inputBuffer[9];
            bufferMotor4[1]=inputBuffer[10];
            bufferMotor4[2]=inputBuffer[11];*/
            Serial.print("I received: ");
            Serial.println(bufferMotor1);

            motor1=atoi(bufferMotor1);

Any ideas on what I'm doing wrong?

cthyde
  • 13
  • 1
  • You have not checked *how many* bytes are available, or tested before every serial read (however it works). If the read is non-blocking, it will return before the the next char is available. As for sending you are sending the buffer size `CONTROLLER_BUFFER_SIZE` not the number of bytes it holds, which would be `strlen(buffer)` possibly `+1` for the `nul` string terminator. – Weather Vane Apr 23 '15 at 17:40
  • I have changed the reader to serial.readBytes in arduino, this has made an improvement. After working with it more and changing values, by managing the periodic values of the task i get better or worst values, Is the task period affecting the read/write to serial ports? – cthyde Apr 25 '15 at 11:55
  • Did you check the return value from `Serial.readBytes()`? If you want robust and well behaved code, you'll check the return value of every function that provides one. That will also make it much easier to detect why the code is malfunctioning. The default timeout is 1 second. Were the correct number of bytes read? – Weather Vane Apr 25 '15 at 16:46

0 Answers0