<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Text;
using AmpsBoxSdk.Commands;
using AmpsBoxSdk.Devices;

namespace AmpsBoxSdk.Io
{
    internal sealed class AmpsBoxCommunicator : IDisposable
    {
        #region Members

        /// <summary>
        /// Synchronization object.
        /// </summary>
        private readonly object sync = new object();

        private readonly byte[] _lf = Encoding.ASCII.GetBytes("\n");
            #endregion

        #region Construction and Initialization

        public AmpsBoxCommunicator(SerialPort port)
        {
            this.port = port;
            this.port.PortName = port.PortName;
            this.port.BaudRate = port.BaudRate;
            this.port.NewLine = "\n";
            this.port.ErrorReceived += PortErrorReceived;
            this.port.RtsEnable = true; // must be true for MIPS / AMPS communication.
            this.port.WriteTimeout = 250;
            this.port.ReadTimeout = 250;
            this.IsEmulated = false;

            this.messageSources = ToDecodedMessage(ToMessage(this.Read)).Publish(); // Only create one connection.
        }

        #endregion


        /// <summary>
        /// Writes ASCII Encoded value to stream
        /// </summary>
        /// <param name="value"></param>
        internal void Write(byte[] value, string separator)
        {
            if (!this.port.IsOpen)
            {
                return;
            }
            lock (sync)
            {
                foreach (var b in value)
                {
                    this.port.BaseStream.WriteByte(b);
                }

                if (string.IsNullOrEmpty(separator)) return;

                var bytes = Encoding.ASCII.GetBytes(separator);
                foreach (var b in bytes)
                {
                    this.port.BaseStream.WriteByte(b);
                }
            }
        }

        internal void WriteEnd()
        {
            lock (sync)
            {
                foreach (var b in _lf)
                {
                    this.port.BaseStream.WriteByte(b);
                }
            }
        }



        internal void WriteHeader(AmpsCommand command)
        {
            

            var commandBytes = CommandMap.Default.GetBytes(command);
            if (commandBytes == null)
            {<commit_msg>Fixed ordering of char , sep. and value.<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Text;
using AmpsBoxSdk.Commands;
using AmpsBoxSdk.Devices;

namespace AmpsBoxSdk.Io
{
    internal sealed class AmpsBoxCommunicator : IDisposable
    {
        #region Members

        /// <summary>
        /// Synchronization object.
        /// </summary>
        private readonly object sync = new object();

        private readonly byte[] _lf = Encoding.ASCII.GetBytes("\n");
            #endregion

        #region Construction and Initialization

        public AmpsBoxCommunicator(SerialPort port)
        {
            this.port = port;
            this.port.PortName = port.PortName;
            this.port.BaudRate = port.BaudRate;
            this.port.NewLine = "\n";
            this.port.ErrorReceived += PortErrorReceived;
            this.port.RtsEnable = true; // must be true for MIPS / AMPS communication.
            this.port.WriteTimeout = 250;
            this.port.ReadTimeout = 250;
            this.IsEmulated = false;

            this.messageSources = ToDecodedMessage(ToMessage(this.Read)).Publish(); // Only create one connection.
        }

        #endregion


        /// <summary>
        /// Writes ASCII / UTF8 Encoded value to stream
        /// </summary>
        /// <param name="value"></param>
        internal void Write(byte[] value, string separator)
        {
            if (!this.port.IsOpen)
            {
                return;
            }
            lock (sync)
            {
                if (string.IsNullOrEmpty(separator)) return;

                var bytes = Encoding.ASCII.GetBytes(separator);
                foreach (var b in bytes)
                {
                    this.port.BaseStream.WriteByte(b);
                }

                foreach (var b in value)
                {
                    this.port.BaseStream.WriteByte(b);
                }
            }
        }

        internal void WriteEnd()
        {
            lock (sync)
            {
                foreach (var b in _lf)
                {
                    this.port.BaseStream.WriteByte(b);
                }
            }
        }



        internal void WriteHeader(AmpsCommand command)
        {
            var commandBytes = CommandMap.Default.GetBytes(command);
            if (commandBytes == null)
            {
{'fraction_non_alphanumeric': 0.09502806736166801, 'fraction_numerical': 0.0026062550120288693, 'mean_word_length': 1.4301022893326838, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1765499', 'n_tokens_mistral': 1358, 'n_tokens_neox': 1275, 'n_words': 295}