Exemplo n.º 1
0
        private void initializePortManager()
        {
            //String portName, int baudRate, Parity parity, int dataBits, StopBits stopBits
            string portName     = cboPorts.Text;
            string parityName   = cboParity.Text;
            string stopBitsName = cboStopBits.Text;

            int baudRate = int.Parse(cboBaudRate.Text);
            int dataBits = int.Parse(cboDataBits.Text);

            numberOfRetries = int.Parse(numberOfRetriesInput.Text);
            timeout = int.Parse(timeoutInput.Text);

            Parity parity = Parity.None;
            switch (parityName)
            {
                case "None":
                    parity = Parity.None;
                    break;
                case "Even":
                    parity = Parity.Even;
                    break;
                case "Mark":
                    parity = Parity.Mark;
                    break;
                case "Odd":
                    parity = Parity.Odd;
                    break;
                case "Space":
                    parity = Parity.Space;
                    break;
            }

            StopBits stopBits = StopBits.None;
            switch (stopBitsName)
            {
                case "None":
                    stopBits = StopBits.None;
                    break;
                case "One":
                    stopBits = StopBits.One;
                    break;
                case "Two":
                    stopBits = StopBits.Two;
                    break;
                case "OnePointFive":
                    stopBits = StopBits.OnePointFive;
                    break;
            }

            portManager = new PortManager(portName, baudRate, parity, dataBits, stopBits);
        }
Exemplo n.º 2
0
 private void readPortBuffer(PortManager portManager, int counter)
 {
     portManagerHelper.readPortManagerBuffer(portManager, counter, hexaOutputString, decimalOutputString, binaryOutputString, statusOutputString);
 }
Exemplo n.º 3
0
        public void readPortManagerBuffer(PortManager portManager, int counter, string[] hexaOutputString, string[] decimalOutputString, string[] binaryOutputString, string[] statusOutputString)
        {
            while (true)
            {
                string[] portManagerResponse = portManager.ReadPort();
                if (portManagerResponse[0] != "" && portManagerResponse[1] != "" && portManagerResponse[2] != "" && portManagerResponse[3] != "")
                {
                    hexaOutputString[counter] = portManagerResponse[0];
                    decimalOutputString[counter] = portManagerResponse[1];
                    binaryOutputString[counter] = portManagerResponse[2];
                    statusOutputString[counter] = portManagerResponse[3];

                    if (counter == 0 || (counter > 0 && hexaOutputString[counter - 1] != hexaOutputString[counter]))
                        break;
                }
                Thread.Sleep(200);
            }
        }
Exemplo n.º 4
0
        public void readPortManagerBuffer(PortManager portManager, int counter, string[] hexaOutputString, string[] decimalOutputString, string[] binaryOutputString, string[] statusOutputString)
        {
            //while (true)
            //{
            //    string[] portManagerResponse = portManager.ReadPort();
            //    if (portManagerResponse[0] != "" && portManagerResponse[1] != "" && portManagerResponse[2] != "" && portManagerResponse[3] != "")
            //    {
            //        hexaOutputString[counter] = portManagerResponse[0];
            //        decimalOutputString[counter] = portManagerResponse[1];
            //        binaryOutputString[counter] = portManagerResponse[2];
            //        statusOutputString[counter] = portManagerResponse[3];

            //        if (counter == 0 || (counter > 0 && hexaOutputString[counter - 1] != hexaOutputString[counter]))
            //            break;
            //    }
            //    Thread.Sleep(200);
            //}
        }
Exemplo n.º 5
0
        //Toma las request armadas guardadas en un param de esta clase, y ejecuta metodos del port
        //    Cosas a implementar:
        //    Si no se puede comunicar que largue un error y termine
        private void startQuery_Click(object sender, EventArgs e)
        {
            //String portName, int baudRate, Parity parity, int dataBits, StopBits stopBits
            string portName = cboPorts.Text;
            string parityName = cboParity.Text;
            string stopBitsName = cboStopBits.Text;

            int baudRate = int.Parse(cboBaudRate.Text);
            int dataBits = int.Parse(cboDataBits.Text);
            numberOfRetries = int.Parse(numberOfRetriesInput.Text);
            timeout = int.Parse(timeoutInput.Text);

            Parity parity = Parity.None;
            switch (parityName)
            {
                case "None":
                    parity = Parity.None;
                    break;
                case "Even":
                    parity = Parity.Even;
                    break;
                case "Mark":
                    parity = Parity.Mark;
                    break;
                case "Odd":
                    parity = Parity.Odd;
                    break;
                case "Space":
                    parity = Parity.Space;
                    break;
            }

            StopBits stopBits = StopBits.None;
            switch (stopBitsName)
            {
                case "None":
                    stopBits = StopBits.None;
                    break;
                case "One":
                    stopBits = StopBits.One;
                    break;
                case "Two":
                    stopBits = StopBits.Two;
                    break;
                case "OnePointFive":
                    stopBits = StopBits.OnePointFive;
                    break;
            }

            portManager = new PortManager(portName, baudRate, parity, dataBits, stopBits);

            //Por cada intento
            for (int currentRetry = 0; currentRetry < numberOfRetries; currentRetry++ )
            {
                //Calculamos inicio del intento
                DateTime initialTime = DateTime.Now;
                var differenceInMilliseconds = (DateTime.Now - initialTime).TotalMilliseconds;

                //Mientras no se pase del timeout del intento
                while (differenceInMilliseconds < timeout)
                {
                    //Actualizamos diferencia entre el inicio y este momento de ejecucion
                    differenceInMilliseconds = (DateTime.Now - initialTime).TotalMilliseconds;
                    try
                    {
                        //Abrimos el puerto
                        portManager.OpenPort();
                        //Inicializamos las variables del formulario como arrays de string de tamaño = cant de requests
                        hexaOutputString    = new string[requests.Count];
                        decimalOutputString = new string[requests.Count];
                        binaryOutputString  = new string[requests.Count];
                        statusOutputString  = new string[requests.Count];

                        int counter = 0;
                        foreach (var request in requests)
                        {
                            //si el id de dispositivo o algo esta mal, al querer escribir se detona
                            portManager.Write(request, 0, request.Length);
                            //Aca leemos el buffer todo el tiempo, hasta que algo vuelve
                            readPortBuffer(portManager, counter);
                            //Cuando tiene las variables completas, escribe el formulario
                            WriteOutput(hexaOutputString[counter], decimalOutputString[counter], binaryOutputString[counter]);
                            counter++;
                        }
                        WriteStatusTextBox();
                        currentRetry = numberOfRetries;
                        break;
                    }
                    catch { }
                    currentRetry++;
                }
            }
        }
Exemplo n.º 6
0
        private void readPortBuffer(PortManager portManager, int counter)
        {
            while (true)
            {
                string[] portManagerResponse = portManager.ReadPort();
                if (portManagerResponse[0] != "" && portManagerResponse[1] != "" && portManagerResponse[2] != "" && portManagerResponse[3] != "")
                {
                    string header                = "\nResponse " + Convert.ToString(counter) + " -- ";
                    hexaOutputString[counter]    = header + portManagerResponse[0];
                    decimalOutputString[counter] = header + portManagerResponse[1];
                    binaryOutputString[counter]  = header + portManagerResponse[2];

                    string status                = "Response " + Convert.ToString(counter) + " -- ";
                    statusOutputString[counter]  = header + portManagerResponse[3] + ". ";
                    break;
                }
                Thread.Sleep(200);
            }
        }
Exemplo n.º 7
0
 private void readPortBuffer(PortManager portManager, int counter)
 {
     try {
         portManagerHelper.readPortManagerBuffer(portManager, counter, hexaOutputString, decimalOutputString, binaryOutputString, statusOutputString);
     }
     catch (Exception e)
     {
         Console.Write(e);
     }
 }