How to use multiple network devices in CM3L?

General discussion about anything related to Ultibo.
gunwoo
Posts: 12
Joined: Fri Feb 02, 2018 1:15 am

Re: How to use multiple network devices in CM3L?

Postby gunwoo » Thu Feb 08, 2018 4:57 am

Test server, Network0, Network1, and Network2 are all connected to one router.

The subnet mask is '255.255.255.0'(All devices are the same.)

LAN9514 uses the MAC address of CM3L, and two LAN9512 uses each MAC address stored in the external EEPROM.

It works well when communicating in each function as follows.

Code: Select all

procedure TCPClient_0;
var
  SendData : String;
  SockAddr0:TSockAddr;
  i:integer;
begin
  ConsoleWindowWriteLn(WindowHandle,'');
  ConsoleWindowWriteLn(WindowHandle,'1st Connect');
  SendData := 'Hello There.'+#13;

  Winsock2TCPClient0.RemoteAddress := '192.168.0.65';
  Winsock2TCPClient0.RemotePort := 1460;
  Winsock2TCPClient0.BoundAddress := '192.168.0.63';
  Winsock2TCPClient0.Connect;

 
  for i:=0 to 10 do begin
    Winsock2TCPClient0.WriteData (@SendData[1], length (SendData));
    milliseconddelay(480);
  end;

end;


procedure TCPClient_1;
var
  SendData : String;
  SockAddr0:TSockAddr;
  i:integer;
begin
  ConsoleWindowWriteLn(WindowHandle,'');
  ConsoleWindowWriteLn(WindowHandle,'1st Connect');
  SendData := 'Hello There 2nd.'+#13;

  Winsock2TCPClient1.RemoteAddress := '192.168.0.65';
  Winsock2TCPClient1.RemotePort := 1470;
  Winsock2TCPClient1.BoundAddress := '192.168.0.66';
  Winsock2TCPClient1.Connect;

  ConsoleWindowWriteLn(WindowHandle,'Socket1 IP = ' + Winsock2TCPClient1.LocalAddress);

  for i:=0 to 10 do begin
    Winsock2TCPClient1.WriteData (@SendData[1], length (SendData));
    milliseconddelay(480);
  end;

end;

procedure TCPClient_2;
var
  SendData : String;
  SockAddr0:TSockAddr;
  i:integer;
begin
  ConsoleWindowWriteLn(WindowHandle,'');
  ConsoleWindowWriteLn(WindowHandle,'1st Connect');
  SendData := 'Hello There 3rd.'+#13;

  Winsock2TCPClient2.RemoteAddress := '192.168.0.65';
  Winsock2TCPClient2.RemotePort := 1480;
  Winsock2TCPClient2.BoundAddress := '192.168.0.67';
  Winsock2TCPClient2.Connect;

  ConsoleWindowWriteLn(WindowHandle,'Socket2 IP = ' + Winsock2TCPClient2.LocalAddress);
 
  for i:=0 to 10 do begin
    Winsock2TCPClient2.WriteData (@SendData[1], length (SendData));
    milliseconddelay(480);
  end;

end;
                       
User avatar
Ultibo
Site Admin
Posts: 2291
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: How to use multiple network devices in CM3L?

Postby Ultibo » Thu Feb 08, 2018 5:30 am

gunwoo wrote:Test server, Network0, Network1, and Network2 are all connected to one router.

The subnet mask is '255.255.255.0'(All devices are the same.)

This will be an invalid configuration, there will be 3 routes that point to 192.168.0.0/255.255.255.0

It may seem to work but we can't be sure exactly what will happen, most likely only one of the networks will be used and the other two will be idle.

gunwoo wrote:It works well when communicating in each function as follows.

Does it work if you don't set the BoundAddress (ie just let the IP stack decide the source address based on the destination)?

Can you add some extra checks to the code to determine where it fails, eg:

Code: Select all

 if not Winsock2TCPClient0.Connect then
  begin
    //Print an error etc
  end;

What is your goal in having three network devices on the same physical and logical network?
Ultibo.org | Make something amazing
https://ultibo.org
gunwoo
Posts: 12
Joined: Fri Feb 02, 2018 1:15 am

Re: How to use multiple network devices in CM3L?

Postby gunwoo » Thu Feb 08, 2018 6:34 am

Ultimately, I will assign a different static IP to each device.(Customer specified IP)

Three devices are used to enhance communication stability.

I tried to test it with one router at the development stage, but the test method seems to be wrong.

I will test with static IP.

If there is a difficult problem, I will ask another question.

Thank you
User avatar
Ultibo
Site Admin
Posts: 2291
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: How to use multiple network devices in CM3L?

Postby Ultibo » Thu Feb 08, 2018 6:46 am

gunwoo wrote:I tried to test it with one router at the development stage, but the test method seems to be wrong.

For testing you might be able to assign 3 different IPs to your router like:

Code: Select all

Test Server IP 1: 192.168.0.65
Test Server IP 2: 192.168.1.65
Test Server IP 3: 192.168.2.65

Even if it only has one interface, many Linux based devices will allow that.

And then use three different IPs on the Ultibo device like this:

Code: Select all

Client 0(Network0) IP : 192.168.0.63
Client 1(Network1) IP : 192.168.1.66
Client 2(Network2) IP : 192.168.2.67

