/// <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> /// 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 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 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); }
// 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); }
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; }
/// <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> /// 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> /// 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); }
/// <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> /// 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 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 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 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; }
public override void ProcessData(byte[] buffer, int offset, int length) { for (int i = 0; i < length; i++) { byte data = buffer[offset + i]; switch (_parseState) { case ParseStates.COMMAND: ProcessCommand(data); break; case ParseStates.MODE: ProcessMode(data); break; case ParseStates.PACKET: _parseBuffer[_parseBufferCount++] = data; if ((_parseBufferCount == 4 && _parseBuffer[3] == 0) || (_parseBufferCount > 4 && _parseBufferCount == (4 + _parseBuffer[3]))) { EosPacket packet = new EosPacket(); packet.Destination = _parseBuffer[0]; packet.Source = _parseBuffer[1]; packet.Command = _parseBuffer[2]; for (int j = 0; j < _parseBuffer[3]; j++) { packet.Add(_parseBuffer[4 + j]); } OnPacketReceived(packet); if (State == EosBusState.WAITING_RESPONSE) { OnResponseReceived(packet); } if (packet.Command == 127 && packet.Data.Count > 0) { EosDevice device = Devices.GetByAddress(packet.Source); device.UpdateState(packet); OnDeviceUpdated(device); } _parseState = ParseStates.COMMAND; //Console.WriteLine(); } break; case ParseStates.ENUMERATIONCOUNT: if (data > 0) { _parseBuffer[0] = data; _parseState = ParseStates.ENUMERATION; } else { _parseState = ParseStates.COMMAND; } break; case ParseStates.ENUMERATION: _parseBuffer[1 + _parseBufferCount++] = data; if (_parseBufferCount == 21) { List <byte> deviceData = new List <byte>(20); for (int j = 0; j < 20; j++) { deviceData.Add(_parseBuffer[2 + j]); } _devices.Add(new EosDevice(this, _parseBuffer[1], deviceData)); _parseBuffer[0]--; _parseBufferCount = 0; if (_parseBuffer[0] == 0) { _parseState = ParseStates.COMMAND; OnBusReset(); //Console.WriteLine(); } } break; default: if (data == 58) { ProcessCommandData(); //Console.WriteLine(); } else { _parseBuffer[_parseBufferCount++] = data; } break; } } }
/// <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; }