How to send a reply message to sender machine via serial port using c#
Posted
by karthik
on Stack Overflow
See other posts from Stack Overflow
or by karthik
Published on 2010-04-12T03:40:06Z
Indexed on
2010/04/12
3:43 UTC
Read the original article
Hit count: 360
c#
|serial-port
I am using the below code to receive the message via serial port which is working fine. Now i need to send back a acknowledgment message to the sender machine. How can send the message ?
private void MonitorSP_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
System.IO.Ports.SerialPort SP = (System.IO.Ports.SerialPort)sender;
//Get the ports available in system
string[] theSerialPortNames = System.IO.Ports.SerialPort.GetPortNames();
string strAvlPortNames = "";
foreach (string s in theSerialPortNames)
{
strAvlPortNames += s.ToString() + ", ";
}
//Read an contruct the message
Thread.Sleep(1000);
string msg = SP.ReadExisting();
string ConstructedMsg = "Port's Found : " + strAvlPortNames + "\n" + "Port Used : " + SP.PortName + "\n" + "Message Received : " + msg;
if (InvokeRequired)
{
richTextBox1.Invoke(new MethodInvoker(delegate { richTextBox1.Text = ConstructedMsg; }));
//Send acknowlegement to sender port
SP.Write(SP.PortName);
return;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace.ToString());
}
}
© Stack Overflow or respective owner