/// <summary> /// Creates a response packet for a given request packet. /// </summary> /// <param name="request">Packet containing the request for data.</param> public EosPacket(EosPacket request) : this() { Destination = request.Source; Command = request.Command; IsResponse = true; }
/// <summary> /// Transmits the current packet to the bus /// </summary> private void TransmitPacket(EosPacket sendPacket) { byte[] buffer = new byte[64]; lock (sendPacket) { sendPacket.Source = 0; buffer[0] = 0xbb; buffer[1] = 0x88; buffer[2] = sendPacket.Destination; buffer[3] = sendPacket.Source; buffer[4] = sendPacket.Command; buffer[5] = (byte)(sendPacket.Data.Count); sendPacket.Data.CopyTo(buffer, 6); buffer[6 + sendPacket.Data.Count] = sendPacket.Checksum; _masterState = BusMasterState.TRANSMITTING; SendData(buffer, 0, sendPacket.Data.Count + 7); if (sendPacket.Command > 127) { _resposneAddress = sendPacket.Destination; } else { _resposneAddress = 0xff; } _busTimer.Interval = TRANSMIT_CHECK_INTERVAL; _busTimer.Start(); sendPacket.IsSent = true; } }
/// <summary> /// Requests the config items for a servo. /// </summary> /// <param name="servo">ID of the servo to get the config for.</param> public void GetServoConfig(byte servo) { EosPacket packet = new EosPacket(); packet.Destination = _address; packet.Command = (byte)EosBusCommands.SERVO_GET_CONFIG; _bus.SendPacket(packet); }
/// <summary> /// Sends a command to the bus. /// </summary> /// <param name="address">Address of the device this command is for.</param> /// <param name="command">Command of this address</param> /// <param name="data">Data to include in this command.</param> public void SendCommand(byte address, EosBusCommands command, byte[] data = null) { EosPacket packet = new EosPacket(); packet.Destination = address; packet.Command = (byte)command; packet.Add(data); }
/// <summary> /// Requests the config items for a stepper. /// </summary> /// <param name="servo">ID of the stepper to get the config for.</param> public void GetStepperConfig(byte stepper) { EosPacket packet = new EosPacket(); packet.Destination = _address; packet.Command = (byte)EosBusCommands.STEPPER_GET_CONFIG; _bus.SendPacket(packet); }
/// <summary> /// Sends a GetInfo packet to the given device. /// </summary> /// <param name="address">Address to send the GetInfo packet to.</param> public void GetInfo(byte address) { EosPacket packet = new EosPacket(); packet.Destination = address; packet.Command = 130; SendPacket(packet); }
public override void SendPacket(EosPacket packet) { _sendPackets.Enqueue(packet); if (State == EosBusState.IDLE) { _busTimer.Interval = TRANSMIT_CHECK_INTERVAL; _busTimer.Start(); } }
/// <summary> /// Sets the current position of the stepper to zero. /// </summary> /// <param name="stepperId">ID of the stepper to zero.</param> public void ZeroStepperPosition(byte stepperId) { EosPacket packet = new EosPacket(); packet.Destination = _address; packet.Command = (byte)EosBusCommands.ZERO_STEPPER; packet.Add(stepperId); _bus.SendPacket(packet); }
protected void OnPacketReceived(EosPacket packet) { EosPacketEventArgs e = new EosPacketEventArgs(packet); EosPacketEventHandler handler = PacketReady; if (handler != null) { handler(this, e); } }
/// <summary> /// Creates an EOS Bus Device for an address and the supplied info packet from the device. /// </summary> /// <param name="bus">Bus that this device is attached to.</param> /// <param name="address">Address which this device is assigned.</param> /// <param name="data">Data from this devices INFO_RESPONSE packet.</param> internal EosDevice(EosBus bus, byte address, List <byte> data) { _bus = bus; _address = address; _pollingErrors = 0; byte[] stringBuffer = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int length = 8; for (int i = 0; i < 8; i++) { if (data[i] == 0) { length = i; break; } stringBuffer[i] = data[i]; } _name = System.Text.ASCIIEncoding.ASCII.GetString(stringBuffer, 0, length); length = 4; for (int i = 0; i < 4; i++) { if (data[i] == 0) { length = i; break; } stringBuffer[i] = data[i + 8]; } stringBuffer[4] = 0; _firmware = System.Text.ASCIIEncoding.ASCII.GetString(stringBuffer, 0, 4); _digialInputs = data[12]; _analogInputs = data[13]; _rotaryEncoders = data[14]; _ledOutputs = data[15]; _steppers = data[16]; _servos = data[17]; _alphaNumericDisplays = data[18]; _groupAddress = data[19]; if (data.Count >= 21) { _coilOutputs = data[20]; } _powerPacket = new EosPacket(Address, EosBusCommands.BACKLIGHT_POWER); _powerPacket.Add((byte)0); _powerPacket.IsSent = true; _levelPacket = new EosPacket(Address, EosBusCommands.BACKLIGHT_LEVEL); _levelPacket.Add((byte)0); _levelPacket.IsSent = true; }
/// <summary> /// Sets the signal output value for the servo. /// </summary> /// <param name="servo">ID of the servo to set the output signal for.</param> /// <param name="value">Signal value in microseconds.</param> public void SetServoValue(byte servo, int value) { EosPacket packet = new EosPacket(); packet.Destination = _address; packet.Command = (byte)EosBusCommands.SERVO_VALUE; packet.Add(servo); packet.Add(value); _bus.SendPacket(packet); }
/// <summary> /// Sets the target position for a stepper on this device. /// </summary> /// <param name="stepperId">ID of the stepper to set the position for.</param> /// <param name="position">Position to move the stepper to.</param> public void SetStepperTargetPosition(byte stepperId, long position) { EosPacket packet = new EosPacket(); packet.Destination = _address; packet.Command = (byte)EosBusCommands.STEPPER_TARGET; packet.Add(stepperId); packet.Add(position); _bus.SendPacket(packet); }
/// <summary> /// Sets the position of a coil needle. /// </summary> /// <param name="coil">ID of the coil to set the position for.</param> /// <param name="position">Position of the coil clamped from -255 to 255.</param> public void SetCoilPosition(byte coil, int position) { EosPacket packet = new EosPacket(); packet.Destination = _address; packet.Command = (byte)EosBusCommands.COIL_SET_POSITION; packet.Add(coil); packet.Add(position); _bus.SendPacket(packet); }
/// <summary> /// Sends a response to the given packet. /// </summary> /// <param name="request"></param> /// <param name="data"></param> public void SendResponse(EosPacket request, byte[] data = null) { EosPacket packet = new EosPacket(); packet.Destination = request.Source; packet.Command = request.Command; packet.IsResponse = true; packet.Add(data); SendPacket(packet); }
/// <summary> /// Sets a new address for a node on this device. /// </summary> /// <param name="newAddress">New address that this device should respond to.</param> public void SetNodeAddress(byte newAddress) { EosPacket packet = new EosPacket(); packet.Destination = _address; packet.Command = (byte)EosBusCommands.SET_ADDRESS; packet.Add(newAddress); _bus.SendPacket(packet); _address = newAddress; }
/// <summary> /// Sets a new group address for this device. /// </summary> /// <param name="newGroup">New address taht this device should listen on.</param> public void SetGroup(byte newGroup) { EosPacket packet = new EosPacket(); packet.Destination = _address; packet.Command = (byte)EosBusCommands.SET_GROUP; packet.Add(newGroup); _bus.SendPacket(packet); _groupAddress = newGroup; }
/// <summary> /// Sets the config items for a stepper. /// </summary> /// <param name="stepper">ID of the setpper to set the config for.</param> /// <param name="maxSpeed">Minimum singal value in microseconds.</param> /// <param name="maxValue">Maximum signal value in microseconds.</param> /// <param name="defaultValue">Default signal value in microseconds.</param> public void SetStepperConfig(byte stepper, uint maxSpeed, ulong idleSleep) { EosPacket packet = new EosPacket(); packet.Destination = _address; packet.Command = (byte)EosBusCommands.STEPPER_SET_CONFIG; packet.Add(stepper); packet.Add((int)maxSpeed); packet.Add((long)idleSleep); _bus.SendPacket(packet); }
/// <summary> /// Sets the config items for a servo. /// </summary> /// <param name="servo">ID of the servo to set the config for.</param> /// <param name="minValue">Minimum singal value in microseconds.</param> /// <param name="maxValue">Maximum signal value in microseconds.</param> /// <param name="defaultValue">Default signal value in microseconds.</param> public void SetServoConfig(byte servo, int minValue, int maxValue, int defaultValue) { EosPacket packet = new EosPacket(); packet.Destination = _address; packet.Command = (byte)EosBusCommands.SERVO_SET_CONFIG; packet.Add(servo); packet.Add(minValue); packet.Add(maxValue); packet.Add(defaultValue); _bus.SendPacket(packet); }
internal EosLed(EosDevice device, byte id) : base(device, id) { _powerPacket = new EosPacket(device.Address, EosBusCommands.LED_POWER); _powerPacket.Add((byte)id); _powerPacket.Add((byte)0); _powerPacket.IsSent = true; _levelPacket = new EosPacket(device.Address, EosBusCommands.LED_LEVEL); _levelPacket.Add((byte)id); _levelPacket.Add((byte)0); _levelPacket.IsSent = true; }
// Helper function to construct LED packets private void LedPacket(EosBusCommands command, byte data1, byte?data2 = null) { EosPacket packet = new EosPacket(); packet.Destination = _address; packet.Command = (byte)command; packet.Add(data1); if (data2 != null) { packet.Add((byte)data2); } _bus.SendPacket(packet); }
public override void SendPacket(EosPacket packet) { byte[] buffer = new byte[64]; buffer[0] = 115; buffer[1] = packet.Destination; buffer[2] = packet.Command; buffer[3] = (byte)(packet.Data.Count); packet.Data.CopyTo(buffer, 4); SendData(buffer, 0, packet.Data.Count + 4); if (packet.Command > 127) { State = EosBusState.WAITING_RESPONSE; } }
/// <summary> /// Sets the name that this device reports on the bus. /// </summary> /// <param name="name">New name for this device.</param> public void SetName(string name) { byte[] nameBuffer = new byte[9]; System.Text.Encoding.ASCII.GetBytes(name, 0, Math.Min(8, name.Length), nameBuffer, 0); nameBuffer[8] = 0; EosPacket packet = new EosPacket(); packet.Destination = _address; packet.Command = (byte)EosBusCommands.SET_NAME; for (int i = 0; i < name.Length && i < 8; i++) { packet.Add(nameBuffer[i]); } _bus.SendPacket(packet); _name = name; }
/// <summary> /// Sets the text output for an alphanumeric display on this device. /// </summary> /// <param name="display">ID of the display to set the text for.</param> /// <param name="text">Text to display on the device.</param> public void SetDisplayText(byte display, string text) { byte[] bytes = new byte[248]; byte length = (byte)Math.Min(248, text.Length); System.Text.Encoding.ASCII.GetBytes(text, 0, length, bytes, 0); EosPacket packet = new EosPacket(); packet.Destination = _address; packet.Command = (byte)EosBusCommands.SET_TEXT; packet.Add(display); packet.Add(length); for (int i = 0; i < length; i++) { packet.Add(bytes[i]); } _bus.SendPacket(packet); }
/// <summary> /// Executes the appropraite action when the bus is idle /// </summary> private void DoIdle() { EosPacket sendPacket = null; if (_sendPackets.TryDequeue(out sendPacket)) { //Console.WriteLine("Transmitting Packet Begin"); TransmitPacket(sendPacket); //Console.WriteLine("Transmitting Packet End"); } else { switch (State) { case EosBusState.POLLING: if (_polling) { NextPoll(); } else { State = EosBusState.IDLE; } break; case EosBusState.SCANNING: NextScan(); break; case EosBusState.IDLE: if (_polling) { State = EosBusState.POLLING; } break; } } }
public EosPacketEventArgs(EosPacket packet) { _packet = packet; }
protected void OnResponseReceived(EosPacket packet) { OnResponseReceived(new EosPacketEventArgs(packet)); }
/// <summary> /// Resets the parser to start looking for the packet start. /// </summary> public void Reset() { _parseTimer.Stop(); _state = BUSRECIEVESTATE.PACKET_S_START; _currentPacket = null; }
/// <summary> /// Processes a byte received on the bus. /// </summary> /// <param name="data">Byte recevied on the bus</param> public bool ProcessData(byte data) { //Console.Write("{0,2:x} ", data); bool packetReady = false; switch (_state) { case BUSRECIEVESTATE.PACKET_S_START: if (data == START_BYTE) { //Console.WriteLine("Start"); _state = BUSRECIEVESTATE.PACKET_S_LEADIN; } else { //Console.WriteLine("Skip"); } break; case BUSRECIEVESTATE.PACKET_S_LEADIN: if (data == LEADIN_BYTE) { //Console.WriteLine("Leadin"); _state = BUSRECIEVESTATE.PACKET_S_ADDRESS; } else { //Console.WriteLine("Reset"); _state = BUSRECIEVESTATE.PACKET_S_START; } break; case BUSRECIEVESTATE.PACKET_S_ADDRESS: //Console.WriteLine("Destination"); _parseTimer.Stop(); _parseTimer.Start(); _currentPacket = new EosPacket(); _currentPacket.Destination = data; _state = BUSRECIEVESTATE.PACKET_S_SRC; break; case BUSRECIEVESTATE.PACKET_S_SRC: //Console.WriteLine("Source"); _parseTimer.Stop(); _parseTimer.Start(); _currentPacket.Source = data; _state = BUSRECIEVESTATE.PACKET_S_COMMAND; break; case BUSRECIEVESTATE.PACKET_S_COMMAND: //Console.WriteLine("Command"); _parseTimer.Stop(); _parseTimer.Start(); _currentPacket.Command = data; _state = BUSRECIEVESTATE.PACKET_S_DATALEN; break; case BUSRECIEVESTATE.PACKET_S_DATALEN: //Console.WriteLine("Data Length"); _parseTimer.Stop(); _parseTimer.Start(); _dataRemainig = data; if (_dataRemainig > 0) { _state = BUSRECIEVESTATE.PACKET_S_DATA; } else { _state = BUSRECIEVESTATE.PACKET_S_CHKSUM; } break; case BUSRECIEVESTATE.PACKET_S_DATA: //Console.WriteLine("Data"); _parseTimer.Stop(); _parseTimer.Start(); _currentPacket.Add(data); if (--_dataRemainig == 0) { _state = BUSRECIEVESTATE.PACKET_S_CHKSUM; } break; case BUSRECIEVESTATE.PACKET_S_CHKSUM: _parseTimer.Stop(); if (_parseTimer != null) { _parseTimer.Stop(); } if (data == _currentPacket.Checksum) { //Console.WriteLine("Good Packet"); OnPacketReceived(_currentPacket); } else { //Console.WriteLine("Bad Packet"); OnParesError(EosParserError.CorruptPacket); } Reset(); packetReady = true; break; } return packetReady; }
/// <summary> /// Sends a packet to the bus. /// </summary> /// <param name="packet">Packet which to send.</param> public abstract void SendPacket(EosPacket packet);
public void UpdateState(EosPacket packet) { _state = packet.Data; }