top of page

Przykładowe podstawowe procedury transmisji danych w środkowisku Embarcadero Delphi

​Procedura odczytu danych przez sieć LAN za pomocą komponentu TidTCPclient

 procedure TForm1.Read_idTCPClient_data(count: byte; buf_data : array of byte; Sender : TObject; var cc: integer);
  var c : byte;
      a: Integer;
      mbuf: TidBytes;
  begin
    if Form8.IdTCPClient1.Connected then
    begin
      SetLength(mbuf, 8);
      for a := 0 to 7 do
        mbuf[a] := buf_data[a];
      try
        Form8.IdTCPClient1.Socket.Write(mbuf, 8);
      except
      end;
      ProgressInfoForm1.Next;
      Form1.TMSFMXLED1.State := true;
      Form1.TMSFMXLED2.State := false;
      c := 0;
      repeat
        ProgressInfoForm1.Next;
        c := c + 1;
        Delay(ModbusTimeOut);
      until Form8.IdTCPClient1.Socket.CheckForDataOnSource(1000) or
        (c = ModbusProbes);
      Form1.TMSFMXLED1.State := false;
      ModBusFrame := false;
      if (c < ModbusProbes) and
        (Form8.IdTCPClient1.Socket.InputBuffer.Size > 0) then
        LanRxChar(count, Sender);
      if ModBusFrame then
        cc := 0
      else
        cc := ModbusProbes;
    end;
  end;

Procedura dekodowania ramki MODBUS

procedure TForm1.LanRxChar(ABytes: byte; Sender: TObject);
  var
    mbuf: TidBytes;
    crc: word;
    crc1: byte;
    crc2: byte;
    a: Integer;
  begin
    if not Firmware_upload then
    begin
      ModBusFrame := false;
      SetLength(mbuf, ABytes);
      a := 0;
      repeat
        Delay(100);
        a := a + 1;
        if not Form8.IdTCPClient1.Socket.InputBufferIsEmpty then
        begin
          if Form8.IdTCPClient1.Socket.InputBuffer.Size >= ABytes then
            Form8.IdTCPClient1.Socket.InputBuffer.ExtractToBytes(mbuf,
              ABytes, false);
          if Form8.IdTCPClient1.Socket.InputBuffer.Size < ABytes then
            Form8.IdTCPClient1.Socket.InputBuffer.Clear;
        end;
      until Form8.IdTCPClient1.Socket.InputBufferIsEmpty or (a = 5);
      if a < 5 then
      begin

        crc := Modbus.crc16(mbuf, ABytes);

        crc1 := Hi(crc);
        crc2 := Lo(crc);
        if (crc1 = mbuf[ABytes - 2]) and (crc2 = mbuf[ABytes - 1]) and
          ((crc1 > 0) or (crc2 > 0)) then
        begin
          for a := 1 to ABytes do
            ModBusBuf.buf[a] := mbuf[a - 1];
          ModBusBuf.count := ABytes;
          ModBusFrame := true;
        end
        else
        begin
          ModBusFrame := false;
        end;
      end;
    end;
  end;

modbusmat.jpg

Przykładowy odczyt Temperatury z ARM poprzez USB za pomocą programu Modbus Mat

modbus_temp.jpg

02       Adres urządzenia - ARM: $02

03       Funkcja $03 - zapytanie X rejestrów

00 65  Pierwszy Adres odczytywanego rejestru

00 01  Ilość odczytywanych rejestrów: X = 1

94 26  Suma kontrolna CRC

02       Adres urządzenia - ARM: $02

03       Odpowiedź dla Funkcji $03

02       Ilość zwracanych bajtów z rejestrów: 2

00 FA  Wartość Temperatury = 25oC

7C 07  Suma kontrolna CRC

modbus_connect.jpg

Procedura tworząca tabelę dekodowania danych MODBUS

procedure set_crc_table;
var
  crc_table: array [1 .. 512] of byte;

begin

  crc_table[1] := $00;
  crc_table[2] := $C1;
  crc_table[3] := $81;
  crc_table[4] := $40;
  crc_table[5] := $01;
  crc_table[6] := $C0;
  crc_table[7] := $80;
  crc_table[8] := $41;
  crc_table[9] := $01;
  crc_table[10] := $C0;
  crc_table[11] := $80;
  crc_table[12] := $41;
  crc_table[13] := $00;
  crc_table[14] := $C1;
  crc_table[15] := $81;
  crc_table[16] := $40;
  crc_table[17] := $01;
  crc_table[18] := $C0;
  crc_table[19] := $80;
  crc_table[20] := $41;
  crc_table[21] := $00;
  crc_table[22] := $C1;
  crc_table[23] := $81;
  crc_table[24] := $40;
  crc_table[25] := $00;
  crc_table[26] := $C1;
  crc_table[27] := $81;
  crc_table[28] := $40;
  crc_table[29] := $01;
  crc_table[30] := $C0;
  crc_table[31] := $80;
  crc_table[32] := $41;
  crc_table[33] := $01;
  crc_table[34] := $C0;
  crc_table[35] := $80;
  crc_table[36] := $41;
  crc_table[37] := $00;
  crc_table[38] := $C1;
  crc_table[39] := $81;
  crc_table[40] := $40;
  crc_table[41] := $00;
  crc_table[42] := $C1;
  crc_table[43] := $81;
  crc_table[44] := $40;
  crc_table[45] := $01;
  crc_table[46] := $C0;
  crc_table[47] := $80;
  crc_table[48] := $41;
  crc_table[49] := $00;
  crc_table[50] := $C1;
  crc_table[51] := $81;
  crc_table[52] := $40;
  crc_table[53] := $01;
  crc_table[54] := $C0;
  crc_table[55] := $80;
  crc_table[56] := $41;
  crc_table[57] := $01;
  crc_table[58] := $C0;
  crc_table[59] := $80;
  crc_table[60] := $41;
  crc_table[61] := $00;
  crc_table[62] := $C1;
  crc_table[63] := $81;
  crc_table[64] := $40;
  crc_table[65] := $01;
  crc_table[66] := $C0;
  crc_table[67] := $80;
  crc_table[68] := $41;
  crc_table[69] := $00;
  crc_table[70] := $C1;
  crc_table[71] := $81;
  crc_table[72] := $40;
  crc_table[73] := $00;
  crc_table[74] := $C1;
  crc_table[75] := $81;
  crc_table[76] := $40;
  crc_table[77] := $01;
  crc_table[78] := $C0;
  crc_table[79] := $80;
  crc_table[80] := $41;
  crc_table[81] := $00;
  crc_table[82] := $C1;
  crc_table[83] := $81;
  crc_table[84] := $40;
  crc_table[85] := $01;
  crc_table[86] := $C0;
  crc_table[87] := $80;
  crc_table[88] := $41;
  crc_table[89] := $01;
  crc_table[90] := $C0;
  crc_table[91] := $80;
  crc_table[92] := $41;
  crc_table[93] := $00;
  crc_table[94] := $C1;
  crc_table[95] := $81;
  crc_table[96] := $40;
  crc_table[97] := $00;
  crc_table[98] := $C1;
  crc_table[99] := $81;
  crc_table[100] := $40;
  crc_table[101] := $01;
  crc_table[102] := $C0;
  crc_table[103] := $80;
  crc_table[104] := $41;
  crc_table[105] := $01;
  crc_table[106] := $C0;
  crc_table[107] := $80;
  crc_table[108] := $41;
  crc_table[109] := $00;
  crc_table[110] := $C1;
  crc_table[111] := $81;
  crc_table[112] := $40;
  crc_table[113] := $01;
  crc_table[114] := $C0;
  crc_table[115] := $80;
  crc_table[116] := $41;
  crc_table[117] := $00;
  crc_table[118] := $C1;
  crc_table[119] := $81;
  crc_table[120] := $40;
  crc_table[121] := $00;
  crc_table[122] := $C1;
  crc_table[123] := $81;
  crc_table[124] := $40;
  crc_table[125] := $01;
  crc_table[126] := $C0;
  crc_table[127] := $80;
  crc_table[128] := $41;
  crc_table[129] := $01;
  crc_table[130] := $C0;
  crc_table[131] := $80;
  crc_table[132] := $41;
  crc_table[133] := $00;
  crc_table[134] := $C1;
  crc_table[135] := $81;
  crc_table[136] := $40;
  crc_table[137] := $00;
  crc_table[138] := $C1;
  crc_table[139] := $81;
  crc_table[140] := $40;
  crc_table[141] := $01;
  crc_table[142] := $C0;
  crc_table[143] := $80;
  crc_table[144] := $41;
  crc_table[145] := $00;
  crc_table[146] := $C1;
  crc_table[147] := $81;
  crc_table[148] := $40;
  crc_table[149] := $01;
  crc_table[150] := $C0;
  crc_table[151] := $80;
  crc_table[152] := $41;
  crc_table[153] := $01;
  crc_table[154] := $C0;
  crc_table[155] := $80;
  crc_table[156] := $41;
  crc_table[157] := $00;
  crc_table[158] := $C1;
  crc_table[159] := $81;
  crc_table[160] := $40;
  crc_table[161] := $00;
  crc_table[162] := $C1;
  crc_table[163] := $81;
  crc_table[164] := $40;
  crc_table[165] := $01;
  crc_table[166] := $C0;
  crc_table[167] := $80;
  crc_table[168] := $41;
  crc_table[169] := $01;
  crc_table[170] := $C0;
  crc_table[171] := $80;
  crc_table[172] := $41;
  crc_table[173] := $00;
  crc_table[174] := $C1;
  crc_table[175] := $81;
  crc_table[176] := $40;
  crc_table[177] := $01;
  crc_table[178] := $C0;
  crc_table[179] := $80;
  crc_table[180] := $41;
  crc_table[181] := $00;
  crc_table[182] := $C1;
  crc_table[183] := $81;
  crc_table[184] := $40;
  crc_table[185] := $00;
  crc_table[186] := $C1;
  crc_table[187] := $81;
  crc_table[188] := $40;
  crc_table[189] := $01;
  crc_table[190] := $C0;
  crc_table[191] := $80;
  crc_table[192] := $41;
  crc_table[193] := $00;
  crc_table[194] := $C1;
  crc_table[195] := $81;
  crc_table[196] := $40;
  crc_table[197] := $01;
  crc_table[198] := $C0;
  crc_table[199] := $80;
  crc_table[200] := $41;
  crc_table[201] := $01;
  crc_table[202] := $C0;
  crc_table[203] := $80;
  crc_table[204] := $41;
  crc_table[205] := $00;
  crc_table[206] := $C1;
  crc_table[207] := $81;
  crc_table[208] := $40;
  crc_table[209] := $01;
  crc_table[210] := $C0;
  crc_table[211] := $80;
  crc_table[212] := $41;
  crc_table[213] := $00;
  crc_table[214] := $C1;
  crc_table[215] := $81;
  crc_table[216] := $40;
  crc_table[217] := $00;
  crc_table[218] := $C1;
  crc_table[219] := $81;
  crc_table[220] := $40;
  crc_table[221] := $01;
  crc_table[222] := $C0;
  crc_table[223] := $80;
  crc_table[224] := $41;
  crc_table[225] := $01;
  crc_table[226] := $C0;
  crc_table[227] := $80;
  crc_table[228] := $41;
  crc_table[229] := $00;
  crc_table[230] := $C1;
  crc_table[231] := $81;
  crc_table[232] := $40;
  crc_table[233] := $00;
  crc_table[234] := $C1;
  crc_table[235] := $81;
  crc_table[236] := $40;
  crc_table[237] := $01;
  crc_table[238] := $C0;
  crc_table[239] := $80;
  crc_table[240] := $41;
  crc_table[241] := $00;
  crc_table[242] := $C1;
  crc_table[243] := $81;
  crc_table[244] := $40;
  crc_table[245] := $01;
  crc_table[246] := $C0;
  crc_table[247] := $80;
  crc_table[248] := $41;
  crc_table[249] := $01;
  crc_table[250] := $C0;
  crc_table[251] := $80;
  crc_table[252] := $41;
  crc_table[253] := $00;
  crc_table[254] := $C1;
  crc_table[255] := $81;
  crc_table[256] := $40;

  crc_table[257] := $00;
  crc_table[258] := $C0;
  crc_table[259] := $C1;
  crc_table[260] := $01;
  crc_table[261] := $C3;
  crc_table[262] := $03;
  crc_table[263] := $02;
  crc_table[264] := $C2;
  crc_table[265] := $C6;
  crc_table[266] := $06;
  crc_table[267] := $07;
  crc_table[268] := $C7;
  crc_table[269] := $05;
  crc_table[270] := $C5;
  crc_table[271] := $C4;
  crc_table[272] := $04;
  crc_table[273] := $CC;
  crc_table[274] := $0C;
  crc_table[275] := $0D;
  crc_table[276] := $CD;
  crc_table[277] := $0F;
  crc_table[278] := $CF;
  crc_table[279] := $CE;
  crc_table[280] := $0E;
  crc_table[281] := $0A;
  crc_table[282] := $CA;
  crc_table[283] := $CB;
  crc_table[284] := $0B;
  crc_table[285] := $C9;
  crc_table[286] := $09;
  crc_table[287] := $08;
  crc_table[288] := $C8;
  crc_table[289] := $D8;
  crc_table[290] := $18;
  crc_table[291] := $19;
  crc_table[292] := $D9;
  crc_table[293] := $1B;
  crc_table[294] := $DB;
  crc_table[295] := $DA;
  crc_table[296] := $1A;
  crc_table[297] := $1E;
  crc_table[298] := $DE;
  crc_table[299] := $DF;
  crc_table[300] := $1F;
  crc_table[301] := $DD;
  crc_table[302] := $1D;
  crc_table[303] := $1C;
  crc_table[304] := $DC;
  crc_table[305] := $14;
  crc_table[306] := $D4;
  crc_table[307] := $D5;
  crc_table[308] := $15;
  crc_table[309] := $D7;
  crc_table[310] := $17;
  crc_table[311] := $16;
  crc_table[312] := $D6;
  crc_table[313] := $D2;
  crc_table[314] := $12;
  crc_table[315] := $13;
  crc_table[316] := $D3;
  crc_table[317] := $11;
  crc_table[318] := $D1;
  crc_table[319] := $D0;
  crc_table[320] := $10;
  crc_table[321] := $F0;
  crc_table[322] := $30;
  crc_table[323] := $31;
  crc_table[324] := $F1;
  crc_table[325] := $33;
  crc_table[326] := $F3;
  crc_table[327] := $F2;
  crc_table[328] := $32;
  crc_table[329] := $36;
  crc_table[330] := $F6;
  crc_table[331] := $F7;
  crc_table[332] := $37;
  crc_table[333] := $F5;
  crc_table[334] := $35;
  crc_table[335] := $34;
  crc_table[336] := $F4;
  crc_table[337] := $3C;
  crc_table[338] := $FC;
  crc_table[339] := $FD;
  crc_table[340] := $3D;
  crc_table[341] := $FF;
  crc_table[342] := $3F;
  crc_table[343] := $3E;
  crc_table[344] := $FE;
  crc_table[345] := $FA;
  crc_table[346] := $3A;
  crc_table[347] := $3B;
  crc_table[348] := $FB;
  crc_table[349] := $39;
  crc_table[350] := $F9;
  crc_table[351] := $F8;
  crc_table[352] := $38;
  crc_table[353] := $28;
  crc_table[354] := $E8;
  crc_table[355] := $E9;
  crc_table[356] := $29;
  crc_table[357] := $EB;
  crc_table[358] := $2B;
  crc_table[359] := $2A;
  crc_table[360] := $EA;
  crc_table[361] := $EE;
  crc_table[362] := $2E;
  crc_table[363] := $2F;
  crc_table[364] := $EF;
  crc_table[365] := $2D;
  crc_table[366] := $ED;
  crc_table[367] := $EC;
  crc_table[368] := $2C;
  crc_table[369] := $E4;
  crc_table[370] := $24;
  crc_table[371] := $25;
  crc_table[372] := $E5;
  crc_table[373] := $27;
  crc_table[374] := $E7;
  crc_table[375] := $E6;
  crc_table[376] := $26;
  crc_table[377] := $22;
  crc_table[378] := $E2;
  crc_table[379] := $E3;
  crc_table[380] := $23;
  crc_table[381] := $E1;
  crc_table[382] := $21;
  crc_table[383] := $20;
  crc_table[384] := $E0;
  crc_table[385] := $A0;
  crc_table[386] := $60;
  crc_table[387] := $61;
  crc_table[388] := $A1;
  crc_table[389] := $63;
  crc_table[390] := $A3;
  crc_table[391] := $A2;
  crc_table[392] := $62;
  crc_table[393] := $66;
  crc_table[394] := $A6;
  crc_table[395] := $A7;
  crc_table[396] := $67;
  crc_table[397] := $A5;
  crc_table[398] := $65;
  crc_table[399] := $64;
  crc_table[400] := $A4;
  crc_table[401] := $6C;
  crc_table[402] := $AC;
  crc_table[403] := $AD;
  crc_table[404] := $6D;
  crc_table[405] := $AF;
  crc_table[406] := $6F;
  crc_table[407] := $6E;
  crc_table[408] := $AE;
  crc_table[409] := $AA;
  crc_table[410] := $6A;
  crc_table[411] := $6B;
  crc_table[412] := $AB;
  crc_table[413] := $69;
  crc_table[414] := $A9;
  crc_table[415] := $A8;
  crc_table[416] := $68;
  crc_table[417] := $78;
  crc_table[418] := $B8;
  crc_table[419] := $B9;
  crc_table[420] := $79;
  crc_table[421] := $BB;
  crc_table[422] := $7B;
  crc_table[423] := $7A;
  crc_table[424] := $BA;
  crc_table[425] := $BE;
  crc_table[426] := $7E;
  crc_table[427] := $7F;
  crc_table[428] := $BF;
  crc_table[429] := $7D;
  crc_table[430] := $BD;
  crc_table[431] := $BC;
  crc_table[432] := $7C;
  crc_table[433] := $B4;
  crc_table[434] := $74;
  crc_table[435] := $75;
  crc_table[436] := $B5;
  crc_table[437] := $77;
  crc_table[438] := $B7;
  crc_table[439] := $B6;
  crc_table[440] := $76;
  crc_table[441] := $72;
  crc_table[442] := $B2;
  crc_table[443] := $B3;
  crc_table[444] := $73;
  crc_table[445] := $B1;
  crc_table[446] := $71;
  crc_table[447] := $70;
  crc_table[448] := $B0;
  crc_table[449] := $50;
  crc_table[450] := $90;
  crc_table[451] := $91;
  crc_table[452] := $51;
  crc_table[453] := $93;
  crc_table[454] := $53;
  crc_table[455] := $52;
  crc_table[456] := $92;
  crc_table[457] := $96;
  crc_table[458] := $56;
  crc_table[459] := $57;
  crc_table[460] := $97;
  crc_table[461] := $55;
  crc_table[462] := $95;
  crc_table[463] := $94;
  crc_table[464] := $54;
  crc_table[465] := $9C;
  crc_table[466] := $5C;
  crc_table[467] := $5D;
  crc_table[468] := $9D;
  crc_table[469] := $5F;
  crc_table[470] := $9F;
  crc_table[471] := $9E;
  crc_table[472] := $5E;
  crc_table[473] := $5A;
  crc_table[474] := $9A;
  crc_table[475] := $9B;
  crc_table[476] := $5B;
  crc_table[477] := $99;
  crc_table[478] := $59;
  crc_table[479] := $58;
  crc_table[480] := $98;
  crc_table[481] := $88;
  crc_table[482] := $48;
  crc_table[483] := $49;
  crc_table[484] := $89;
  crc_table[485] := $4B;
  crc_table[486] := $8B;
  crc_table[487] := $8A;
  crc_table[488] := $4A;
  crc_table[489] := $4E;
  crc_table[490] := $8E;
  crc_table[491] := $8F;
  crc_table[492] := $4F;
  crc_table[493] := $8D;
  crc_table[494] := $4D;
  crc_table[495] := $4C;
  crc_table[496] := $8C;
  crc_table[497] := $44;
  crc_table[498] := $84;
  crc_table[499] := $85;
  crc_table[500] := $45;
  crc_table[501] := $87;
  crc_table[502] := $47;
  crc_table[503] := $46;
  crc_table[504] := $86;
  crc_table[505] := $82;
  crc_table[506] := $42;
  crc_table[507] := $43;
  crc_table[508] := $83;
  crc_table[509] := $41;
  crc_table[510] := $81;
  crc_table[511] := $80;
  crc_table[512] := $40;
