开发者

Difference between what i get from hyperterminal and what i get in C#

开发者 https://www.devze.com 2023-02-14 02:33 出处:网络
I\'m developing an application in C# to capture info from the serial port. The problem that I have is that the application is not reading exactly the same information that I\'m reading from the hypert

I'm developing an application in C# to capture info from the serial port. The problem that I have is that the application is not reading exactly the same information that I'm reading from the hyperterminal.

My question is, has anyone have this problem?, what properties can II check from my app that can be generating this problem?. I want to make a copy of the port configuration that I have in the hyperterminal, onto my application.

Thanks you for your help.

    void recibir(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        Thread.Sleep(0250);
        string crudo = sptSerial.ReadExisting();
        if (crudo.Equals("\0\0\0\0\0\0\0\0"))
        {
            MessageBox.Show("ERROR DE LECTURA","Control Toyota",MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        else
        {
            string binario = "";
            string binarioFinal = "";
            string binarioProcesado = "";
            int numero = 0;
            string info = convertAsciiTextToHex(crudo);
 开发者_运维技巧           binario = hex2binary(info);
            binarioProcesado = completarPaquete(binario);
            //binarioFinal = binarioProcesado.Substring(10, 16);
            if (flagCompletado == 1)
            {
                binarioFinal = binarioProcesado.Substring(5);
            }
            else
            {
                if (binarioProcesado.Length > 20)
                {
                    binarioFinal = binarioProcesado.Substring(5);
                }
                else
                {
                    binarioFinal = binarioProcesado.Substring(4);
                }
            }

            numero = ToDecimal(binarioFinal);
            csTarjeta tarjeta = new csTarjeta();
            string x = "";
            string y = "";
            try
            {
                tarjeta = csMTRSerial.armarPersona(numero);
            }
            catch (Exception ex)
            {
                MessageBox.Show("ERROR COMUNICANDO CON LA BASE DE DATOS\n" + ex.Message);
            }
            try
            {
                tarjeta.sUbicacion = csMTRSerial.devolverPosicion();
                x = tarjeta.sUbicacion.Substring(11, 10);
                x = x.Replace(",", ".");
                y = tarjeta.sUbicacion.Substring(0, 10);
                y = y.Replace(",", ".");
            }
            catch (Exception ex)
            {
                MessageBox.Show("ERROR COMUNICANDO CON EL GPS\n" + ex.Message);
            }

            if (tarjeta.sValido == 1)
            {
                lblAviso.Text = "";
                lblNombre.Text = "Nombre: " + tarjeta.sNombre;
                //lblApellido.Text = "Apellido: " + tarjeta.sApellido;
                lblNroTarjeta.Text = "Nro. de Tarjeta: " + tarjeta.sNroTarjeta.ToString();

                MemoryStream stream = new MemoryStream(tarjeta.sFoto);
                picFoto.Visible = true;
                picFoto.Image = Image.FromStream(stream);
                stream.Dispose();
                stream.Close();
                lblAviso.BackColor = Color.Green;
                lblAviso.Text = "TARJETA VALIDA";
                lblAviso.ForeColor = Color.Black;


            }
            else
            {
                lblNombre.Text = "";
                lblNroTarjeta.Text = "";
                picFoto.Visible = false;
                lblAviso.BackColor = Color.Red;
                lblAviso.Text = "TARJETA NO VALIDA";
                lblAviso.ForeColor = Color.White;
            }
            csRuta ruta = new csRuta();
            ruta.sID = idRuta;
            ruta.sNombre = nombreRecorrido;
            csMTRSerial.insertarFichada(tarjeta.sNroTarjeta, tarjeta.sNombre, tarjeta.sValido, x, y, nombreRecorrido);
            flagCompletado = 0;
        }
    }


    string crudo = sptSerial.ReadExisting();

You are reading a string from the device, then seem to make a lot of effort to turn that back into binary. This doesn't work well, you are for one subject to the conversion of bytes to characters as determined by the SerialPort.Encoding property. Which defaults to ASCII, turning any byte value > 0x7f to a question mark.

Read binary data with the SerialPort.Read() method.

0

精彩评论

暂无评论...
验证码 换一张
取 消