tags:

views:

547

answers:

5

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?

+2  A: 

Have a look at this SO question

epatel
+1  A: 

There's a nice serial communication tutorial here.

Seth
A: 

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  A: 

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
Nice extrapolation of missing context.
Michael Petrotta
+1 for identifying a a random device by the letter "W".
Seth
I would get rid of that Thread.Sleep(500).. there's better ways to accomplish this task
Miky Dinescu