end;

MBUS_params.png

Funkcja obliczająca CRC dla ramki MODBUS

function crc16(buf: array of byte; count: integer): word;
var
  i: integer;
  index: integer;
  crc_low: byte;
  crc_high: byte;
begin
  crc_low := $FF;
  crc_high := $FF;
  for i := 0 to count - 3 do
  begin
    index := crc_high xor buf[i];
    crc_high := crc_low xor crc_table[index + 1];
    crc_low := crc_table[index + 257];
  end;
  result := (crc_high * 256) + crc_low;
end;

Procedura zapisu danych przez sieć LAN za pomocą komponentu TidTCPclient

procedure TForm6.Send_data_outs(buf_data : array of byte; Sender : TObject);
var
  buf: array [1 .. 30] of byte;
  mbuf: TidBytes;
  crc: word;
  crc1: byte;
  crc2: byte;
  a, c: byte;
  i: Integer;
begin
  repeat
    for i := 1 to 10 do
      ModBusBuf.buf[i] := 0;
    ModBusFrame := false;
    for a := 0 to 23 do
      buf[a+1] := buf_Data[a];

    crc := modbus.crc16(buf, 26);
    crc1 := Hi(crc);
    crc2 := Lo(crc);
    buf[25] := crc1;
    buf[26] := crc2;
    c := 0;

    if Form8.IdTCPClient1.Connected then
    begin
      SetLength(mbuf, 26);
      for a := 1 to 26 do
        mbuf[a - 1] := buf[a];
      Form8.IdTCPClient1.Socket.Write(mbuf, 26);
      c := 0;
      repeat
        c := c + 1;
        ModBusFrame := false;
        Form1.Delay(ModBusTimeOut);
      until Form8.IdTCPClient1.Socket.CheckForDataOnSource(1000) or (c = 5);
      ModBusFrame := false;
      if (Form8.IdTCPClient1.Socket.InputBuffer.Size > 0) then
        Form1.LanRxChar(8, Sender);
    end;
  until ModBusFrame and (ModBusBuf.buf[2] = 16) and (ModBusBuf.buf[6] = $0A) or (c = 5);
end;

bottom of page