RS-232 communication using C#

5.2k views Asked by At

Hi everyone I want to send/receive data serially via rs-232 using c#,VS10 ... my code for do so is as follows .....

SerialPort sp = new SerialPort();
//predefined values are baud rate =9600, parity=none, databits=8,stopbit=1 
//for sending data I used a textbox same name itself and a button "sendbutton" which coded as follow
try
{
    sp.WriteLine(textBox.Text);
    textBox.Text = "";
}
catch (System.Exception ex)
{
    baudRatelLabel.Text = ex.Message;
}
//for receiving data code is as follows 
try
{
    textBox.Text = "";
    textBox.Text = sp.ReadLine();
}
catch (System.Exception ex)
{
    baudRatelLabel.Text = ex.Message;
}

I installed this application in two pc and communicate both using using rs232 (both female end). But the data i'm sendig from one pc by send button is not received by another pc ....what should i do now.....

3

There are 3 answers

10
Joel Rondeau On BEST ANSWER

First thing I would do is use a different application to read the data. HyperTerminal, for example. Try to figure out if your app is sending or not, then, once you get it to send, work on trying to figure out if it is receiving data properly or not.

0
Preet Sangha On

Before you debug this. First rule of any hardware interation - you should verify that that the hardware and OS are correctly working. Both machines should be able to communicate to either other, or modems or other RS232 devices first. If the ports are all working properly, then you should try and debug your program using a know program on one side first.

I would use a comms program such as http://realterm.sourceforge.net/ as the know side.

I learned this lesson the hard way by spending 2 weeks debugging a program when there was dry joint in the cable!

1
Thorsten Dittmar On

Call the Open method before trying to send anything. The MSDN Documentation on the Open method even has sample code.