jisotalo / ads-client

Unofficial Node.js ADS library for connecting to Beckhoff TwinCAT automation systems using ADS protocol.
https://jisotalo.fi/ads-client/
MIT License
80 stars 17 forks source link

convertFromRaw does not correctly check the size for arrays of structs #155

Closed InconceivableDuck closed 2 months ago

InconceivableDuck commented 2 months ago

Related to https://github.com/jisotalo/ads-client/issues/53

Looks like it was fixed for arrays of basic types, but not for arrays of complex types.

Error:

ClientException: Given data buffer and data type sizes mismatch: Buffer is 16192 bytes and data type is 2484 bytes

Symbol Info:

{
  indexGroup: 16448,
  indexOffset: 841576,
  size: 16192,
  adsDataType: 65,
  adsDataTypeStr: 'ADST_BIGTYPE',
  flags: 8,
  flagsStr: [ 'TypeGuid' ],
  arrayDimension: 0,
  nameLength: 25,
  typeLength: 39,
  commentLength: 0,
  name: 'GVL_Var.TestArrayStruct40',
  type: 'ARRAY [0..10,15..30] OF DUT_Structure_1',
  comment: '',
  arrayData: [],
  typeGuid: 'efa3486981b90e95fbbf485c484d6131',
  attributes: [],
  reserved: <Buffer 00 00 00 00 00 00 00>
}

Result from readRawMulti:

{
  success: true,
  errorInfo: { error: false, errorCode: 0, errorStr: 'No error' },
  target: { indexGroup: 16448, indexOffset: 841576 },
  data: <Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 16142 more bytes>
}

Output of getDataType from within convertFromRaw:

{
  name: '',
  type: 'DUT_Structure_1',
  size: 92,
  offset: 0,
  adsDataType: 65,
  adsDataTypeStr: 'ADST_BIGTYPE',
  comment: '',
  attributes: [],
  rpcMethods: [],
  arrayData: [ { startIndex: 0, length: 11 }, { startIndex: 15, length: 16 } ],
  subItems: [
    {
      name: 'TestInt',
      type: 'INT',
      size: 2,
      offset: 0,
      adsDataType: 2,
      adsDataTypeStr: 'ADST_INT16',
      comment: '',
      attributes: [Array],
      rpcMethods: [],
      arrayData: [],
      subItems: []
    },
    {
      name: 'TestReal',
      type: 'REAL',
      size: 4,
      offset: 4,
      adsDataType: 4,
      adsDataTypeStr: 'ADST_REAL32',
      comment: '',
      attributes: [Array],
      rpcMethods: [],
      arrayData: [],
      subItems: []
    },
    {
      name: 'TestString',
      type: 'STRING(80)',
      size: 81,
      offset: 8,
      adsDataType: 30,
      adsDataTypeStr: 'ADST_STRING',
      comment: '',
      attributes: [],
      rpcMethods: [],
      arrayData: [],
      subItems: []
    }
  ]
}

We can see the raw data's length matches the symbol's size, but the check errors. Using readSymbol or commenting out the check does result in successfully parsing the result.

crishoj commented 2 months ago

I also encountered an issue with arrays of structs. I believe it is fixed in the upcoming v2.0, in case you'd like to give that a try:

 "ads-client": "^2.0.0-beta.3"

Migration guide: https://github.com/jisotalo/ads-client/blob/2.0.0-beta.3/MIGRATION.md

InconceivableDuck commented 2 months ago

I can confirm that v2 does work.

jisotalo commented 2 months ago

Thanks for reporting! If everything works in v2, then I might not look into this.

InconceivableDuck commented 2 months ago

Happy to wait for the v2 release. Closing this issue.