That should work because multiple logical networks on the same physical network is allowed (you just can't go the other way and have multiple physical networks using the same logical network).

Let us know how you go.
Ultibo.org | Make something amazing
https://ultibo.org
gunwoo
Posts: 12
Joined: Fri Feb 02, 2018 1:15 am

Re: How to use multiple network devices in CM3L?

Postby gunwoo » Fri Feb 09, 2018 2:28 am

I did not understand the IP stack.

So I misunderstood what you explained.

I understand now, and I have successfully communicated with the test server.

Thank you. :)

I'm sorry, but there are a few more things I want to know.

I have seen post on how to use static IP in the forums.

This is a way to enter a static IP address by typing the command in cmdline.txt

Is there any way to specify a static IP in the source code?

If not in the source code, is there any other way?
User avatar
Ultibo
Site Admin
Posts: 2291
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: How to use multiple network devices in CM3L?

Postby Ultibo » Fri Feb 09, 2018 11:33 pm

gunwoo wrote:This is a way to enter a static IP address by typing the command in cmdline.txt

Is there any way to specify a static IP in the source code?

It should be possible to manage IP addreses in code, but the process will require a number of steps and there is no simple single function like "SetIPAddress".

I'm not sure if you realize but the settings for specifying a static IP from the command line do work with multiple networks, for example the first interface would be NETWORK0_IP_ADDRESS= and the second one would be NETWORK1_IP_ADDRESS= and so on, the same applies to the config, netmask and gateway settings.

If you can give us a couple of days we'll work out a set of steps to set or change the IP from code and post them here.
Ultibo.org | Make something amazing
https://ultibo.org
User avatar
Ultibo
Site Admin
Posts: 2291
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: How to use multiple network devices in CM3L?

Postby Ultibo » Wed Feb 14, 2018 12:42 am

gunwoo wrote:Is there any way to specify a static IP in the source code?

While testing the steps required to do this we discovered that a small piece of code was missing from the IP transport that is needed to make this work, we have now committed a change to add the missing functionality.

In order to use the example below you will need to update your RTL to the latest version as documented in the commit announcement.

Here is a simple SetIPAddress() function that allows changing the current IP address in code, the current address can be DHCP, static or any of the other supported configurations.

This example interacts directly with the underlying IP transport provider so it is extremely important that the locking and synchronization shown is not omitted, failure to correctly protect access to the configuration details will likely result in a crash or deadlock.

Code: Select all

uses
  SysUtils,
  Classes,
  GlobalSock,
  Network,
  Transport,
  IP;

function SetIPAddress(const AName,AAddress,ANetmask,AGateway:String):Boolean;
var
 Adapter:TNetworkAdapter;
 Transport:TNetworkTransport;
 TransportAdapter:TIPTransportAdapter;
begin
 {}
 Result:=False;

 if Length(AName) = 0 then Exit;
 if Length(AAddress) = 0 then Exit;
 if Length(ANetmask) = 0 then Exit;
 {Gateway can be empty}

 {Get Adapter}
 Adapter:=AdapterManager.GetAdapterByName(AName,True,NETWORK_LOCK_READ);
 if Adapter = nil then Exit;
 try
  {Get IP Transport}
  Transport:=TransportManager.GetTransportByType(AF_INET,PACKET_TYPE_IP,True,NETWORK_LOCK_READ);
  if Transport = nil then Exit;
  try
   {Get Transport Adapter (The connection between the Network device and the IP Transport)}
   TransportAdapter:=TIPTransportAdapter(Transport.GetAdapterByAdapter(Adapter,True,NETWORK_LOCK_WRITE));
   if TransportAdapter = nil then Exit;

   {Set the Adapter Status to Down}
   TransportAdapter.Adapter.Status:=ADAPTER_STATUS_DOWN;

   {Unlock Transport Adapter}
   TransportAdapter.WriterUnlock;

   WriteLn(AName + ' set to ADAPTER_STATUS_DOWN');

   WriteLn('Waiting until Transport Adapter is Unconfigured');

   {Lock Transport Adapter (Reader)}
   TransportAdapter.ReaderLock;

   {Wait for Transport Adapter to be Unconfigured}
   while TransportAdapter.Configured do
    begin
     {Wait}
     Sleep(500);
    end;

   {Unlock Transport Adapter}
   TransportAdapter.ReaderUnlock;

   WriteLn('Setting IP, Netmask and Gateway');

   {Lock Transport Adapter (Writer)}
   TransportAdapter.WriterLock;

   {Set the IP address, netmask and gateway}
   TransportAdapter.ConfigDefault:=CONFIG_TYPE_STATIC;
   TransportAdapter.ConfigAddress:=InAddrToHost(StringToInAddr(AAddress));
   TransportAdapter.ConfigNetmask:=InAddrToHost(StringToInAddr(ANetmask));
   if Length(AGateway) <> 0 then
    begin
     TransportAdapter.ConfigGateway:=InAddrToHost(StringToInAddr(AGateway));
    end;

   {Set the Adapter Status to Up}
   TransportAdapter.Adapter.Status:=ADAPTER_STATUS_UP;

   {Unlock Transport Adapter}
   TransportAdapter.WriterUnlock;
   
   WriteLn(AName + ' set to ADAPTER_STATUS_UP');

   Result:=True;
  finally
   Transport.ReaderUnlock;
  end;
 finally
  Adapter.ReaderUnlock;
 end;
end;


To use the function simply call it from your code like this, passing the name of the network device, the IP and netmask and (optionally) the gateway:

Code: Select all

 SetIPAddress('Network0','192.168.1.55','255.255.255.0','192.168.1.1');
Ultibo.org | Make something amazing
https://ultibo.org

Return to “Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest