Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Buenos días, me gustaría saber si me pueden colaborar con la conexión de una balanza digital marca digitalpos modelo DIG-B01 tengo la conexión ya lista pero no logro capturar el valor del peso ya que la balanza es de respuesta continua y devuelve una trama de datos
el código es el siguiente

Translation:
Good morning, I would like to know if you can help me with the connection of a digital scale brand digitalpos model DIG-B01 I have the connection already ready but I cannot capture the value of the weight since the scale is of continuous response and returns a data frame
the code is the following


What I have tried:

C#
private void BtnProbar_Click(object sender, EventArgs e)
{
 try
 {
  Puertos = new SerialPort(CbxPuertos.Text, 9600, Parity.None, 8, StopBits.One);
  Puertos.Handshake = Handshake.None;
  Puertos.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
  Puertos.ReadTimeout = 500;
  Puertos.WriteTimeout = 500;
  Puertos.Handshake = Handshake.None;
  Puertos.Open();
  Puertos.WriteLine("0P");

  if (Puertos.IsOpen)
   lEstado.Text = "Conectado";
  else
   MessageBox.Show("Fallo la conexion");
  }
  catch (Exception ex)
  {
   MessageBox.Show(ex.Message);
  }
}

void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
 try
 {
  if (this.Enabled == true)
   {
    Thread.Sleep(500);
    string data = Puertos.ReadExisting();
    this.BeginInvoke(new DelegadoAcceso(si_DataReceived), new object[] { data });
   }
  }
  catch (Exception ex)
  {
  }
 }

private void si_DataReceived(string accion)
{
 this.TxValue.Text = accion;
 Puertos.Close();
}

este es el código que tengo hasta el momento y me captura la siguiente trama de datos
Translation:
this is the code I have so far and it captures the following data frame

\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002S 00.225kgd\u0003\u0004\0\u0001\u0002"
Posted
Updated 20-Dec-21 0:00am
v2
Comments
Richard Deeming 17-Dec-21 11:03am    
This is an English-language site. Please post in English.

You are assuming that the data is "ready and complete" when it arrives, and that very definitely isn't the case: you will get a DataReceived event when data arrives, but that doesn't mean it's the "first caracter" of a sequence, or that all the other characters will arrive half a second later.

Instead of using the DataReceived event, set up a BackgroundWorker task that monitors the input stream, and "builds up" valid values before passing them back to the UI thread via a Progress report.

If you look at the manufacturers site, you should find the data format, which is likely to be at the very least terminated by a character or sequence (othern a newline) and hopefully prefixed by a separate "start of data" character or sequence. Use that to "frame" your data in the background worker, and it should start to work.
If you can't work out the sequences, use something like Hyperterminal to look at the raw data the device emits to find out and to be sure that you have the right communication settings.
 
Share this answer
 
Comments
Diego Pereira 2021 17-Dec-21 12:07pm    
la respuesta que se obtiene de la balanza es continua, con Hyperterminal la respuesta se ve así.

S 00.335kgdS 00.335kgdS 00.335kgdS 00.335kgdS 00.335kgd
S 00.335kgdS 00.335kgdS 00.335kgdS 00.335kgdS 00.335kgdS 00.
335kgdS 00.335kgdS 00.335kgdS 00.335kgdS 00.335kgdS 00.335kg
dS 00.335kgdS 00.335kgdS 00.335kgdS 00.335kgdS 00.335kgd
S 00.335kgdS 00.335kgdS 00.335kgdU 00.220kggU 00.130kgeU 00.
110kggU 00.110kggU 00.110kggU 00.110kggS 00.110kgaS 00.110kg
aS 00.110kgaS 00.110kgaS 00.110kgaS 00.110kgaS 00.110kga
S 00.110kgaS 00.110kgaS 00.110kgaS 00.110kgaS 00.110kgaS 00.
110kgaS 00.110kgaS 00.110kgaS 00.110kgaS 00.110kgaS

por otro lado no entiendo como crear una tarea BackgroundWorker, si me puedes ayudar a solucionar mi inconveniente te agradecería mucho
OriginalGriff 17-Dec-21 12:30pm    
THis is an English language website, so it's polite to ask in English instead of expecting volunteers to translate it for you ...
OriginalGriff 17-Dec-21 12:37pm    
BackgroundWorker: read the documentation, which includes sample code:
https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker
It has the big advantages that it terminates when the app does, so you don't have to stop the thread automatically, and that it handles the invoke for you by reporting progress on the original thread via an Event.
And that data appears to have lead in and tail out sequences: the stuff that isn't "00.335kg" and so on. So use HyperTerminal of similar to work out what exactly that is, and use that to work out where in an incoming stream the actual data is.

I can't do that for you: I don't have the raw data, just a "picture" of it once it's been through your browser, the CP DB, the CP web server, and my browser - so I cannot identify any of the "square blocks" which indicate unprintable characters of some form!

I did some maintenance on a similar device interface, a few years ago, can't remember too much now.

Without seeing the manufacturers specs, I would make a guess that you are getting character 1 (Start of header) then character 2 (Start of text), then the data, then character 3 (End of text) and character 4 (End of trans). So what you need is the data between the STX and ETX characters.

There may be a character or code you need to send to start the device transmitting, and probably another code to send to tell it to stop transmitting, then you just need to sample some of the data which was received.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900