Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello i have a code:


SQL
public class Network
   {
       public Socket read;
       public IPEndPoint ie;

       public Network(string ip)
       {
           read = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
           ie = new IPEndPoint(IPAddress.Parse(File.ReadAllText("c:\\ipaddress.txt")), 502);
       }


       public bool InitConnection()
       {
           read.Connect(ie);
           return read.Connected;

       }


       public int send(byte[] data)
       {
           return read.Send(data);

       }

       public byte[] receive()
       {
           byte[] data = new byte[41];
           int num = read.Receive(data);
           return data;

       }

       public void close_connection()
       {
           //read.Close();
           read.Shutdown(SocketShutdown.Both);


       }

       public void refreshSocket()
       {
           read.Shutdown(SocketShutdown.Both);

       }


   }



- The above class is used to establish socket connection with a device.
- In the main class:

SCENARIO1:
C#
static void main()
   {
  byte[] data = new data[]{0x01,0x02,0x44,0x34,0x23,0x11};
  Networking net = new Networking();
 if(net.InitConnection())
    {
     net.send(data);
     net.receive();
    }
        ////Please note that i have used all the data from the Networking class, while doing so i am receiving  correct data in the receiving buffer. In short every thing works fine.

   }

SCENARIO2:
C#
static void main()
   {
  byte[] data = new data[]{0x01,0x02,0x44,0x34,0x23,0x11};
  byte[] ret = new ret[41];   
 Socket read = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    read.Connect("172.16.3.230", 502);
    read.Send(data);
    read.Receive(ret);
  ////please note here i am explicitly sending the data over the socket i.e directly connecting not through any class. But this time i get an Error. If i do the below thing : 
  foreach(byte b in ret)
  Console.WriteLine(b.ToString("x2"));  

  ///Here the result shows that the data which i receive into the ret buffer is shifted by one position to the right.
  ///for example if received data is : 01 02 03 04, then after shifting: 00 01 02 03.
  /// I have lost the last byte.
 
 /// I am communicating with a PLC device and reading its holding registers. This problem doesnt occur if i read the coils of the PLC device.        


   }


Thanks,
- Rahul
Posted
v2
Comments
TrushnaK 10-Jan-14 6:55am    
and question is?
Timberbird 10-Jan-14 7:45am    
Have you tried to use any traffic analyzer to see if actual data sent/received is equal in both cases?
Rahul VB 11-Jan-14 2:24am    
yes, i have used ethreal to capture the packets. There i saw that the response is perfectly fine.

1 solution

Instead of trying to roll your own, just use NModbus[^]. I use it quite often to communicate with Modbus PLC's and it works very well.
 
Share this answer
 
Comments
Rahul VB 11-Jan-14 2:30am    
sir, yes thanks i downloaded that, and i am viewing the source code, but i just have one doubt: why is the operation improving if i define all the operation of the socket in a class, why can i not use the socket's send/receive method directly to send data packets directly?i hope i am not nagging but, it is an astonishing fact.

Thanks a lot
- Rahul

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900