Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6145359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:47:03+00:00 2026-05-23T18:47:03+00:00

I have a weighbridge application, where I am using a serial port to get

  • 0

I have a weighbridge application, where I am using a serial port to get the weights from the weighbridge.

I need to take the weights two times. One with the empty lorry and then with the loaded lorry. I just wonder if I can use two serial ports, one for weighing empty lorry and the other for weighing loaded one? something like

 spWeighIn = new SerialPort("COM1", 2400, Parity.None, 8, StopBits.One);
 spWeighOut = new SerialPort("COM1", 2400, Parity.None, 8, StopBits.One);

and use them simultaneously

Thanks

This is my code:

    SerialPort spWeigh;
    string strResponseWeigh;

    private delegate void SetTextDeleg(string text);

    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        spWeigh = new SerialPort("COM1", 2400, Parity.None, 8, StopBits.One);
        spWeigh.RtsEnable = false;
        spWeigh.DtrEnable = false;
        spWeigh.Handshake = Handshake.None;
        spWeigh.ReadTimeout = 10000;   
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        if (!spWeigh.IsOpen)
            spWeigh.Open();
        spWeigh.DataReceived += new SerialDataReceivedEventHandler(spWeigh_DataInReceived);
        spWeigh.Write("W");   
    }

    void spWeigh_DataInReceived(object sender, SerialDataReceivedEventArgs e)
    {
        strResponseWeigh = spWeigh.ReadLine();         
        string wt = strResponseWeigh.Substring(5, 7);
        this.TxtFrstWt.Dispatcher.BeginInvoke(new SetTextDeleg(sin_DataReceived), new object[] { wt });
    }

    private void sin_DataReceived(string data)
    {
        TxtFrstWt.Text = data.Trim();
        TxtDateIn.Text = DateTime.Now.ToString("dd/MM/yyyy");         
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        if (!spWeigh.IsOpen)
            spWeigh.Open();
        spWeigh.DataReceived += new SerialDataReceivedEventHandler(spWeigh_DataOutReceived);
        spWeigh.Write("W");   
    }

    void spWeigh_DataOutReceived(object sender, SerialDataReceivedEventArgs e)
    {
        strResponseWeigh = spWeigh.ReadLine();               
        string wt = strResponseWeigh.Substring(5, 7);               
        this.TxtFrstWt.Dispatcher.BeginInvoke(new SetTextDeleg(sout_DataReceived), new object[] { wt });
    }
    private void sout_DataReceived(string data)
    {
        TxtScndryWt.Text = data.Trim();
        TxtDateOut.Text = DateTime.Now.ToString("dd/MM/yyyy");
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:47:03+00:00Added an answer on May 23, 2026 at 6:47 pm

    Let me see if I got this right.

    You’re saying that you have a device connected to the serial port and that you need to read the information twice from there, right?

    Your code is a bit convulted when dealing with that. I’d redo it in a bit more like this:

    SerialPort spWeigh;
    string strResponseWeigh;
    
    private delegate void SetTextDeleg(string text);
    
    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }
    
    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        spWeigh = new SerialPort("COM1", 2400, Parity.None, 8, StopBits.One);
        spWeigh.RtsEnable = false;
        spWeigh.DtrEnable = false;
        spWeigh.Handshake = Handshake.None;
        spWeigh.ReadTimeout = 10000; 
        spWeigh.DataReceived += spWeigh_DataInReceived;
        spWeigh.Open();
    }
    
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        spWeigh.Write("W"); 
    }
    
    void spWeigh_DataInReceived(object sender, SerialDataReceivedEventArgs e)
    {
        strResponseWeigh = spWeigh.ReadLine();
        string wt = strResponseWeigh.Substring(5, 7);
        this.TxtFrstWt.Dispatcher.BeginInvoke(new SetTextDeleg(sin_DataReceived), new object[] { wt });
    }
    
    private void sin_DataReceived(string data)
    {
        TxtFrstWt.Text = data.Trim();
        TxtDateIn.Text = DateTime.Now.ToString("dd/MM/yyyy");
    }
    

    You see, you don’t need to attach event handlers every time you click the button, that’s why the line

    spWeigh.DataReceived += spWeigh_DataInReceived;
    

    appears to the MainWindow_Loaded event handler.

    Trying to open a serial connection more than once might cause some problems, that too went to the loaded event handler.

    If I understand the process correctly, every time the device received a W through the serial interface, it spills out the weight in the scale through the same serial port, right?

    This way all you need to do is to send a ‘W’ when you click the button and the results should appear on the data received event handler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a n-tire web application and search often times out after 30 secs. How
Have a really weird one here. The app has two targets, just to be
Have any one tried to activate fancybox thumbnail gallery using a button or an
Have a bunch of classes which I need to do serialize and deserialize from/to
Have started playing with Xcode 4.2, and created a single page application using storyboard
Have two UIBarButtonItems want to make it as one UIBarButtonItem and toggle between them
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Have you managed to get Aptana Studio debugging to work? I tried following this,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.