简体   繁体   English

在同一个 COM 端口上使用两个串口 - WPF C#

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

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 ?为什么要使用SerialPort的两个实例? It's not like you'd have to save the result inside these objects.您不必将结果保存在这些对象中。

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.出现在MainWindow_Loaded事件处理程序中。

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?如果我理解正确的过程,每次设备通过串行接口接收到W时,它都会通过同一个串行端口溢出秤中的重量,对吗?

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.这样,您需要做的就是在单击按钮时发送一个“W”,结果应该出现在数据接收事件处理程序上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM