Receiving data via TINA

  • 1
  • 2
  • 3
  • Page 2 of 3
nwrightson
Benutzer
Avatar
Gender:
Location: Newcastle
Age: 62
Homepage: nweha.homeserver.c…
Posts: 362
Registered: 08 / 2003
Subject:

Re: Receiving data via TINA

 · 
Posted: 31.07.2007 - 02:05  ·  #9
My understanding of UDP is that messages can be sent in a broadcast format as well as specifically to IP address.

Can we detect these two different types of inbound messages?
I.e. a) Flag to say it was a broadcast
b) address of sender
rh
Administrator
Avatar
Gender:
Location: Germany
Age: 24
Homepage: e-lab.de
Posts: 5558
Registered: 03 / 2002
Subject:

Re: Receiving data via TINA

 · 
Posted: 31.07.2007 - 11:43  ·  #10
Hello Neil,

the received byte count can be found here:
socket^.PacketRecInfo.RecLen

the sender IP address is here:
socket^.PacketRecInfo.PeerIP

Because I didn't use Broadcasts until now I'm not sure if there is a RxFlag for
Broadcasts.
But Broadcasts can be identified by their PeerIP consisting of all $FF

rolf
nwrightson
Benutzer
Avatar
Gender:
Location: Newcastle
Age: 62
Homepage: nweha.homeserver.c…
Posts: 362
Registered: 08 / 2003
Subject:

Re: Receiving data via TINA

 · 
Posted: 31.07.2007 - 12:12  ·  #11
Hi Rolf,
Sorry to be a pain with all of the questions.
Thanks for the feedback.
Hopefully it will all be working tomorrow.
AV
 
Avatar
 
Subject:

Re: Receiving data via TINA

 · 
Posted: 26.11.2012 - 18:06  ·  #12
This topic is 5 years old, but there is still a lack of documentation.

I want to receive UDP packets by TINA and read the profi driver manual and the example AVR TINA_ENC_UDP. More specific information (about my task) I couldn't find in the forum and internet.

In general the AVR example is nice. Unfortunately it doesn't tell how to read out the received data in detail.

Configuration of example:
Code

  //Construct the first Socket  (xUDP)
  UDP := TinaCreateSocket;
  With UDP^ do
    Protocol:= pxUDP;
    LocalPort:= 8000;
    RemotePort:= 1000;
    RemoteHost:= IP;
    PacketRecInfo.BufferPtr:= @MyBuf;
    PacketRecInfo.BufferLen:= SizeOf(MyBuf);
  endwith;


Receiving code of example:
Code

    //Socket UDP
    if TinaPacketReceived(UDP) then
      //We receive a packet @Socket UDP
      //We do nothing so dont block the Socket with trash! Lets resume !
      TinaResumeReceive(UDP);
    endif;


I suppose the received data is stored inside of PacketRecInfo.BufferPtr.


Questions:

1.
How to read out the buffer?
I suppose the content is a Byte array with the length BufferLen, which I have to convert to my wanted values.
Great would be an example how to read 4 bytes from the buffer (which I can convert to a Float type).

2.
What happens if the micro controller received 2 or more packets?
Is there only the next packet content in BufferPtr or the content of all received packets?


What I want:

For my application I want to receive UPD packets with a byte list, which represents a "frame" of several values (Bytes, Floats and Integers). After reading the Byte array, I want to convert sub-arrays to the related types.
rh
Administrator
Avatar
Gender:
Location: Germany
Age: 24
Homepage: e-lab.de
Posts: 5558
Registered: 03 / 2002
Subject:

Re: Receiving data via TINA

 · 
Posted: 26.11.2012 - 18:38  ·  #13
Hello AV,

yes, BufferPtr points to the received data.
Profi Manual pg. 121:
Code
Type
TtinaPacketReceive = Record
   PeerIP : tIPaddr;
   PeerPort : Word;
   RecLen : Word;
   BufferPtr : Pointer;
   BufferLen : Word;
end;


If data is received then "RecLen" contains the byte count of the reception.
Data must be read out with
Code
x:= PacketRecInfo.RecLen;
while x > 0 do
  d:= PacketRecInfo.BufferPtr^;
  dec(x);
endwhile;
TinaResumeReceive(socket); 

Of course "d" can be another pointer etc.
The socket is blocked until you have re-enabled it with:
TinaResumeReceive(socket);

rolf
AV
 
Avatar
 
Subject:

Re: Receiving data via TINA

 · 
Posted: 27.11.2012 - 17:12  ·  #14
Thanks for your answer (-:

Code
d:= PacketRecInfo.BufferPtr^; 

What happens by this line of code?

Does it read one Byte from the buffer and moves the pointer to the next position automatically?

In this case I hope I can convert the encoded value like this:
Code
FUNCTION GetFloatFromBuffer(var pRecInfo :tTinaPacketReceive) :Float;
var
  flo    :Float;
  bytes  :Array[0..3] of Byte;
begin
  bytes[0] := Byte(pRecInfo.buffPtr^);
  bytes[1] := pRecInfo.buffPtr^;
  bytes[2] := pRecInfo.buffPtr^;
  bytes[3] := pRecInfo.buffPtr^;
  flo := Float(bytes);
  return(flo);
end;


FUNCTION GetIntegerFromBuffer(var pRecInfo :tTinaPacketReceive) :Integer;
var
  itg    :Integer;
  bytes  :Array[0..1] of Byte;
begin
  bytes[0] := pRecInfo.buffPtr^;
  bytes[1] := pRecInfo.buffPtr^;
  flo := Integer(bytes);
  return(itg);
end;


And these helper functions I can call like this:
Code
if TinaPacketReceived(udp) then
  // parse the data of the UDP packet
  myByte := PacketRecInfo.BuffPtr^;
  myFloat := GetFloatFromBuffer(var PacketRecInfo);
  myInteger := GetIntegerFromBuffer(var PacketRecInfo);
endif;
rh
Administrator
Avatar
Gender:
Location: Germany
Age: 24
Homepage: e-lab.de
Posts: 5558
Registered: 03 / 2002
Subject:

Re: Receiving data via TINA

 · 
Posted: 27.11.2012 - 21:08  ·  #15
Hello AV,

pointers are never automatically incremented. If you want this then use:
d:= PacketRecInfo.BufferPtr^++; // auto increment after transfer
Also using an array for the destination introduces an overhead for array indexes.
Use this:
Code
var
  itg : integer;
  b0[@itg] : byte;
  b1[@itg+1] : byte;
begin
  b0:= pRecInfo.buffPtr^++;  // low byte of itg
  b1:= pRecInfo.buffPtr^++;  // high byte of itg

reading a float is similar.
rolf
AV
 
Avatar
 
Subject:

Re: Receiving data via TINA

 · 
Posted: 03.12.2012 - 12:29  ·  #16
Hello Mr. Hofmann,

thanks again for your notes (-:

Now I wrote my parser functions in this way:
Code

FUNCTION GetIntegerFromBuffer(var pRecInfo :tTinaPacketReceive) :Integer;
var
  itg           :Integer;
  byte0[@itg]   :Byte;
  byte1[@itg+1] :Byte;
begin
  byte0 := pRecInfo.buffPtr^++;  // low byte of itg
  byte1 := pRecInfo.buffPtr^++;  // high byte of itg
  return(itg);
end;


FUNCTION GetFloatFromBuffer(var pRecInfo :tTinaPacketReceive) :Float;
//FUNCTION GetFloatFromBuffer(var buffPtr :Pointer) :Float;
var
  flo           :Float;
  byte0[@flo]   :Byte;
  byte1[@flo+1] :Byte;
  byte2[@flo+2] :Byte;
  byte3[@flo+3] :Byte;
begin
  byte0 := pRecInfo.buffPtr^++; // lowest byte of float
  byte1 := pRecInfo.buffPtr^++;
  byte2 := pRecInfo.buffPtr^++;
  byte3 := pRecInfo.buffPtr^++; // highest byte of float
  return(flo);
end;


(Currently I can't test it, because I still have trouble with TINA, which I want to use in an additional file/unit.)



For security reasons the last Byte of my UDP packets contains value 255.
So I can perform a quick check for the expected length.

How shall I read the last Byte of the packet?

Code
// Sicherheitsprüfung (letztes Byte im UDP-Paket muß 255 sein)
if PacketRecInfo.BuffPtr^++ <> 255
  // ... 
end;


Or just write BuffPtr^ without ++?
Reason: Perhaps the ++ could create an exception, when the buffer size is equal to the packet size.



Quote by rh
pointers are never automatically incremented.

I asked this, because you wrote this code, which doesn't increment the pointer.
Additionally I remember a discussion about a C++ line of code, which increases automatically by reading.
Code

while x > 0 do
  d:= PacketRecInfo.BufferPtr^;
  dec(x);
endwhile; 





Quote by rh
The socket is blocked until you have re-enabled it

Do you mean, the receiving is blocked directly after the first packet?
So there would be never more than 1 packet inside of the buffer.
Is it right?
(In the manual of the profi driver there is no information about such details.)

Sincerely yours
Andreas
  • 1
  • 2
  • 3
  • Page 2 of 3
Selected quotes for multi-quoting:   0

Registered users in this topic

Currently no registered users in this section

The statistic shows who was online during the last 5 minutes. Updated every 90 seconds.
MySQL Queries: 15 · Cache Hits: 14   132   146 · Page-Gen-Time: 0.039733s · Memory Usage: 2 MB · GZIP: on · Viewport: SMXL-HiDPI