123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- namespace SimensCNC
- {
- public class ETH_S7SimensLib
- {
- public static string cncIP;
- int receiveData;
- byte[] sendCommands;
- byte[] buffer;//发送报文容器
- byte[] datas;//接收报文容器
- static IAsyncResult connResult = null;
- Func<byte[], int, object> func;
- public ETH_S7SimensLib()
- {
- }
- public Socket socket { get; set; }
- /// <summary>
- /// 链接函数
- /// </summary>
- public bool ETH_S7SimensConnect(string adrip, ushort port, int overtime)
- {
- try
- {
- cncIP = adrip;
- IPAddress ip = IPAddress.Parse(adrip);
- int ports = Convert.ToInt32(port);
- socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- connResult = socket.BeginConnect(ip, ports, null, null);
- socket.ReceiveTimeout = 2000;
- Thread.Sleep(5000);
- connResult.AsyncWaitHandle.WaitOne(5, true); //等待2秒
- if (connResult.IsCompleted)
- {
- HandShark();
- return true;
- }
- else
- {
- return false;
- }
- }
- catch
- {
- return false; ;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 断开设备连接
- /// </summary>
- /// <param name="no">设备编号</param>
- public void ETH_S7SimensDisConnect(string no)
- {
- try
- {
- socket.Shutdown(SocketShutdown.Both);
- socket.Close();
- }
- catch
- {
- }
- finally
- {
- }
-
- }
- /// <summary>
- /// 三次握手
- /// </summary>
- void HandShark()
- {
- socket.Send(ETH_S7SimensCommands.FIRST_HAND_SHANK);
- buffer = new byte[1024];
- receiveData = socket.Receive(buffer);
- socket.Send(ETH_S7SimensCommands.SENCOND_HAND_SHARK);
- buffer = new byte[1024];
- receiveData = socket.Receive(buffer);
- socket.Send(ETH_S7SimensCommands.THREE_HAND_SHARK);
- buffer = new byte[1024];
- receiveData = socket.Receive(buffer);
- }
- /// <summary>
- /// 获取CNCIP
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetCNCIP(string no,ref object result)
- {
- if (cncIP != null)
- {
- result = cncIP;
- return true;
- }
- else
- {
- return false;
- };
- }
- /// <summary>
- /// 获取采集时间
- /// </summary>
- /// <returns></returns>
- public bool ETH_S7SimensCollectTime(ref object result)
- {
- if (socket.Connected == true)
- {
- result = DateTime.Now; ;
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 获取心跳包
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetKeepAlive(string no,ref object result)
- {
- if (connResult.IsCompleted == true)
- {
- result = true;
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 获取版本信息
- /// </summary>
- /// <param name="no"></param>
- public bool ETH_S7SimensGetVerVison(ref object result)
- {
- try
- {
- func = new Func<byte[], int, string>(AnalysisStrData);
- return GetData(ETH_S7SimensCommands.READ_VER_INFO, (int)DataTypeEnum.DATATYPE_STRING, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 定义机床指纹
- /// </summary>
- public bool ETH_S7SimensGetCNCID(ref object result)
- {
- try
- {
- func = new Func<byte[], int, string>(AnalysisStrData);
- return GetData(ETH_S7SimensCommands.CNC_ID, (int)DataTypeEnum.DATATYPE_STRING, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
-
- }
- /// <summary>
- /// 获取机床类型
- /// </summary>
- public bool ETH_S7SimensCNCType(ref object result)
- {
- try
- {
- func = new Func<byte[], int, string>(AnalysisStrData);
- return GetData(ETH_S7SimensCommands.CNC_TYPE, (int)DataTypeEnum.DATATYPE_STRING, func, ref result);
- }
- catch
- {
- return true;
- }
- finally
- {
- }
-
- }
- /// <summary>
- /// 操作模式
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetModeInfo(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalusisModeData);
- return GetData(ETH_S7SimensCommands.CNC_MODE, (int)DataTypeEnum.DATATYPE_INT, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 运行状态
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetStateInfo(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalusisStatusData);
- return GetData(ETH_S7SimensCommands.CNC_STATUS, (int)DataTypeEnum.DATATYPE_INT, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 获取加工件数
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetProductCounts(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalysisDoubleData);
- return GetData(ETH_S7SimensCommands.CNC_PRODUCTS, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 进给设定速度
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetSetFeedSpeed(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalysisDoubleData);
- return GetData(ETH_S7SimensCommands.CNC_FEEDSETSPEED, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 进给实际速度
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetActFeedSpeed(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalysisDoubleData);
- return GetData(ETH_S7SimensCommands.CNC_FEEDACTSPEED, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 主轴设定速度
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetSetSpSpeed(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalysisDoubleData);
- return GetData(ETH_S7SimensCommands.CNC_SPINDLESETSPEED, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 主轴实际速度
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetActSpSpeed(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalysisDoubleData);
- return GetData(ETH_S7SimensCommands.CNC_SPINDLEACTSPEED, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 主轴倍率
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetSFeed(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalysisDoubleData);
- return GetData(ETH_S7SimensCommands.CNC_SPINDRATE, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 进给倍率
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimenstGetFeedRate(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalysisDoubleData);
- return GetData(ETH_S7SimensCommands.CNC_FEEDRATE, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- ///循环时间
- /// </summary>
- /// <param name="socket"></param>
- public bool ETH_S7SimenstGetCycTime(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalysisDoubleData);
- return GetData(ETH_S7SimensCommands.RUN_TIME, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- ///剩余时间
- /// </summary>
- /// <param name="socket"></param>
- public bool ETH_S7SimenstGetRemTime(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalysisDoubleData);
- return GetData(ETH_S7SimensCommands.REMAIN_TIME, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 获取加工程序名称
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetPrgName(ref object result)
- {
- try
- {
- func = new Func<byte[], int, string>(AnalysisStrData);
- return GetData(ETH_S7SimensCommands.PROGRAM_NAME, (int)DataTypeEnum.DATATYPE_STRING, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 加工程序内容
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetPrgContent(ref object result)
- {
- try
- {
- func = new Func<byte[], int, string>(AnalysisStrData);
- return GetData(ETH_S7SimensCommands.CurrentPro, (int)DataTypeEnum.DATATYPE_STRING, func, ref result);
- }
- catch
- {
- return true;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 获取轴名称
- /// 同时是获取机床参数的方法
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetAxisName(ref object result)
- {
- try
- {
- func = new Func<byte[], int, string>(AnalysisStrData);
- return GetData(ETH_S7SimensCommands.AXIS_NAME, (int)DataTypeEnum.DATATYPE_STRING, func, ref result);
- }
- catch
- {
- return true;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 获取机械坐标
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetMacPos(ref object result)
- {
- try
- {
- List<object> pos = new List<object>();
- foreach (var s in ETH_S7SimensCommands.posflag)
- {
- func = new Func<byte[], int, object>(AnalysisDoubleData);
- ETH_S7SimensCommands.MACHINE_POS[26] = s;
- GetData(ETH_S7SimensCommands.MACHINE_POS, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
- pos.Add(result);
- }
- if (pos != null)
- {
- result = string.Join("/", pos);
- return true;
- }
- else
- {
- return false;
- }
- }
- catch
- {
- return false;
- }
- finally
- {
- }
-
- }
- /// <summary>
- /// 获取相对坐标,工件坐标
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetRelPos(ref object result)
- {
- try
- {
- List<object> pos = new List<object>();
- foreach (var s in ETH_S7SimensCommands.posflag)
- {
- func = new Func<byte[], int, object>(AnalysisDoubleData);
- ETH_S7SimensCommands.RELATIVELY_POS[26] = s;
- GetData(ETH_S7SimensCommands.RELATIVELY_POS, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
- pos.Add(result);
- }
- if (pos != null)
- {
- result = string.Join("/", pos);
- return true;
- }
- else
- {
- return false;
- }
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 获取剩余坐标
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetRemPos(ref object result)
- {
- try
- {
- List<object> pos = new List<object>();
- foreach (var s in ETH_S7SimensCommands.posflag)
- {
- func = new Func<byte[], int, object>(AnalysisDoubleData);
- ETH_S7SimensCommands.REMAIN_POS[26] = s;
- GetData(ETH_S7SimensCommands.REMAIN_POS, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
- pos.Add(result);
- }
- if (pos != null)
- {
- result = string.Join("/", pos);
- return true;
- }
- else
- {
- return false;
- }
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 获取刀具号T
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetToolNum(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalysisInt32Data);
- return GetData(ETH_S7SimensCommands.TOOL_NUMBER, (int)DataTypeEnum.DATATYPE_INT, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 获取报警号
- /// </summary>
- /// <param name="no"></param>
- /// <param name="result"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetAlarmMsgNum(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalysisInt32Data);
- return GetData(ETH_S7SimensCommands.CNC_ALARM_NUM, (int)DataTypeEnum.DATATYPE_INT, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 获取报警信息
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetAlarmMsg(byte numflag, ref object result)
- {
- try
- {
- ETH_S7SimensCommands.CNC_ALARM[26] = numflag;
- func = new Func<byte[], int, object>(AnalysisAlarm);
- return GetData(ETH_S7SimensCommands.CNC_ALARM, (int)DataTypeEnum.DATATYPE_INT, func, ref result);
- }catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 获取刀具半径补偿编号
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetDNum(ref object result)
- {
- func = new Func<byte[], int, object>(AnalysisInt32Data);
- return GetData(ETH_S7SimensCommands.TOOL_D_NUMBER, (int)DataTypeEnum.DATATYPE_INT, func,ref result);
- }
- /// <summary>
- /// 获取刀具长度补偿编号
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetHNum(ref object result)
- {
- func = new Func<byte[], int, object>(AnalysisInt32Data);
- return GetData(ETH_S7SimensCommands.TOOL_H_NUMBER, (int)DataTypeEnum.DATATYPE_INT, func, ref result);
- }
- /// <summary>
- /// 获取长度补偿X
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetLengthX(ref object result)
- {
- func = new Func<byte[], int, object>(AnalysisDoubleData);
- return GetData(ETH_S7SimensCommands.TOOL_X_LENGTH, (int)DataTypeEnum.DATATYPE_DOUBLE, func,ref result);
- }
- /// <summary>
- /// 获取长度补偿Z
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetLengthZ(ref object result)
- {
- func = new Func<byte[], int, object>(AnalysisDoubleData);
- return GetData(ETH_S7SimensCommands.TOOL_Z_LENGTH, (int)DataTypeEnum.DATATYPE_DOUBLE, func,ref result);
- }
- /// <summary>
- /// 获取刀具磨损半径
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetRadius(ref object result)
- {
- func = new Func<byte[], int, object>(AnalysisDoubleData);
- return GetData(ETH_S7SimensCommands.TOOL_RADIU, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
- }
- /// <summary>
- /// 获取刀沿位置
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetEDG(ref object result)
- {
- func = new Func<byte[], int, object>(AnalysisDoubleData);
- return GetData(ETH_S7SimensCommands.TOOL_EDG, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
- }
- /// <summary>
- /// 获取母线电压
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetVoltage(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalysisFloatData);
- return GetData(ETH_S7SimensCommands.DRIVE_VOLTAGE, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 获取电机功率
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetLoad(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalysisFloatData);
- return GetData(ETH_S7SimensCommands.DRIVER_LOAD1, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- public bool ETH_S7SimensGetSPLoad1(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalysisFloatData);
- return GetData(ETH_S7SimensCommands.DRIVER_SPLOAD, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- public bool ETH_S7SimensGetSPLoad2(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalysisFloatData);
- return GetData(ETH_S7SimensCommands.DRIVER_SPLOAD2, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 获取电机温度
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetTemper(ref object result,byte flag)
- {
- try
- {
- ETH_S7SimensCommands.DRIVER_TEMPER[22] = flag;
- func = new Func<byte[], int, object>(AnalysisFloatData);
- return GetData(ETH_S7SimensCommands.DRIVER_TEMPER, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 获取电流
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public bool ETH_S7SimensGetCurrent(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalysisFloatData);
- return GetData(ETH_S7SimensCommands.DRIVER_CURRENT, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- //public static byte[] R_Driver ={
- // 0x03, 0x00, 0x00, 0x1d, //12+17=29
- // 0x02, 0xf0, 0x80, 0x32, 0x01,
- // 0x00, 0x00, 0x00, 0x14,
- // 0x00, 0x0c, //10+2
- // 0x00, 0x00,
- // 0x04,
- // 0x01,
- // 0x12, 0x08, 0x82, 0xa1, 0x00, 0x25, 0x00, 0x01, 0x82, 0x01,//倒数第三位R驱动器下标,如R37[1],0X03=R37[2];倒数第五位为地址,如25为十进制37,第七位为轴,依次递增切换轴
- // 0x03, 0x00, 0x00, 0x07, 0x02, 0xf0, 0x00
- //};
- /// <summary>
- /// 获取驱动器
- /// </summary>
- /// <param name="no">无意义</param>
- /// <param name="adrFlag"></param>
- /// <param name="axisFlag">byte[] aixsflag = new byte[] { 0xa1, 0xa2, 0xa3 };//s,,x,y轴标识 </param>
- /// <param name="flag">下标标识</param>
- /// <param name="result"></param>
- /// <returns></returns>】
- public bool ETH_S7SimensGetRDriver(byte adrFlag, byte axisFlag,byte flag, ref object result)
- {
- try
- {
- ETH_S7SimensCommands.R_Driver[24] = adrFlag;
- ETH_S7SimensCommands.R_Driver[22] = axisFlag;
- ETH_S7SimensCommands.R_Driver[26] = flag;
- func = new Func<byte[], int, object>(AnalysisFloatData);
- return GetData(ETH_S7SimensCommands.R_Driver, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
- }catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 读取R变量
- /// </summary>
- /// <returns></returns>
- public bool ETH_S7SimensGetRValue(byte[] flag, ref object result)
- {
- try
- {
- ETH_S7SimensCommands.CNC_READ_R[26] = flag[0];
- ETH_S7SimensCommands.CNC_READ_R[25] = flag[1];
- func = new Func<byte[], int, object>(AnalysisDoubleData);
- return GetData(ETH_S7SimensCommands.CNC_READ_R, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
- }
- catch
- {
- return true;
- }
- finally
- {
- }
- }
- bool GetData(byte[] byteMessage, int dataType,Func<byte[],int,object> func,ref object result)
- {
- socket.Send(byteMessage);
- Thread.Sleep(10);
- buffer = new byte[1024];
- receiveData = socket.Receive(buffer);
-
- //监听到消息
- if (receiveData > 0)
- {
-
- result= func(buffer,receiveData) ;
- return true;
- }
- else
- {
- return false;
- }
- }
-
- /// <summary>
- /// 解析字符串数据
- /// </summary>
- /// <param name="byteMessage"></param>
- /// <param name="length"></param>
- /// <returns></returns>
- string AnalysisStrData(byte[] byteMessage, int length)
- {
-
- datas = new byte[length] ;
- Array.Copy(byteMessage, datas, length);
- var ss=BitConverter.ToString(datas);
- var s= (Encoding.GetEncoding("UTF-8").GetString(datas.Skip(25).ToArray())).Replace("\0", "");
- if (s=="")
- {
- return "未加工";
- }
- else
- {
- return s;
- }
-
- }
- public bool ETH_S7SimensGetTemper_a1(ref object result)
- {
- try
- {
- func = new Func<byte[], int, object>(AnalysisFloatData);
- return GetData(ETH_S7SimensCommands.DRIVER_TEMPER, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
- }
- catch
- {
- return false;
- }
- finally
- {
- }
- }
- /// <summary>
- /// 解析操作模式
- /// </summary>
- /// <param name="byteMessage"></param>
- /// <param name="length"></param>
- /// <returns></returns>
- object AnalusisModeData(byte[] byteMessage, int length)
- {
- try
- {
- datas = new byte[length];
- Array.Copy(byteMessage, datas, length);
- if (datas[24] == 0x02)
- {
- if (datas[31] == 0x00)
- {
- if (datas[25] == 0x00)
- {
- return SimensMode.JOG;
- }
- else if (datas[25] == 0x01)
- {
- return SimensMode.MDA;
- }
- else if (datas[25] == 0x02)
- {
- return SimensMode.AUTO;
- }
- else
- {
- return SimensMode.OTHER;
- }
- }
- else if (datas[31] == 0x03)
- {
- return SimensMode.REFPOINT;
- }
- else
- {
- return SimensMode.OTHER;
- }
- }
- else
- {
- return SimensMode.OTHER;
- }
- }
- catch
- {
- return SimensMode.OTHER;
- }
- }
- /// <summary>
- /// 运行状态
- /// </summary>
- /// <param name="byteMessage"></param>
- /// <param name="length"></param>
- /// <returns></returns>
- object AnalusisStatusData(byte[] byteMessage, int length)
- {
- try
- {
- datas = new byte[length];
- Array.Copy(byteMessage, datas, length);
- if (datas[24] == 0x02)
- {
- if ((datas[25] == 0x00) && (datas[31] == 0x05)) return SimensStatus.RESET;
- else if ((datas[25] == 0x02) && (datas[31] == 0x02)) return SimensStatus.STOP;
- else if ((datas[25] == 0x01) && (datas[31] == 0x03)) return SimensStatus.START;
- else if ((datas[25] == 0x01) && (datas[31] == 0x05)) return SimensStatus.SPENDLE_CW_CCW;
- else return SimensStatus.OTHER;
- }
- else
- {
- return SimensStatus.OTHER;
- }
- }
- catch
- {
- return SimensStatus.OTHER;
- }
- }
- /// <summary>
- /// 解析小端序float
- /// </summary>
- /// <param name="byteMessage"></param>
- /// <param name="length"></param>
- /// <returns></returns>
- object AnalysisFloatData(byte[] byteMessage, int length)
- {
- try
- {
- datas = new byte[length];
- Array.Copy(byteMessage, datas, length);
- //Console.WriteLine(BitConverter.ToString(datas));
- var value = BitConverter.ToSingle(datas.Skip(25).Reverse().ToArray(), 0);
- return value;
- }
- catch(Exception ex)
- {
- return ((int)DataTypeEnum.DATATYPE_ERRO);
- }
- }
- /// <summary>
- /// 解析double
- /// </summary>
- /// <param name="byteMessage"></param>
- /// <param name="length"></param>
- /// <returns></returns>
- object AnalysisDoubleData(byte[] byteMessage, int length)
- {
- object value = null;
- try
- {
- datas = new byte[length];
- Array.Copy(byteMessage, datas, length);;
- if (datas[3] == 33)
- {
- value = BitConverter.ToDouble(datas.Skip(25).Take(8).ToArray(), 0);
- }
- else
- {
- value = BitConverter.ToDouble(datas.Skip(0).ToArray(), 0);
- }
- return value;
- }
- catch (Exception ex)
- {
- return ((int)DataTypeEnum.DATATYPE_ERRO);
- }
- }
- /// <summary>
- /// 解析整型数据
- /// </summary>
- object AnalysisInt32Data(byte[] byteMessage, int length)
- {
- try
- {
- datas = new byte[length];
- Array.Copy(byteMessage, datas, length);
- var ss = BitConverter.ToString(datas);
- var s = (Encoding.GetEncoding("UTF-8").GetString(datas.Skip(25).ToArray())).Replace("\0", "");
- var value = BitConverter.ToInt16(datas.Skip(25).Take(2).ToArray(), 0);
-
- return value;
- }
- catch (Exception ex)
- {
- return (int)DataTypeEnum.DATATYPE_ERRO;
- }
- }
- /// <summary>
- /// 解析报警号
- /// </summary>
- object AnalysisAlarm(byte[] byteMessage, int length)
- {
- try
- {
- datas = new byte[length];
- Array.Copy(byteMessage, datas, length);
- Console.WriteLine(BitConverter.ToString(datas));
- var s = datas.Skip(25).Take(4).ToArray();
- var value = BitConverter.ToInt32(s, 0);
- return value;
- }
- catch (Exception ex)
- {
- return (int)DataTypeEnum.DATATYPE_ERRO;
- }
- }
- object AnalysisWorkSystem(byte[] byteMessage, int length)
- {
- try
- {
- datas = new byte[length];
- Array.Copy(byteMessage, datas, length);
- var s = datas.Skip(39).Take(2).ToArray();
- var value = BitConverter.ToInt16(s, 0);
- return value;
- }
- catch (Exception ex)
- {
- return (int)DataTypeEnum.DATATYPE_ERRO;
- }
- }
- }
- }
|