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

时间:2011-07-16 10:27:38

标签: c# wpf serial-port

我有一个地磅应用程序,我正在使用一个串口来获得地磅的重量。

我需要重量两次。一个是空车,然后是装载的卡车。我只是想知道我是否可以使用两个串口,一个用于称重空载货车而另一个用于称重装载一个?

之类的东西
 spWeighIn = new SerialPort("COM1", 2400, Parity.None, 8, StopBits.One);
 spWeighOut = new SerialPort("COM1", 2400, Parity.None, 8, StopBits.One);

并同时使用它们

由于

这是我的代码:

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

2 个答案:

答案 0 :(得分:1)

我认为这不会起作用。为什么你甚至需要两个串口连接?

以任何方式从端口读取重量并保存结果。 然后用加载的步骤重复该步骤。 计算差异就完成了。

您为什么要使用SerialPort的两个实例?这不像你必须将结果保存在这些对象中。

答案 1 :(得分:1)

让我看看我是否做对了。

你说有一个设备连接到串口,你需要从那里读取两次信息,对吗?

在处理时,您的代码有点令人沮丧。我有点像这样重做它:

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

您看,每次单击按钮时都不需要附加事件处理程序,这就是行

的原因
spWeigh.DataReceived += spWeigh_DataInReceived;

出现在MainWindow_Loaded事件处理程序中。

尝试多次打开串行连接可能会导致一些问题,这也会导致加载的事件处理程序。

如果我正确理解了该过程,每次设备通过串行接口收到W时,它会通过相同的串口溢出秤中的重量,对吗?

这样,您只需在单击按钮时发送“W”,结果应显示在数据接收事件处理程序中。