using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; namespace demo { class CDX { public static Socket client; public static byte[] measureCmd = { 0x30, 0x02, 0x0D, 0x60 }; public static bool SendMsgToServer(byte[] msg, int timeOut = 0) { bool ret = false; try { client.SendTimeout = timeOut; int len = msg.Length; int retLen = client.Send(msg); if (len == retLen) { ret = true; } } catch (System.Exception ex) { throw new Exception(ex.Message); } return ret; } public static bool ReceiveMsg(byte[] buffer, int timeOut = 0) { bool ret = false; try { client.ReceiveTimeout = timeOut; int recvLen = client.Receive(buffer); if (recvLen > 0) { ret = true; } } catch (System.Exception ex) { throw new Exception(ex.Message); } return ret; } public static bool ReceiveMsg(ref double val, int timeOut = 0) { bool ret = false; byte[] buffer = new byte[10]; try { client.ReceiveTimeout = timeOut; int receiveCount = client.Receive(buffer); if (receiveCount > 0) { ret = true; byte[] temp = new byte[10]; //for (int i = 2; i <= receiveCount;i++ ) //{ //} temp = buffer.Skip(2).Take(receiveCount - 2).ToArray(); //buffer.CopyTo(temp, 2); string str = null; for (int i = 0; i < temp.Length; i++) { str += temp[i].ToString("X2"); } val = Convert.ToInt32(str, 16); val = val / 1000000; //byte[] tempBuffer = new byte[receiveCount]; //Buffer.BlockCopy(buffer, 2, tempBuffer, 2, receiveCount); //msg = Encoding.Default.GetString(tempBuffer); } } catch (System.Exception ex) { throw new Exception(ex.Message); } return ret; } } }