Serial Communication Port Help

L

laura salhuana

Hi:

I am trying to write a serial communication GUI application. My
application has the following specifications. When I set my board up in
hyperterminal I do the following set up: bits per second = 38400, data
bits = 8, parity = none, stop bits = 1, flow control = none. Then once I
hit OK if I type in a "V" my board responds with 6 bits of data.
x[Xdata]y[YData]z[ZData] I have the following code to set up the com
port and then write a V and I have set up an event for when data is
received. But for some reason I never get the event. Am I doing
something wrong with the setup or am I not writing the "V"? Please
help! Below is the code.

******************COMPORT.CS ************************
public partial class COM_Port : Form
{
SerialPort SCIPort;
TriaxReloaded tr;
string readVal;


public COM_Port()
{
InitializeComponent();
// Clear the CmbPorts combo box
cmbPorts.Items.Clear();
//Get Port names in the System that are currently being used
this.cmbPorts.Items.Add("<SELECT PORT>");
foreach (string s in SerialPort.GetPortNames())
{
cmbPorts.Items.Add(s);
}

if (cmbPorts.Items.Count == 0)badu
{
MessageBox.Show("No Ports Available!", "COM Port Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}

}

private void btnOpenPort_Click(object sender, EventArgs e)
{

try
{
SCIPort = new SerialPort();
SCIPort.PortName = cmbPorts.Text;
SCIPort.BaudRate = 38400;
SCIPort.DataBits = 8;
SCIPort.Parity = Parity.None;
SCIPort.StopBits = StopBits.One;
SCIPort.Open();
SCIPort.ReadTimeout = 50;
SCIPort.Write("R");
this.Hide();
tr = new TriaxReloaded();
tr.ShowDialog();
this.Close();

}
catch
{
MessageBox.Show("COM Port Selelection Can Not Be
Empty!", "COM Port Warning", MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}

}
}
******************RAWDATA.CS ************************
public RawData()
{
InitializeComponent();
COM_Port.SCIPort.Write("V");
COM_Port.SCIPort.ReadTimeout = 50;
COM_Port.SCIPort.Write("V");
getData();
}

private void getData()
{
try
{
if (COM_Port.SCIPort.IsOpen)
{
COM_Port.SCIPort.Write("V");
COM_Port.SCIPort.DataReceived += new
SerialDataReceivedEventHandler(port_DataReceived);

}
/*if(COM_Port.SCIPort.IsOpen == false)
{
MessageBox.Show("Port is not open!", "COM Port
Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
}*/
}
catch (Exception ex)
{
MessageBox.Show("Error while opening " + ex.Message);
}
}
void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//txtXVolt.Text = "data received 1";
try
{
string data2 = "";
//txtXVolt.Text = "data received 1";
while (COM_Port.SCIPort.BytesToRead > 0)
{
//txtXVolt.Text = "data received 2";
data2 +=
Convert.ToChar(COM_Port.SCIPort.ReadByte());
VoltX(data2);
}
data2 = "";
}
catch (InvalidOperationException ex)
{
MessageBox.Show("Unable to show data!" + ex.Message,
"Data Received Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void VoltX(string data)
{
txtXVolt.Invoke(new EventHandler(delegate
{
try
{
txtXVolt.SelectedText = string.Empty;
char[] delimiters = { 'x', 'y', 'z' };
string[] words = data.Split(delimiters);
string xVolt = words[1];
txtXVolt.Text = "xVolt";
}
catch
{
MessageBox.Show("Error Reading the Accelerometer
Voltage Value, incorrect data.", "Accelerometer Value Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}));
}
 
G

Guest

I would not map the event directly after you output the V command.

im talking about this line of code;

COM_Port.SCIPort.DataReceived += new
SerialDataReceivedEventHandler(port_DataReceived);


put that in your form load event.

You can have it mapped without having the port configured.


laura salhuana said:
Hi:

I am trying to write a serial communication GUI application. My
application has the following specifications. When I set my board up in
hyperterminal I do the following set up: bits per second = 38400, data
bits = 8, parity = none, stop bits = 1, flow control = none. Then once I
hit OK if I type in a "V" my board responds with 6 bits of data.
x[Xdata]y[YData]z[ZData] I have the following code to set up the com
port and then write a V and I have set up an event for when data is
received. But for some reason I never get the event. Am I doing
something wrong with the setup or am I not writing the "V"? Please
help! Below is the code.

******************COMPORT.CS ************************
public partial class COM_Port : Form
{
SerialPort SCIPort;
TriaxReloaded tr;
string readVal;


public COM_Port()
{
InitializeComponent();
// Clear the CmbPorts combo box
cmbPorts.Items.Clear();
//Get Port names in the System that are currently being used
this.cmbPorts.Items.Add("<SELECT PORT>");
foreach (string s in SerialPort.GetPortNames())
{
cmbPorts.Items.Add(s);
}

if (cmbPorts.Items.Count == 0)badu
{
MessageBox.Show("No Ports Available!", "COM Port Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}

}

private void btnOpenPort_Click(object sender, EventArgs e)
{

try
{
SCIPort = new SerialPort();
SCIPort.PortName = cmbPorts.Text;
SCIPort.BaudRate = 38400;
SCIPort.DataBits = 8;
SCIPort.Parity = Parity.None;
SCIPort.StopBits = StopBits.One;
SCIPort.Open();
SCIPort.ReadTimeout = 50;
SCIPort.Write("R");
this.Hide();
tr = new TriaxReloaded();
tr.ShowDialog();
this.Close();

}
catch
{
MessageBox.Show("COM Port Selelection Can Not Be
Empty!", "COM Port Warning", MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}

}
}
******************RAWDATA.CS ************************
public RawData()
{
InitializeComponent();
COM_Port.SCIPort.Write("V");
COM_Port.SCIPort.ReadTimeout = 50;
COM_Port.SCIPort.Write("V");
getData();
}

private void getData()
{
try
{
if (COM_Port.SCIPort.IsOpen)
{
COM_Port.SCIPort.Write("V");
COM_Port.SCIPort.DataReceived += new
SerialDataReceivedEventHandler(port_DataReceived);

}
/*if(COM_Port.SCIPort.IsOpen == false)
{
MessageBox.Show("Port is not open!", "COM Port
Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
}*/
}
catch (Exception ex)
{
MessageBox.Show("Error while opening " + ex.Message);
}
}
void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//txtXVolt.Text = "data received 1";
try
{
string data2 = "";
//txtXVolt.Text = "data received 1";
while (COM_Port.SCIPort.BytesToRead > 0)
{
//txtXVolt.Text = "data received 2";
data2 +=
Convert.ToChar(COM_Port.SCIPort.ReadByte());
VoltX(data2);
}
data2 = "";
}
catch (InvalidOperationException ex)
{
MessageBox.Show("Unable to show data!" + ex.Message,
"Data Received Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void VoltX(string data)
{
txtXVolt.Invoke(new EventHandler(delegate
{
try
{
txtXVolt.SelectedText = string.Empty;
char[] delimiters = { 'x', 'y', 'z' };
string[] words = data.Split(delimiters);
string xVolt = words[1];
txtXVolt.Text = "xVolt";
}
catch
{
MessageBox.Show("Error Reading the Accelerometer
Voltage Value, incorrect data.", "Accelerometer Value Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}));
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top