function [SampleRate,Error] = i3dmgx2_WriteDataRate(SerialLink,SampleRate)
%Reads the data rate from EEPROM
%
%Arguments: SerialLink - Handle of serial link
%           SampleRate - Rate at which device sends out packets
%
%Returns:   SampleRate - Rate at which device sends out packets
%           Error - Error number

EEPROMAddress = [hex2dec('FC'),hex2dec('A2')]; %Convert EEPROM address to decimal bytes
EEPROMWord = ushort2bytes(round(51200/SampleRate)); %Convert EEPROM word to bytes
Error = setCommTimeouts(SerialLink,16); %Set timeout to a large value to account for long gaps when the sample rate is low
if Error == 0
    Read = 1; %Activate Read == 1 loop
    StartTime = now; %Find starting time
    while Read == 1 && 86400*(now - StartTime) <= 16
        [Packet,Error] = i3dmgx2_WriteEEPROMValue(SerialLink,EEPROMAddress,EEPROMWord); %Read sample rate from device
        if Error == 0 && max(Packet(2:3)) > 0
            SampleRate = 51200/convert2ushort(Packet(2:3)); %Convert EEPROM word to decimal
            Read = 0; %Exit Read == 1 loop
        else
            SampleRate = 1;
        end
    end
else
    SampleRate = 1;
end
if Error == 0
    Error = setCommTimeouts(SerialLink,1/SampleRate+.1); %Update timeouts based on found sample rate
end