locked
How to read weighing machine value into textbox using rs232port?

    Question

  • sir,

    i have (Avery Weighing Machine) for weight  which is connected to computer,

    i want to display the weight into Textbox using C#.Net

    Please give me a sample code

     

    Thanking you

     

    Thursday, September 02, 2010 11:25 AM

Answers

  • Hi,

    //Add the NameSpace fo serialPort

    using System.Io.ports;

    Write the below code in a Button Click :-

    ---------------------------------------

    SerilaPort sp=new SerialPort();

    sp.PortName="COM1";

    sp.BaudRate=9600

    sp.DataBits=8;

    sp.Open();

    textWieght.Text=sp.ReadLine().ToString();

    sp.Close();

    your code is over...Uset it tell me if it works

     



    PS.Shakeer Hussain Hyderabad
    Monday, September 06, 2010 6:39 AM
  • Hi,

    Through the UI:

    Create a new windows forms application
    On the default form, form 1, from the toolbox, drag a ‘serial port’ onto the form
    You will see the serial port in the designer view
    Highlight the serial port
    In the properties window, find the ‘Events’ option
    Double click in Data Received. It will add the event handler to the code form you
    In the form load event, add code to open the port (see previous post from me)
    Put a break point in your code on the data received
    Run your application and weigh yourself!
    You should then get the event fired when the scales transmit its data

    Or programmatically:

    private SerialPort _serialPort = null;
    
        private void Form1_Load(object sender, EventArgs e)
        {
          _serialPort = new SerialPort("COM1", 9600, Parity.None, 8);
          _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
          _serialPort.Open();
        }
    
        void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
          string data = _serialPort.ReadExisting();
    
          // do something with your data, you will need to invoke a delegate to update the UI
        }
    
    Hope this helps,




    Stuart Phillips. If my answer helps you.. please mark as Answered.
    Tuesday, September 07, 2010 8:03 PM

All replies

  • That’s a very open question. Firstly, create a new serial port object (just drag one onto a windows forms application), configure its settings i.e. which COM port, baud rate etc etc. Attach a handler to the data received event, open the port and wait for data.

    You can create a serial port manually:

    SerialPort

     

    port = new SerialPort("COM1", 9600, Parity.None, 8);

    port.DataReceived +=
    new SerialDataReceivedEventHandler(port_DataReceived);

    port.Open();

    There are a hundred questions from this, such as what format does the information come through the COM port, does it keep the port open all the time etc etc.

    First step is just to get the port open and receiving data. Most manufacturers will produce some sort of API that will detail the format of the data it outputs, but to be honest, it’s not going to be that difficult to decode if it’s just a weight value.

    Hope this helps,


    Stuart Phillips. If my answer helps you.. please mark as Answered.
    • Proposed as answer by Stuart Phillips Thursday, September 02, 2010 7:01 PM
    Thursday, September 02, 2010 12:01 PM
  • Stuatr,I am novie to .Net could you give me a sample code for complete..i drag a serial port..

    i cant see any handler here to receive data from port..

     

    please help me to  know clearly?

     

     

    Monday, September 06, 2010 6:19 AM
  • Hi,

    //Add the NameSpace fo serialPort

    using System.Io.ports;

    Write the below code in a Button Click :-

    ---------------------------------------

    SerilaPort sp=new SerialPort();

    sp.PortName="COM1";

    sp.BaudRate=9600

    sp.DataBits=8;

    sp.Open();

    textWieght.Text=sp.ReadLine().ToString();

    sp.Close();

    your code is over...Uset it tell me if it works

     



    PS.Shakeer Hussain Hyderabad
    Monday, September 06, 2010 6:39 AM
  • Hi,

    Through the UI:

    Create a new windows forms application
    On the default form, form 1, from the toolbox, drag a ‘serial port’ onto the form
    You will see the serial port in the designer view
    Highlight the serial port
    In the properties window, find the ‘Events’ option
    Double click in Data Received. It will add the event handler to the code form you
    In the form load event, add code to open the port (see previous post from me)
    Put a break point in your code on the data received
    Run your application and weigh yourself!
    You should then get the event fired when the scales transmit its data

    Or programmatically:

    private SerialPort _serialPort = null;
    
        private void Form1_Load(object sender, EventArgs e)
        {
          _serialPort = new SerialPort("COM1", 9600, Parity.None, 8);
          _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
          _serialPort.Open();
        }
    
        void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
          string data = _serialPort.ReadExisting();
    
          // do something with your data, you will need to invoke a delegate to update the UI
        }
    
    Hope this helps,




    Stuart Phillips. If my answer helps you.. please mark as Answered.
    Tuesday, September 07, 2010 8:03 PM
  • Hi Saravanan2009,

     

    If your question has been solved, please don’t forget mark/vote the replies which helped you, this will encourage the other community members to join in discussion and help each one.

     

    In addition, I think the reply from Stuart Phillips is good.

     

    If there’s anything unclear, please feel free to let me know. And I’m glad to help you.

     

    Have a nice day!

    Mike

    Wednesday, September 08, 2010 4:50 AM
    Moderator
  •  I tried your code & i am getting the unformatted data or raw data..how can i encode that raw data..
    • Proposed as answer by robinsoncruzo Friday, April 27, 2012 12:17 PM
    • Unproposed as answer by robinsoncruzo Friday, April 27, 2012 12:17 PM
    Friday, April 27, 2012 12:07 PM
  • You should really be looking to abandon serial ports as many new pc's dont have them.

    There is a great USB C# and PIC C18 project at www.waitingforfriday.com

    The example shows an LED and a switch working but it can be modified to do what ever you like.

    I have changed it to function as an LC meter and a USB controlled audio mixer with not a lot of effort.

    Simon who runs the website is very helpful if you get stuck.


    n.Wright

    Monday, April 30, 2012 1:11 AM