Ejemplo n.º 1
0
    IEnumerator WriteComport()
    {
        com.statusManager.statusText.text = "Preparing data for transfer to device...";
        Reset();


        for (int x = 0; x < dataTableX.Length; x++)
        {
            output += dataTableX[x].text + ",";

            if ((x % inputFieldOffset) == 0 && x != 0)
            {
                output           += "\n";
                inputFieldOffset += 16;
            }
        }

        inputFieldOffset = 15;
        output          += "\n";

        for (int x = 0; x < dataTableY.Length; x++)
        {
            output += dataTableY[x].text + ",";

            if ((x % inputFieldOffset) == 0 && x != 0 && x != 255)
            {
                output           += "\n";
                inputFieldOffset += 16;
            }
        }

        byte[] bytes = Encoding.ASCII.GetBytes(output);

        var xmodem = new XModemCommunicator();

        xmodem.Port = com.serialPort;
        xmodem.Data = bytes;

        // Subscribe to events.
        xmodem.Completed += (s, e) => {
            Debug.Log($"Operation completed.\n");
            com.statusManager.statusText.text = "Writing data to device has successfully completed!";
        };
        xmodem.Aborted += (s, e) => {
            Debug.Log("Operation Aborted.\n");
            com.statusManager.statusText.text = "Writing data to device has failed!";
        };

        StartCoroutine(PrepareWriteState());
        yield return(new WaitUntil(() => com.messageSent));

        com.messageSent = false;
        xmodem.Send();

        if (xmodem.State != XModemStates.Idle)
        {
            xmodem.CancelOperation();
        }

        //com.EnableDataRead();
        Reset();
        yield return(null);

        com.EnableDataRead();
    }