public static byte[] CreateCommandEx(FXSeriesControl control, FXSeriesCmd cmd, int addrFrom, int len, byte[] dataBytes) { string addrStr = Utils.FillStringWithChar(Convert.ToString(addrFrom, 16).ToUpper(), '0', 4, true); string lenStr = Utils.FillStringWithChar(Convert.ToString(len, 16).ToUpper(), '0', 2, true); if (control == FXSeriesControl.ENQ) return new byte[] { (byte)FXSeriesControl.ENQ }; else if (control == FXSeriesControl.STX) { byte[] cmdBytes = GetCmdByte(cmd); if (cmd == FXSeriesCmd.Read || cmd == FXSeriesCmd.ReadConfig || cmd == FXSeriesCmd.ReadProgram) return ReadCommand(addrStr, lenStr, cmdBytes); else if (cmd == FXSeriesCmd.Write || cmd == FXSeriesCmd.WriteConfig || cmd == FXSeriesCmd.WriteProgram) return WriteCommand(addrStr, lenStr, dataBytes, cmdBytes); else if (cmd == FXSeriesCmd.ON || cmd == FXSeriesCmd.OFF) return ForceOnOffCommand(cmd, addrStr); else throw new NotImplementedException(); } else throw new Exception("PC only can send command ENQ or STX"); }
private List<byte[]> CreateRegionCMDList(FXSeriesCmd cmd, int from, int len, string[] dataStrArray) { List<byte[]> cmdList = new List<byte[]>(); byte[] cmdBytes; int dataStrArrayIndex = 0; while (len > MAXCMDLEN) { len = len - MAXCMDLEN; Console.WriteLine(len); string[] tempMaxLen = null; if (dataStrArray != null) { tempMaxLen = new string[MAXCMDLEN]; Array.Copy(dataStrArray, dataStrArrayIndex, tempMaxLen, 0, MAXCMDLEN); } cmdBytes = FXCommander1.CreateCommand(FXSeriesControl.STX, cmd, from, MAXCMDLEN, tempMaxLen); from += MAXCMDLEN; dataStrArrayIndex += MAXCMDLEN; cmdList.Add(cmdBytes); } if (len > 0) { string[] tempLeft = null; if (dataStrArray != null) { tempLeft = new string[len]; Array.Copy(dataStrArray, dataStrArrayIndex, tempLeft, 0, len); } cmdBytes = FXCommander1.CreateCommand(FXSeriesControl.STX, cmd, from, len, tempLeft); cmdList.Add(cmdBytes); } return cmdList; }
/// <summary>生成强制On/Off命令 /// /// </summary> /// <param name="cmd"></param> /// <returns></returns> private static byte[] ForceOnOffCommand(FXSeriesCmd cmd, string addrStr) { byte[] result = new byte[9]; result[0] = 0x02; if (cmd == FXSeriesCmd.ON) result[1] = 0x37; else result[1] = 0x38; result[2] = (byte)addrStr[2]; result[3] = (byte)addrStr[3]; result[4] = (byte)addrStr[0]; result[5] = (byte)addrStr[1]; result[6] = 0x03; byte[] checkBytes = Utils.CheckSum(result, 1, 6, 2, HighOrLow.KeepLow); result[7] = checkBytes[0]; result[8] = checkBytes[1]; return result; }
/// <summary> /// get real command data from command type /// </summary> /// <param name="cmd">command type</param> /// <returns>real command data</returns> private static byte[] GetCmdByte(FXSeriesCmd cmd) { byte[] result = null; switch (cmd) { case FXSeriesCmd.ON: result = new byte[] { (byte)'7'}; break; case FXSeriesCmd.OFF: result = new byte[] { (byte)'8'}; break; case FXSeriesCmd.Read: result = new byte[] { (byte)'0'}; break; case FXSeriesCmd.Write: result = new byte[] { (byte)'1'}; break; case FXSeriesCmd.ReadConfig: result = new byte[] { (byte)'E',(byte)'0',(byte)'0'}; break; case FXSeriesCmd.WriteConfig: result = new byte[] { (byte)'E',(byte)'1',(byte)'0'}; break; case FXSeriesCmd.ReadProgram: result = new byte[] { (byte)'E',(byte)'0',(byte)'1'}; break; case FXSeriesCmd.WriteProgram: result = new byte[] { (byte)'E',(byte)'1',(byte)'1'}; break; } return result; }