I need to receive data from a scale, I have already managed to send data but I cannot receive the data from the scale that is connected by RS232. How can I connect to this scale that shows me the weight on a Label? How could I connect this balance?
This code helps me to communicate with the com but I do not receive anything from the scale:
Code
public partial class FormInicio : Form
{
public FormInicio()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void puertosDisponibles()
{
foreach (string puertoDis in System.IO.Ports.SerialPort.GetPortNames())
{
cmbPuertos.Items.Add(puertoDis);
}
}
private void Form1_Load(object sender, EventArgs e)
{
puertosDisponibles();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
serialPort1.Close();
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string datorx=serialPort1.ReadExisting();
txtRx.Text = datorx.Trim();
}
private void cmbPuertos_SelectedIndexChanged_1(object sender, EventArgs e)
{
serialPort1.PortName = cmbPuertos.Text;
cmbPuertos.Enabled = false;
try
{
serialPort1.Open();
}
catch (Exception ex)
{
MessageBox.Show("Selecciones otro puerto", "Puerto no disponible");
cmbPuertos.Enabled = true;
}
}
private void btnEnviar_Click_1(object sender, EventArgs e)
{
try
{
serialPort1.Write(txtTx.Text.Trim());
txtTx.Clear();
}
catch (Exception ex)
{
MessageBox.Show("No se puedo enviar la información", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}
I have the following code that I call in a textBox, but I can't get anything. What am I doing wrong or how could I display the scale data?
SerialPort spPuertoSerie = new SerialPort();
spPuertoSerie.PortName = "COM1";
spPuertoSerie.Handshake = Handshake.None;
spPuertoSerie.BaudRate = 9600;
spPuertoSerie.Parity = Parity.None;
spPuertoSerie.StopBits = StopBits.One;
spPuertoSerie.DataBits = 8;
I once worked with a scale. I think the problem is in this method:
Make sure that the event handler is not assigned by the properties window but at the top of your call to the Open() method. That way it would look like this:
That pro proves useful.