Dries Dijkstra

Dries Dijkstra

  • NA
  • 7
  • 15.4k

C# form rs232 COM-port application

Feb 20 2012 4:26 AM
We would like to create a program that counts how many times a "high" signal is received from a RS232 (COM) port.

4 signals have been hooked up to a rs232 connector and a form application will be used to count how many of each signals have been received..

Because of the fact that I'm a bit of a C# newbie, this isn't as easy as I had hoped.
What the program has to do:

- The form has 4 textboxes to show a counter for each pin
- One start button
- One stop button
- When the start button is pushed, the program opens a COM-connection and starts to get data from the com port
- When a high signal is received on one of the ports
- An event trigger will start and update 1 of 4 textboxes
- In my example below, I first want to try to fill 1 textbox with a value, but I keep getting the following errors:

* An object reference is required for the non-static field, method, or property 'System.Windows.Forms.Control.Text.get'

* Next to that when I run a similar (same) program in a console application, when the event is triggered it outputs "Pin x" to the console. It gets triggered 100.000 times it seems.. It keeps outputting, but
the signal only changes once per 5 seconds.

I hope you guys understand my problem and hope you can help me. Thanks in advance!

This is what I have so far..

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;
using System.IO.Ports;


namespace
WindowsFormsApplication1
{
public partial class Form1 :
Form
{
public Form1()
{
InitializeComponent();
}

private
void button1_Click(object sender, EventArgs e)
{
SerialPort port = new
SerialPort("COM3", 9600, Parity.None, 8, StopBits.One); // Instantiate the
communications // port with some basic settings
port.Open(); // Open the port
for communications

while (true)
{
if
(Buttonstop==1)
{
port.Close(); // Close the
port
break;
}
port.PinChanged += new
SerialPinChangedEventHandler(port_PinChanged); // Nieuwe event
trigger
}
}

public static void port_PinChanged(object sender,
SerialPinChangedEventArgs e) // This will be called whenever the port
changes.
{
string info = string.Empty;
switch
(e.EventType)
{
case SerialPinChange.CDChanged:
Form1.textBox3.Text =
"Pin 1";
break;
case SerialPinChange.CtsChanged:
Form1.Text = "Pin
8";
break;
case SerialPinChange.DsrChanged:
Form1.textBox3.Text = "Pin
6";
break;
case SerialPinChange.Ring:
Form1.textBox3.Text = "Pin
9";
break;
default:
break;
}
}
}
}

Answers (8)