public ScribblerCommand(ScribblerHelper.Commands command, byte data) { Data = new byte[ScribblerHelper.CommandSize(command) - 1]; CommandType = (byte)command; Data[0] = data; HasEcho = ScribblerHelper.HasEcho(command); ResponseLength = ScribblerHelper.ReturnSize(command); }
/// <summary> /// For setAllLEDs command /// </summary> public ScribblerCommand(ScribblerHelper.Commands command, bool left, bool center, bool right) { Data = new byte[ScribblerHelper.CommandSize(command) - 1]; CommandType = (byte)command; Data[0] = left ? (byte)1 : (byte)0; Data[1] = center ? (byte)1 : (byte)0; Data[2] = right ? (byte)1 : (byte)0; HasEcho = ScribblerHelper.HasEcho(command); ResponseLength = ScribblerHelper.ReturnSize(command); }
/// <summary> /// for speaker commands /// </summary> /// <param name="command"></param> /// <param name="duration"></param> /// <param name="frequency1"></param> /// <param name="frequency2"></param> public ScribblerCommand(ScribblerHelper.Commands command, int duration, int frequency1, int frequency2) { Data = new byte[ScribblerHelper.CommandSize(command) - 1]; CommandType = (byte)command; Data[0] = (byte)(duration >> 8); Data[1] = (byte)duration; Data[2] = (byte)(frequency1 >> 8); Data[3] = (byte)frequency1; Data[4] = (byte)(frequency2 >> 8); Data[5] = (byte)frequency2; HasEcho = ScribblerHelper.HasEcho(command); ResponseLength = ScribblerHelper.ReturnSize(command); }
/// <summary> /// For setName /// </summary> /// <param name="command"></param> /// <param name="data"></param> public ScribblerCommand(ScribblerHelper.Commands command, string data) { Data = new byte[ScribblerHelper.CommandSize(command) - 1]; CommandType = (byte)command; int length = Math.Min(data.Length, 8); char[] tmp = data.ToCharArray(0, length); for (int i = 0; i < length; i++) { Data[i] = (byte)tmp[i]; } for (int i = length; i < 8; i++) { Data[i] = 0; } HasEcho = ScribblerHelper.HasEcho(command); ResponseLength = ScribblerHelper.ReturnSize(command); }
/// <summary> /// DO NOT USE THIS COMMAND DIRECTLY. /// In Scribbler.cs, post a message to _scribblerComPort /// </summary> /// <param name="cmd"></param> /// <returns></returns> internal ScribblerResponse SendCommand(ScribblerCommand cmd) { ScribblerResponse echo = null; ScribblerResponse response = null; int outMessageSize = helper.CommandSize((ScribblerHelper.Commands)cmd.CommandType); byte[] buffer = new byte[outMessageSize]; if (buffer != null) { int ix = 0; //buffer = cmd.ToByteArray(); buffer[ix++] = cmd.CommandType; // Changed this so it doesn't copy entire command (Fluke commands are shorter than 8 bytes) int len = Math.Min(cmd.Data.Length, (outMessageSize - 1)); if (cmd.Data != null && cmd.Data.Length > 0) { Array.Copy(cmd.Data, 0, buffer, 1, len); } ix += len; //foreach (byte b in cmd.Data) // buffer[ix++] = b; //fill to standard size while (ix < outMessageSize) { buffer[ix++] = 0; } #if DEBUG Console.Write("\nSent: "); foreach (byte b in buffer) { if (b != 0) { Console.Write(b + " "); } else { Console.Write("` "); } } Console.Write("\n"); #endif try { // When requesting a response, clear the inbound buffer if (_serialPort.BytesToRead > 0) { _serialPort.DiscardInBuffer(); } _serialPort.Write(buffer, 0, ix); } catch { Console.WriteLine("Serial Port Timeout. Lost connection with Scribbler."); //throw new IOException(); } if (helper.HasEcho((ScribblerHelper.Commands)cmd.CommandType)) { echo = GetEcho(buffer, outMessageSize); } response = GetCommandResponse(helper.ReturnSize((ScribblerHelper.Commands)cmd.CommandType), helper.HasEcho((ScribblerHelper.Commands)cmd.CommandType)); } return(response); }