sony
sony

Reputation: 1503

Use two serial ports on same COM port - WPF C#

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

Upvotes: 0

Views: 2020

Answers (2)

Paulo Santos
Paulo Santos

Reputation: 11567

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.

Upvotes: 1

Mario
Mario

Reputation: 36487

I don't think that's going to work. Why do you even need two connections to the serial port anyway?

Read the weight from the port in whatever way this is done and save the result. Then repeat the step with the loaded one. Calculate the difference and you're done.

Why would you like to use two instances of SerialPort? It's not like you'd have to save the result inside these objects.

Upvotes: 1

Related Questions