开发者

Using same serialport on two different forms

I have two forms = Form1.CS and SMS.CS on the first form (Form1) i've configured and ready to work serialport1. I have button which is called send message. This button opens new form called SMS.cs.

private void SMS_Click(object sender, EventArgs e)
    {
        SMS settings = new SMS();
        settings.ShowDialog();
    }

I'd like to use my configured serialport1 on to forms: Form1 and form SMS.cs. It's also possible to receive from form SMS data, and send it using serialport1 on Form1 for example:

 private void SMS_Click(object sender, EventArgs e)
    {
        SMS settings = new SMS();
        settings.ShowDialog();
        SerialPort1.Writeln(Data from form SMS)
    }

but i don't know how to do it. The best idea in my opinion is to send data directly from SMS form..

Edit:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApplication1
{
    public partial class SMSForm : Form
    {
        SerialPort SerialP;

        public SMSForm(Object SerialP)
        {
            InitializeComponent(开发者_StackOverflow社区);
        }

        private void button1_Click(object sender, EventArgs e) 
        {
            SerialP.WriteLine("ATI");
        }
    }
}


To use the same SerialPort in two forms you need to send the reference to the SerialPort to the second form.

The simplest way is to add a new constructor to the SMS form which accepts a SerialPort, store it in a member variable and use it.

Where you do

SMS settings = new SMS();

you will need to actually do this:

SMS settings = new SMS(SerialPort1);


Form1 is the SerialPort controller - that is, Form1 configures SerialPort. If you have an event that affects SerialPort, such as `SerialPort1.Writeln', you should pass a method reference to the dialog constuctor. This will logically keep classes separate while sharing methods.

You can create a custom EventArgs object to pass the string back to your SerialPort:

public class WriteToSerialPortEventArgs : EventArgs
{
    public WriteToSerialPortEventArgs (string Write)
    { WriteBytes = Encoding.GetBytes(write); }  // simplified - see MSDN for more options 

    public byte[] WriteBytes
    { get; private set; }   
}

Form1 sample event delegate passed to your SMS dialog:

void OnWriteToSerialPort(object sender, WriteToSerialPortEventArgs e )
{
    SerialPort1.WriteLine(e.WriteBytes);
}

Write your SMS constructor:

// field
Action<object, WriteToSerialPortEventArgs> writeDelegate;

public SMS (Action<object, WriteToSerialPortEventArgs> writeDelegate)
{
    this.writeDelegate = writeDelegate;
    this.button1.Click += new EventHandler(button1_Click);
}

... SMS event delegate:

void button1_Click(object sender, EventArgs e)
{
    writeDelegate.Invoke(sender, new WriteToSerialPortEventArgs (txtBox.Text);
}

Technically, you don't need the full Invoke(...) method signature. I included it for completeness and can be written as:

writeDelegate(sender, new WriteToSerialPortEventArgs (txtBox.Text);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