private void _DiscardIncomming() { if (_rx_step != EReceiverStep.STOP) { _rx_frame_buffer = null; _rx_step = EReceiverStep.STOP; } }
private void _ProccessIncomingByte(byte data) { if (_rx_step == EReceiverStep.Address) { d_rx_frame_address = data; _rx_step = EReceiverStep.Command; } else if (_rx_step == EReceiverStep.Command) { d_rx_frame_command = data; d_rx_frame_addrcmd = (ushort)((d_rx_frame_address << 8) | d_rx_frame_command); _rx_step = EReceiverStep.Size; } else if (_rx_step == EReceiverStep.Size) { d_rx_frame_size_left = data; d_rx_frame_buffer = new byte[data]; _rx_step = d_rx_frame_size_left > 0 ? EReceiverStep.Data : EReceiverStep.CRC; } else if (_rx_step == EReceiverStep.Data) { d_rx_frame_buffer[d_rx_frame_buffer.Length - d_rx_frame_size_left--] = data; if (d_rx_frame_size_left == 0) { _rx_step = EReceiverStep.CRC; } } else if (_rx_step == EReceiverStep.CRC) { if (data == _CRC8(d_rx_frame_command, d_rx_frame_buffer)) { _rx_step = EReceiverStep.Stop; try { if ((d_rx_frame_command & 0x80) != 0) { lock (d_dict_sync_frame_processors) { List <Action <byte[]> > _list_of_commands; if (!d_dict_sync_frame_processors.TryGetValue(d_rx_frame_addrcmd, out _list_of_commands)) { d_dict_sync_frame_processors[d_rx_frame_addrcmd] = _list_of_commands = new List <Action <byte[]> >(); } _list_of_commands[0](d_rx_frame_buffer); _list_of_commands.RemoveAt(0); } } else { byte _addr = d_rx_frame_address; byte _cmd = d_rx_frame_command; byte[] _bytes_data = d_rx_frame_buffer; Task.Run(() => d_act_async_frame_processor(_addr, _cmd, _bytes_data)); } } catch { } d_rx_frame_command = 0; d_rx_frame_buffer = null; } else { d_is_frame_collision = true; _DiscardIncomming(); } } }
private void _ProccessIncomingByte(byte data) { if (_rx_step == EReceiverStep.COMMAND) { _rx_frame_command = data; _rx_step = EReceiverStep.SIZE; } else if (_rx_step == EReceiverStep.SIZE) { if (data > 0) { _rx_frame_size_left = data; _rx_frame_buffer = new byte[data]; _rx_step = EReceiverStep.STUB; } else { _is_frame_collision = true; _DiscardIncomming(); } } else if (_rx_step == EReceiverStep.STUB) { if (data == __SYNC_STUB) { _rx_stub_index = 0; _rx_step = _rx_frame_size_left > 0 ? EReceiverStep.DATA : EReceiverStep.CRC; } else { _is_frame_collision = true; _DiscardIncomming(); } } else if (_rx_step == EReceiverStep.DATA) { _rx_frame_buffer[_rx_frame_buffer.Length - _rx_frame_size_left--] = data; if (++_rx_stub_index >= __SYNC_SIGNATURE.Length - 1) { _rx_step = EReceiverStep.STUB; } else if (_rx_frame_size_left == 0) { _rx_step = EReceiverStep.CRC; } } else if (_rx_step == EReceiverStep.CRC) { if (data == _CRC8(_rx_frame_buffer)) { _rx_step = EReceiverStep.STOP; _act_frame_processor(new CFrame(_rx_frame_command, _rx_frame_buffer)); } else { _is_frame_collision = true; _DiscardIncomming(); } } }
private void _DiscardIncomming() { if (_rx_step != EReceiverStep.Stop) { if (_rx_step < EReceiverStep.Address) { EtaDebug.DebugWrite(ConsoleColor.Red, true, "DiscardIncomming"); } if (_rx_step < EReceiverStep.Command) { EtaDebug.DebugWrite(ConsoleColor.Red, true, "DiscardIncomming addr: {0}", d_rx_frame_address); } else if (_rx_step < EReceiverStep.Size) { EtaDebug.DebugWrite(ConsoleColor.Red, true, "DiscardIncomming addr: {0}, cmd: {1}", d_rx_frame_address, d_rx_frame_command); } else { EtaDebug.DebugWrite(ConsoleColor.Red, true, "DiscardIncomming addr: {0}, cmd: {1} {2}", d_rx_frame_address, d_rx_frame_command, BitConverter.ToString(d_rx_frame_buffer)); } d_rx_frame_command = 0; d_rx_frame_buffer = null; _rx_step = EReceiverStep.Stop; } }
private void _ConnectionThread() { try { while (true) { CFrame _frame_outgoing = null; if (_serial_port.BytesToWrite == 0) { lock (_queue_frame_outgoing) { if (_frame_singleton != null) { _frame_outgoing = _frame_singleton; _frame_singleton = null; } else if (_queue_frame_outgoing.Count > 0) { _frame_outgoing = _queue_frame_outgoing.Dequeue(); } } } if (_frame_outgoing != null) { byte[] _buffer = _frame_outgoing._bytes_frame; int _offset = 0; byte _command = _frame_outgoing._command; int _count = _frame_outgoing.BytesFrame.Length; if (_count <= byte.MaxValue) { _Write(__SYNC_SIGNATURE); _Write(new[] { _command, (byte)_count, __SYNC_STUB }); while (_count > 0) { int _block_count = Math.Min(_count, __SYNC_SIGNATURE.Length - 1); _Write(_buffer, _offset, _block_count); if (_block_count == __SYNC_SIGNATURE.Length - 1) { _Write(new[] { __SYNC_STUB }); } _offset += _block_count; _count -= _block_count; } _Write(new[] { _CRC8(_buffer) }); } } else if (_serial_port.BytesToRead > 0) { while (_serial_port.BytesToRead > 0) { byte _data = (byte)_serial_port.ReadByte(); if (_data == __SYNC_SIGNATURE[_rx_signature_index]) { _rx_signature_index++; if (_rx_signature_index >= __SYNC_SIGNATURE.Length) { _rx_signature_index = 0; if (_rx_step != EReceiverStep.STOP) { _DiscardIncomming(); } _rx_step = EReceiverStep.COMMAND; } } else { if (_rx_step != EReceiverStep.STOP) { if (_rx_signature_index > 0) { for (int _i = 0; _rx_signature_index > 0 && _rx_step != EReceiverStep.STOP; _rx_signature_index--) { _ProccessIncomingByte(__SYNC_SIGNATURE[_i++]); } } _ProccessIncomingByte(_data); } else { _is_frame_collision = true; } _rx_signature_index = 0; } } } else { Thread.Sleep(1); } } } catch (Exception) { try { if (_serial_port.IsOpen) { _serial_port.Close(); } } catch (Exception) { } if (_act_disconnect != null) { _act_disconnect(); } } }
private void _ConnectionThread() { try { while (true) { CFrame _frame_outgoing = null; if (d_serial_port.BytesToWrite == 0) { lock (d_queue_frame_outgoing) { if (d_queue_frame_outgoing.Count > 0) { _frame_outgoing = d_queue_frame_outgoing.Dequeue(); } } if (_frame_outgoing == null) { lock (d_sorted_dict_singletons) { if (d_sorted_dict_singletons.Count > 0) { KeyValuePair <uint, CFrame> _kvp_frame_singleton = d_sorted_dict_singletons.First(); d_sorted_dict_singletons.Remove(_kvp_frame_singleton.Key); _frame_outgoing = _kvp_frame_singleton.Value; } } } } if (_frame_outgoing != null) { byte _address = _frame_outgoing.Address; byte _command = _frame_outgoing.Command; byte[] _buffer = _frame_outgoing.BytesFrame ?? new byte[0]; int _count = _buffer.Length; byte _rotation_index = 0; ushort _rotation_byte = 0; byte[] _tx_buffer = new byte[((_count * 8) / 7) + 6]; int _tx_buffer_idx = 0; byte _crc = 0; Action <byte> _act_send = data => { if (_rotation_index >= 7) { _tx_buffer[_tx_buffer_idx++] = (byte)_rotation_byte; _rotation_index = 0; _rotation_byte = 0; } _rotation_byte |= (ushort)(data << _rotation_index); _tx_buffer[_tx_buffer_idx] = (byte)((_rotation_byte & 0x7F) | ((_tx_buffer_idx == 0) ? 0x80 : 0)); _tx_buffer_idx++; _rotation_index++; _rotation_byte >>= 7; _crc = _CRC8_Update(_crc, data); }; _act_send(_address); _act_send(_command); _act_send((byte)_count); foreach (byte _data in _buffer) { _act_send(_data); } _act_send(_crc); if (_rotation_index > 0) { _tx_buffer[_tx_buffer_idx++] = (byte)_rotation_byte; } _Write(_tx_buffer, 0, _tx_buffer_idx); } else if (d_serial_port.BytesToRead > 0) { while (d_serial_port.BytesToRead > 0) { byte _data = (byte)d_serial_port.ReadByte(); if ((_data & 0x80) != 0) { if (_rx_step != EReceiverStep.Stop) { _DiscardIncomming(); } _rx_step = EReceiverStep.Address; d_rx_rotation_index = 7; d_rx_rotation_byte = (byte)(_data & 0x7F); } else if (_rx_step != EReceiverStep.Stop) { ushort _data16 = (ushort)(_data << d_rx_rotation_index); d_rx_rotation_byte |= (byte)_data16; d_rx_rotation_index += 7; if (d_rx_rotation_index >= 8) { _ProccessIncomingByte(d_rx_rotation_byte); d_rx_rotation_byte = (byte)(_data16 >> 8); d_rx_rotation_index -= 8; } } } } else { Thread.Sleep(1); } } } catch (Exception) { try { if (d_serial_port.IsOpen) { d_serial_port.Close(); } } catch (Exception) { } d_act_disconnect?.Invoke(); } }