示例#1
0
        public static void AcceptCallback(IAsyncResult ar)
        {
            // Signal the main thread to continue.
            allDone.Set();

            // Get the socket that handles the client request.
            Socket listener = (Socket)ar.AsyncState;
            Socket handler  = listener.EndAccept(ar);

            Console.WriteLine($"socket conncetion to {handler.RemoteEndPoint} has established");
            // Create the state object.


            var modbusClient = new ModbusClient(handler);

            modbusClient.UnitIdentifier = Convert.ToByte(0);
            int unitId = modbusClient.ReportSlaveID();

            Console.WriteLine($"{handler.RemoteEndPoint} has UnitId:  " + unitId.ToString());

            try
            {
                obj.Where(a => a.ID == unitId).First().modbusClient = modbusClient;
                //for (int j = 1; j == 1; j++)
                //{


                //        obj[unitnum].modbusClient = modbusClient; unitnum++;
                //        // modbus.UnitIdentifier = Convert.ToByte(j);
                //    //    int[] serverResponse = modbusClient.ReadHoldingRegisters(1, 10);


                //    //for (int i = 0; i < serverResponse.Length; i++)
                //    //{
                //    //    Console.WriteLine($"data from {handler.RemoteEndPoint} recieved: {serverResponse[i]} ");
                //    //}

                //    Thread.Sleep(500);

                //}

                //    modbus.ReadHoldingRegisters(1, 10);
            }
            catch
            {
                handler.Close();
                //unitnum--;
                Console.WriteLine("AcceptCallback Error");
            }
        }
示例#2
0
    public static void AcceptCallback(IAsyncResult ar)
    {
        // Signal the main thread to continue.
        allDone.Set();

        // Get the socket that handles the client request.
        Socket listener = (Socket)ar.AsyncState;
        Socket handler  = listener.EndAccept(ar);

        Console.WriteLine($"socket conncetion to {handler.RemoteEndPoint} has established");
        // Create the state object.
        StateObject state = new StateObject();

        state.workSocket = handler;
        modbus           = new ModbusClient(handler);
        for (int j = 1; j == 1; j++)
        {
            //modbus.UnitIdentifier = Convert.ToByte(1);
            int unitId = modbus.ReportSlaveID();
            //Thread.Sleep(1000);
            Console.WriteLine($"{handler.RemoteEndPoint} has UnitId:  " + modbus.UnitIdentifier.ToString());
            //modbus.UnitIdentifier = unitId;
            try
            {
                int[] serverResponse = modbus.ReadHoldingRegisters(1, 10);

                for (int i = 0; i < serverResponse.Length; i++)
                {
                    Console.WriteLine($"data from {handler.RemoteEndPoint} recieved: {serverResponse[i]} ");
                }

                Thread.Sleep(2000);
                //    modbus.ReadHoldingRegisters(1, 10);
            }
            catch
            { Console.WriteLine("time out in  stream.Read"); }
        }
    }