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();
}
private void button1_Click(object sender, EventArgs e)
{
SerialP.WriteLine("ATI");
}
}
}
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:
Form1sample event delegate passed to your SMS dialog:Write your SMS constructor:
… SMS event delegate:
Technically, you don’t need the full
Invoke(...)method signature. I included it for completeness and can be written as: