none
TCP/Ip connection RRS feed

  • Question

  • Hi, I want to ask for help, advice. I'm trying to connect a device that is plugged into my computer and shows what the room temperature is. I try to connect to the device using C # and get the console at what temperature the device shows. I think the code I'm writing is not correct ... the device to which I want to connect and show the data on the console is Dixell http://thermofrostcryo.co.uk/refrigeration-products/products/dixell.html i use and modbus poll please advice. Thanks!

    I hope I do not write in the wrong place. Please excuse if so!

    This is my code:

    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using Modbus.Device;
    using System.Threading;

    namespace TestConnect1
    {
        public class ModBusConnection
        {
            static void Main(string[] args)
            {

                TcpClient modbus = new TcpClient("192.168.1.199", 502);
                NetworkStream n = modbus.GetStream();
                Console.WriteLine("Conected");
                string ch = Console.ReadLine();
                byte[] message = Encoding.Unicode.GetBytes(ch);
                modbus.Close();
                Console.ReadKey();

            }
        }
    }

    Sunday, March 25, 2018 11:24 AM

Answers

  • Hello theStaba,

    Try to refer the sample on the TcpClient Class. Do something changed as below.

          static void Main(string[] args)
            {
                TcpClient modbus = new TcpClient("192.168.1.199", 502);
                NetworkStream stream = modbus.GetStream();
    
                // Buffer to store the response bytes.
                var data = new Byte[256];
    
                // String to store the response ASCII representation.
                String responseData = String.Empty;
    
                // Read the first batch of the TcpServer response bytes.
                Int32 bytes = stream.Read(data, 0, data.Length);
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                Console.WriteLine("Received: {0}", responseData);
    
                stream.Close();
                modbus.Close();
                Console.ReadKey();
            }

    The above works well on my side with custom socket server. Of course you need to debug step by step under your running environment.

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Ping.L Monday, March 26, 2018 2:58 PM
    • Marked as answer by theStaba Tuesday, April 3, 2018 3:07 PM
    Monday, March 26, 2018 6:49 AM
    Moderator