Go Down

Topic: Bugs in Serial.read()??[NEW] (Read 912 times) previous topic - next topic

windrunnerRobot

Hello~
Today when I try to use my arduino to control servos from <Robotis Dynamixel>through serial port,I find that Arduino Mega may behave wrong in this condition:
(Who teach me how to put code here)
Thank AWOL for teaching me how to post code:)
This is the hole test code for detect a special array of HEX bytes:
FF FF 01 03 00 20 DB
That is a answer packet from my servo.
Code: [Select]
#include <LiquidCrystal.h>
LiquidCrystal lcd(53, 51, 49, 47, 45, 43);
int ledPin =  13;
int TxEN = 39;
int RxEN = 37;

void setup()
{
 Serial.begin(1000000);
 Serial2.begin(1000000);
 pinMode(ledPin, OUTPUT);
 pinMode(TxEN, OUTPUT);
 pinMode(RxEN, OUTPUT);
 lcd.begin(16, 2);
}

void loop()
{
 byte data = ReadS(1,0x2B,1);
 Serial.write(data);
 delay(1000);
}

byte ReadS(byte ID,byte address,byte length)
{
 byte RevFlag = 0;
 byte rx_counter = 0;
 byte rx_buffer[] = {0,0,0,0,0,0,0};
 byte data;
 Serial2.flush();
 digitalWrite(TxEN, HIGH);
 digitalWrite(RxEN, LOW);
 Serial2.write(0xff);
 Serial2.write(0xff);
 Serial2.write(ID);
 Serial2.write(0x04);
 Serial2.write(0x02);
 Serial2.write(address);
 Serial2.write(length);
 unsigned int checksum = ID+6+address+length;
 checksum = ~(checksum%256);
 byte sum=checksum&0x000000ff;
 Serial2.write(sum);
 digitalWrite(RxEN, HIGH);
 digitalWrite(TxEN, LOW);
 digitalWrite(ledPin, HIGH);
 while(!(Serial2.available()>0));
 while(Serial2.available()>0)//(!RevFlag)&&
 {
   Serial.write(Serial2.available());
   /*digitalWrite(ledPin, HIGH);
   delay(200);
   digitalWrite(ledPin, LOW);
   delay(200);*/
   //Serial.write(Serial1.read());
   rx_buffer[rx_counter] = Serial2.read();
   rx_counter++;
   switch(rx_counter)
   {
     case 1:
     if(rx_buffer[rx_counter-1] != 0xff)
     {
       Serial.write(rx_counter);
       Serial.write(rx_buffer[rx_counter-1]);
       Serial.write(0xff);
       rx_counter = 0;
     }
     break;
     case 2:
     if(rx_buffer[rx_counter-1] != 0xff)
     {
       Serial.write(rx_counter);
       Serial.write(rx_buffer[rx_counter-1]);
       Serial.write(0xff);        rx_counter = 0;
     }
     break;
     case 3:
     if(rx_buffer[rx_counter-1] != ID)
     {
       Serial.write(rx_counter);
       Serial.write(rx_buffer[rx_counter-1]);
       Serial.write(0xff);        rx_counter = 0;
     }
     break;
     case 4:
     if(rx_buffer[rx_counter-1] != 3)
     {
       Serial.write(rx_counter);
       Serial.write(rx_buffer[rx_counter-1]);
       Serial.write(0xff);        rx_counter = 0;
     }
     break;
     case 5:
     if(rx_buffer[rx_counter-1] != 0x00)
     {
       Serial.write(rx_counter);
       Serial.write(rx_buffer[rx_counter-1]);
       Serial.write(0xff);        rx_counter = 0;
     }
     break;
     case 6:
     data = rx_buffer[rx_counter-1];
     break;
     case 7:
     unsigned int RXchecksum;
     for(byte i=2;i<6;i++)
     {
       RXchecksum += rx_buffer[i];
     }
     RXchecksum = ~(RXchecksum%256);
     byte RXsum=RXchecksum&0x000000ff;
     if(rx_buffer[rx_counter-1] != RXsum)
     {
       Serial.write(rx_counter);
       Serial.write(rx_buffer[rx_counter-1]);
       Serial.write(0xff);        rx_counter = 0;
     }
     else RevFlag = 1;
     break;
   }
 }
 /*for(byte i=0;i++;i<7)
 {
   lcd.print(rx_buffer[i]);
 }*/
 Serial2.flush();
 return(data);
}


lcd is just for testing.
When I saw the LED on ,I know that the MC is waiting for the packet.And I send FF FF 01 03 00 20 DB to the Serial2 port,I recieve this from Serial port:
06 05 04 03 02 05 20 FF 01 01 DB FF 97
I think the right answer is
07 06 05 04 03 02 01 00 20
20 is the data I want in loop();


Please Help me~ Thank you!
windrunner

Groove

1Mbps is quite a rate for a 16MHz controller.
I'd be inclined to reduce the bit rate to something more sensible like 2400bps - if the problem persists, it's your logic that's at fault.
If the problem goes away, then the controller/software isn't up to the higher bit rate.
Per Arduino ad Astra

ksamay

Hi.

According to me the problem is with using the Serial.Read at 1 mbps.

U can fix the problem by either of the following two ways.

1. Lower the baud rate . i know BAUD 34 i.e 57600 works fine with the arduino serial library.

2. I personally did the following.

Disabled the RX interrupt in the main serial library in arduino and used the Robotis embedded C lib that is available at support.robotis.com. It works perfectly at 1 MBps.

Let me know if you need more details .

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