Pages: [1]   Go Down
Author Topic: Bugs in Serial.read()??[NEW]  (Read 819 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Per Arduino ad Astra

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 .
Logged

Pages: [1]   Go Up
Jump to: