Hi All,
I have question about how to make ok button for RS232, so the preference can applied in main menu,
here is my code
CODE
public partial class rs232 : Form
{
string InputData = String.Empty;
// This delegate enables asynchronous calls for setting
// the text property on a TextBox control:
delegate void SetTextCallback(string text);
public rs232()
{
InitializeComponent();
// Nice methods to browse all available ports:
string[] ports = SerialPort.GetPortNames();
// Add all port names to the combo box:
foreach (string port in ports)
{
cmbComSelect.Items.Add(port);
}
}
private void cmbComSelect_SelectionChangeCommitted(object sender, EventArgs e)
{
if (port.IsOpen) port.Close();
port.PortName = cmbComSelect.SelectedItem.ToString();
//stsStatus.Text = port.PortName + ": 9600,8N1";
// try to open the selected port:
try
{
port.Open();
}
// give a message, if the port is not available:
catch
{
MessageBox.Show("Serial port " + port.PortName + " cannot be opened!", "RS232 tester", MessageBoxButtons.OK, MessageBoxIcon.Warning);
cmbComSelect.SelectedText = "";
// stsStatus.Text = "Select serial port!";
}
}
private void btnSend_Click(object sender, EventArgs e)
{
if (port.IsOpen) port.WriteLine(txtOut.Text);
else MessageBox.Show("Serial port is closed!", "RS232 tester", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtOut.Clear();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtIn.Clear();
}
private void port_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
{
InputData = port.ReadExisting();
if (InputData != String.Empty)
{
// txtIn.Text = InputData; // because of different threads this does not work properly !!
SetText(InputData);
}
}
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.txtIn.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else this.txtIn.Text += text;
}
private void ok_button(object sender, EventArgs e)
{
}
Do you have any idea what should be in OK button?
so after I click ok button then will go to main form and I can use the port of rs232.
Thank you
Regards