Tutorial :RS232 question - how to read weight to PC



Question:

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?


Solution:1

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;  }  


Solution:2

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »