1

I have a scale that connect to PC through RS232, I send "W" to receive the weight. The scale sends the weight all the time as it's read. How do I catch the weight that is being read?

Can i get any C# sample code?

Mike Atlas
  • 8,193
  • 4
  • 46
  • 62
Gold
  • 60,526
  • 100
  • 215
  • 315

2 Answers2

12

Sending a W? Sounds like the Mettler Toledo scale that FedEx gives businesses. I happen to have some code that reads from such a scale:


// where this.port is an instance of SerialPort, ie
// this.port = new SerialPort(
//  portName,
//  1200,
//  Parity.None,
//  8,
//  StopBits.One);
//  this.port.Open();

protected override bool GetWeight(out decimal weightLB, out bool stable)
{
    stable = false;
    weightLB = 0;

    try
    {
        string data;

        this.port.Write("W\r\n");
        Thread.Sleep(500);
        data = this.port.ReadExisting();

        if (data == null || data.Length < 12 || data.Substring(8, 2) != "LB")
        {
            return false;
        }

        if (decimal.TryParse(data.Substring(1, 7), out weightLB))
        {
            stable = (data[11] == '0');

            return true;
        }
    }
    catch (TimeoutException)
    {
        return false;
    }

    return false;
}
Nicholas Piasecki
  • 25,203
  • 5
  • 80
  • 91
0

You need to use SerialPort component of .NET. The full description and examples are available on MSDN site: http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx

alemjerus
  • 8,023
  • 3
  • 32
  • 40