www.vtiinstruments.com
EX1629 Onboard Memory 337
int sendLen, recLen = 0;
int i = 0;
char byte[3];
if(strlen(data) != ((DS2431_SCRATCHPAD_LEN * 2 ) +
(DS2431_SCRATCHPAD_LEN-1)))
{
printf("Data was not the right length (wanted 23, got %i)\n",
strlen(data));
return -1;
}
sendLen = 1; // reserve first byte for length
// access the current device with address in DATA_ID
SendPkt[sendLen++] = CMD_ML_ACCESS;
// construct a block of communication to MicroLAN
SendPkt[sendLen++] = CMD_ML_DATA;
SendPkt[sendLen++] = 4+DS2431_SCRATCHPAD_LEN; // block length
SendPkt[sendLen++] = 5+DS2431_SCRATCHPAD_LEN; // data length
// send the write scratchpad command
SendPkt[sendLen++] = WRITE_SCRATCHPAD;
// send the address byte
if (address > (DS2431_MEMORY_LEN-DS2431_SCRATCHPAD_LEN) || address
< 0)
{
printf("Invalid scratchpad address, must be between 0 and 88
hex\n");
return -1;
}
SendPkt[sendLen++] = address;
SendPkt[sendLen++] = 0;
// the 5 bytes of data to write
for (i = 0; i < ((2*DS2431_SCRATCHPAD_LEN) +
(DS2431_SCRATCHPAD_LEN-1)); i+=3)
{
strncpy(byte, &data[i],2);
byte[2]='\0';
SendPkt[sendLen++] = (uint8_t)strtoul(byte, NULL, 16); //convert
to hex
}
//The copy command:
// access the current device with address in DATA_ID
SendPkt[sendLen++] = CMD_ML_ACCESS;
// construct a block of communication to MicroLAN
SendPkt[sendLen++] = CMD_ML_DATA;
SendPkt[sendLen++] = 5; // block length
SendPkt[sendLen++] = 4; // data length
// send the copy scratchpad command
SendPkt[sendLen++] = COPY_SCRATCHPAD;
// send the validation key
if (address > (DS2431_MEMORY_LEN-DS2431_SCRATCHPAD_LEN) || address
< 0)
{
printf("Invalid scratchpad address, must be between 0 and 88
hex\n");
return -1;
}
SendPkt[sendLen++] = address;
SendPkt[sendLen++] = 0;
SendPkt[sendLen++] = 0x07;
// delay for 128ms
SendPkt[sendLen++] = CMD_DELAY;
SendPkt[sendLen++] = 1;
SendPkt[sendLen++] = DELAY_128 | DELAY_MS;