网口通信C#

网口通信

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;


namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int port = 10005;
            string host = "192.168.10.200";//服务器端ip地址

            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);

            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            clientSocket.Connect(ipe);

            //send message
            string sendStr = "FData,0";
            byte[] sendBytes = Encoding.ASCII.GetBytes(sendStr);
            clientSocket.Send(sendBytes);

            //receive message
            string recStr = "";
            byte[] recBytes = new byte[4096];
            int bytes = clientSocket.Receive(recBytes, recBytes.Length, 0);
            recStr += Encoding.ASCII.GetString(recBytes, 0, bytes);
            Console.WriteLine(recStr);
           // int leng = recStr.IndexOf("N");
            string[] sArray = recStr.Split('N');

            for (int i = 1; i < (sArray.Length-1); i++)
            {
                String data = sArray[i];
                  String data1 = data.Substring(20,13);
                  Decimal dData = 0.0M;
                  if (data1.Contains("E"))
                  {
                      dData = Decimal.Parse(data1.ToString(), System.Globalization.NumberStyles.Float);
                       
                  }
                  Console.WriteLine(dData);
        

            }
            Console.ReadLine();
            clientSocket.Close();
        }

       
    }
}






 

你可能感兴趣的:(网口通信C#)