-
March 8th, 2012, 12:27 PM
#1
Serial Port Communication.... HELP!!!!
Hello Guys,
I am doing my final year project at uni and I have to use RS232 cable (pin 2 and 3 jumpered) to create a communication link via COM port. I have these codes so far but for some reason I can't display the text message via the same COM port.
This is what I done under my supervisor's guidance...
Code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
#region serial port initialisation
spComm.BaudRate = 9600;
spComm.StopBits = StopBits.One;
spComm.Handshake = Handshake.None;
spComm.DataBits = 8;
spComm.Parity = Parity.None;
#endregion
spComm.Open();
lblMesRec.Text = "No Data Recieved YET!!!";
tbMessagetoSend.Text = "default message";
}
private void btnSendData_Click(object sender, EventArgs e)
{
//MessageBox.Show("Hello World");
string sData;
bool state = false;
sData = tbMessagetoSend.Text;
state = spComm.IsOpen;
//spComm.WriteLine(sData);
spComm.Write("Hello World");
}
private void spComm_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
lblMesRec.Text += spComm.ReadExisting();
}
private void button1_Click(object sender, EventArgs e)
{
lblMesRec.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
lblMesRec.Text = spComm.ReadExisting();
}
private void lblMesRec_Click(object sender, EventArgs e)
{
}
private void tbMessagetoSend_TextChanged(object sender, EventArgs e)
{
}
}
}
this is what i have done on my own to improve it.... but unfortunately this doesn't work as well...
namespace NewProject
{
public partial class Form1 : Form
{
// Global variable
string MRstring;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnSend_Click(object sender, EventArgs e)
{
Com1.PortName = "COM1";
Com1.BaudRate = 9600;
Com1.Open();
if (Com1.IsOpen)
{
btnSend.Enabled = false;
btnReceive.Enabled = true;
textBox1.ReadOnly = true;
}
}
private void btnReceive_Click(object sender, EventArgs e)
{
if (Com1.IsOpen)
{
Com1.Close();
btnSend.Enabled = true;
btnReceive.Enabled = false;
textBox1.ReadOnly = true;
}
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Com1.IsOpen) return;
char[] buff = new char[1];
// Load element 0 with the key character.
buff[0] = e.KeyChar;
// Send the one character buffer.
Com1.Write(buff, 0, 1);
}
private void DisplayText(object sender, EventArgs e)
{
textBox1.AppendText(MRstring);
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
MRstring = Com1.ReadExisting();
this.Invoke(new EventHandler(DisplayText));
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (Com1.IsOpen) Com1.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
I have started panicking coz the deadline is next week....Any help will be really appreciated...
Last edited by DataMiser; March 8th, 2012 at 02:42 PM.
Reason: added code tags
-
March 8th, 2012, 01:16 PM
#2
Re: Serial Port Communication.... HELP!!!!
First question, are you sure it's COM1? Stupid as it may sound, these things happen =)
- Why do you use two COM objects? On your Form_Load you use spComm, but then you use Com1.
- How did you create them? Did you drag a COM object using the forms UI creation interface or is it code created? (if the later, you never seem to create the instance)
Code:
SerialPort spComm = new SerialPort(...);
- Given the previous doubts, are you linking the DataRecieved event to the COM object?
Code:
spComm.DataRecieved += new eventHandler ...
Your code shows a serialPort1_DataRecieved method, which (considering it's neither Com1 nor spComm _DataRecieved) might not be linked to the COM object anymore if you played around deleting and adding objects by dragging them into your form.
I'd suggest you clean the code up a little first, to make sure what it is you're actually using.
(BTW, when posting code, use [ code ] [ /code ] tags to post yours)
-
March 8th, 2012, 01:23 PM
#3
Re: Serial Port Communication.... HELP!!!!
To check if the event is correctly linked using the object browser, you can select the events tab and search for DataReceived. If it is blank, then it means it wasn't linked. You will have a drop down menu that will contain the available valid methods that would suit that event.
For all these reasons, it's sometimes easier to follow things up by creating these kind of objects by hand in your own code. In my opinion at least =)
Second BTW, welcome.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|