Byte Array Broken?

Hello, I am building a Xmodem sender containing my eagle key using a Arduino Duemilanove. Xmodem part works well but the byte array points to some of the machinecode.

#include <avr/pgmspace.h>
const PROGMEM int filesize = 19;
const PROGMEM byte data[] = {0x50, 0x4b,/*The rest of the data*/};
int datablock = 0;
int incomingByte = 0;
void setup() {
  Serial.begin(9600);
  message();
}

void loop() {
}

//Help message
void message() {
  Serial.println(F("-Use Xmodem to recive eagle.zip"));
  Serial.println(F("-Press R to reset transfer."));
  Serial.println(F("-Press H to display this message."));
  Serial.flush();
}

void serialEvent() {
  incomingByte = Serial.read();
  if (incomingByte == 104 || incomingByte == 72) {
    message();
  } else if (incomingByte == 114 || incomingByte == 82) {
    datablock = 0;
    Serial.println(F("Transfer reset."));
    Serial.flush();
  } else if (incomingByte == 21) {
    sendBlock(datablock, filesize);
  } else if (incomingByte == 6) {
    datablock++;
    sendBlock(datablock, filesize);
  }
}

void sendBlock(int xmodemblock, int numberofblocks) {
  if (xmodemblock == numberofblocks) {
    Serial.write(4);
  } else {
    int sum = 0;
    Serial.write(1);
    Serial.write(xmodemblock + 1);
    Serial.write(255 - (xmodemblock + 1));
    Serial.flush();
    for (int i = (xmodemblock * 128); i < (xmodemblock + 1) * 128; i++) {
      Serial.write(data[i]);
      sum = 0xff & (sum + data[i]);
      Serial.flush();
    }
    Serial.write(lowByte(sum));
    Serial.flush();
  }
}

Xmodem_USB_stick.ino (16.1 KB)

hexdump of what I recived.txt (11.9 KB)

You're not reading "data" correctly. Check the reference pages for details of how to read PROGMEM.

Also, the code can be changed a little to make it easier to read:

#define NAK 21               // Place at top of file
#define ACK 6


void serialEvent() {
  incomingByte = toupper(Serial.read());
  switch (incomingByte) {
    case 'H':
      message();
      break;
    case 'R':
      datablock = 0;
      Serial.println(F("Transfer reset."));
      Serial.flush();
      break;
    case NAK:
      sendBlock(datablock, filesize);
      break;
    case ACK:
      datablock++;
      sendBlock(datablock, filesize);
      break;
    default:
     // Serial.print("In default: incomingByte = ");   // Error trap...??
     // Serial.println(incomingByte);
      break;
  }
}

GhostWolf: Hello, I am building a Xmodem sender containing my eagle key using a Arduino Duemilanove. Xmodem part works well but the byte array points to some of the machinecode.

Yes, your data array has been declared to reside in program memory space (PROGMEM, therefore you CANNOT access the data as if they were in a RAM array using

data[i]

You have to use a function to retrieve the data from PROGMEM instead, like

pgm_read_byte(&data[i])

jurs: Yes, your data array has been declared to reside in program memory space (PROGMEM, therefore you CANNOT access the data as if they were in a RAM array using

data[i]

You have to use a function to retrieve the data from PROGMEM instead, like

pgm_read_byte(&data[i])

Thank you, it worked perfectly.