I'm newbie in programming. Want to send series of pulses (square waves) to PC and use C# to count the incoming pulses and display it like a running counter (real time). Frequency of pulses: 1-10Hz
however i cant figure out how to do this. Should i connect the pulses to CTS of serial port? (instead of sending the pulses via TX pin), and how about the logic level of Serial port? my pulses is 5V (hi), and 0V (low). Do i need MAX 232?
how to keep reading (listening) to the port while the application does something else?
please help. i need enlightenment from expert here :) some sample code will be much appreciated.
Loading
Ryan AlfordPosted Jan 31, 2008, 9:45 AM
__________________________________________________
public SerialPort sp;
private delegate void SetTextDeleg(string text);
sp = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
this.sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
Thread.Sleep(500);
string x = sp.ReadLine();
this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { x });
}
catch
{ }
}
private void si_DataReceived(string data)
{
string scannedData = data.Trim();
// do work with data
}
_________________________________________
second example
_________________________________________
SerialPort sp = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
sp.Open();
sp.Write("P"); // This is a "PRINT" command for the scale which returns the weight
string inputChar = Convert.ToChar(sp.ReadChar()).ToString();
// this will read each character that is returned from the serial port.
_________________________________________________