SHARE
TWEET

Untitled

a guest Sep 21st, 2019 124 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int LogoClient::ReadBlock(dword Addr, word ByteCount, byte *Data)
  2. {
  3.   size_t Length = 0;
  4.   byte Checksum = 0;
  5.  
  6.   if (Data == NULL)
  7.     return SetLastError(errCliInvalidPDU);
  8.   memset(Data, 0, ByteCount);
  9.  
  10.   // Send the Query message
  11.   memset(&PDU, 0, sizeof(PDU));
  12.   // Read Block Command Code
  13.   PDU.H[Length++] = 0x05;
  14.   // Address Field
  15.   if (AddrLength == 4)
  16.   {
  17.     PDU.H[Length++] = highByte(word(Addr >> 16));
  18.     PDU.H[Length++] = lowByte(word(Addr >> 16));
  19.   }
  20.   PDU.H[Length++] = highByte(word(Addr));
  21.   PDU.H[Length++] = lowByte(word(Addr));
  22.   // Number of bytes
  23.   PDU.H[Length++] = highByte(ByteCount);
  24.   PDU.H[Length++] = lowByte(ByteCount);
  25.   // Send Query
  26.   if (StreamClient->write(PDU.H, Length) != Length)
  27.     return SetLastError(errStreamDataSend);
  28.  
  29.   // Receive the Response message
  30.   memset(&PDU, 0, Length);
  31.   // Get first byte
  32.   if (RecvPacket(PDU.H, 1) != 0)
  33.     return LastError;
  34.   LastPDUType = PDU.H[0];       // Store PDU Type
  35.  
  36.   if (LastPDUType == ACK)       // Connection confirmed
  37.   {
  38.     // Get Data Block
  39.     Length = 0;                 // Init statement
  40.     while (Length < ByteCount)
  41.     {
  42.       PDURequested = ByteCount - Length;
  43.       if (PDURequested > MaxPduSize)
  44.         PDURequested = MaxPduSize;
  45.       if (RecvPacket(Data + Length, PDURequested) != 0)
  46.         return LastError;
  47.       Length += PDURequested;   // Iteration expression
  48.     }
  49.  
  50.     // Get Checksum
  51.     if (RecvPacket(PDU.T, 1) != 0)
  52.       return LastError;
  53.  
  54.     // Calculate Checksum (only the data field are used for this)
  55.     for (int i = 0; i < ByteCount; i++)
  56.       Checksum = (*(Data + i) ^ Checksum) % 256;
  57.  
  58.     if (PDU.T[0] != Checksum)
  59.       // Error, the response does not have the correct Checksum
  60.       return SetLastError(errCliDataRead);
  61.   }
  62.   else if (LastPDUType == NOK)  // Request not confirmed
  63.   {
  64.     // Get second byte
  65.     if (RecvPacket(PDU.H[1], 1) != 0)
  66.       return LastError;
  67.  
  68.     // Get Error Type
  69.     return SetLastError(CpuError(PDU.H[1]));
  70.   }
  71.   else
  72.     return SetLastError(errCliDataRead);
  73.  
  74.   return SetLastError(0);
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top