473,785 Members | 2,938 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serial Communication Port Help



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()
{
InitializeCompo nent();
// Clear the CmbPorts combo box
cmbPorts.Items. Clear();
//Get Port names in the System that are currently being used
this.cmbPorts.I tems.Add("<SELE CT PORT>");
foreach (string s in SerialPort.GetP ortNames())
{
cmbPorts.Items. Add(s);
}

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

}

private void btnOpenPort_Cli ck(object sender, EventArgs e)
{

try
{
SCIPort = new SerialPort();
SCIPort.PortNam e = cmbPorts.Text;
SCIPort.BaudRat e = 38400;
SCIPort.DataBit s = 8;
SCIPort.Parity = Parity.None;
SCIPort.StopBit s = StopBits.One;
SCIPort.Open();
SCIPort.ReadTim eout = 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", MessageBoxButto ns.OK,
MessageBoxIcon. Warning);
}

}
}
*************** ***RAWDATA.CS *************** *********
public RawData()
{
InitializeCompo nent();
COM_Port.SCIPor t.Write("V");
COM_Port.SCIPor t.ReadTimeout = 50;
COM_Port.SCIPor t.Write("V");
getData();
}

private void getData()
{
try
{
if (COM_Port.SCIPo rt.IsOpen)
{
COM_Port.SCIPor t.Write("V");
COM_Port.SCIPor t.DataReceived += new
SerialDataRecei vedEventHandler (port_DataRecei ved);

}
/*if(COM_Port.SC IPort.IsOpen == false)
{
MessageBox.Show ("Port is not open!", "COM Port
Warning", MessageBoxButto ns.OK, MessageBoxIcon. Error);
}*/
}
catch (Exception ex)
{
MessageBox.Show ("Error while opening " + ex.Message);
}
}
void port_DataReceiv ed(object sender, SerialDataRecei vedEventArgs e)
{
//txtXVolt.Text = "data received 1";
try
{
string data2 = "";
//txtXVolt.Text = "data received 1";
while (COM_Port.SCIPo rt.BytesToRead 0)
{
//txtXVolt.Text = "data received 2";
data2 +=
Convert.ToChar( COM_Port.SCIPor t.ReadByte());
VoltX(data2);
}
data2 = "";
}
catch (InvalidOperati onException ex)
{
MessageBox.Show ("Unable to show data!" + ex.Message,
"Data Received Error", MessageBoxButto ns.OK, MessageBoxIcon. Error);
}
}
private void VoltX(string data)
{
txtXVolt.Invoke (new EventHandler(de legate
{
try
{
txtXVolt.Select edText = string.Empty;
char[] delimiters = { 'x', 'y', 'z' };
string[] words = data.Split(deli miters);
string xVolt = words[1];
txtXVolt.Text = "xVolt";
}
catch
{
MessageBox.Show ("Error Reading the Accelerometer
Voltage Value, incorrect data.", "Accelerome ter Value Error",
MessageBoxButto ns.OK, MessageBoxIcon. Error);
}
}));
}


*** Sent via Developersdex http://www.developersdex.com ***
Feb 21 '07 #1
1 2504

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

im talking about this line of code;

COM_Port.SCIPor t.DataReceived += new
SerialDataRecei vedEventHandler (port_DataRecei ved);
put that in your form load event.

You can have it mapped without having the port configured.
"laura salhuana" wrote:
>

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()
{
InitializeCompo nent();
// Clear the CmbPorts combo box
cmbPorts.Items. Clear();
//Get Port names in the System that are currently being used
this.cmbPorts.I tems.Add("<SELE CT PORT>");
foreach (string s in SerialPort.GetP ortNames())
{
cmbPorts.Items. Add(s);
}

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

}

private void btnOpenPort_Cli ck(object sender, EventArgs e)
{

try
{
SCIPort = new SerialPort();
SCIPort.PortNam e = cmbPorts.Text;
SCIPort.BaudRat e = 38400;
SCIPort.DataBit s = 8;
SCIPort.Parity = Parity.None;
SCIPort.StopBit s = StopBits.One;
SCIPort.Open();
SCIPort.ReadTim eout = 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", MessageBoxButto ns.OK,
MessageBoxIcon. Warning);
}

}
}
*************** ***RAWDATA.CS *************** *********
public RawData()
{
InitializeCompo nent();
COM_Port.SCIPor t.Write("V");
COM_Port.SCIPor t.ReadTimeout = 50;
COM_Port.SCIPor t.Write("V");
getData();
}

private void getData()
{
try
{
if (COM_Port.SCIPo rt.IsOpen)
{
COM_Port.SCIPor t.Write("V");
COM_Port.SCIPor t.DataReceived += new
SerialDataRecei vedEventHandler (port_DataRecei ved);

}
/*if(COM_Port.SC IPort.IsOpen == false)
{
MessageBox.Show ("Port is not open!", "COM Port
Warning", MessageBoxButto ns.OK, MessageBoxIcon. Error);
}*/
}
catch (Exception ex)
{
MessageBox.Show ("Error while opening " + ex.Message);
}
}
void port_DataReceiv ed(object sender, SerialDataRecei vedEventArgs e)
{
//txtXVolt.Text = "data received 1";
try
{
string data2 = "";
//txtXVolt.Text = "data received 1";
while (COM_Port.SCIPo rt.BytesToRead 0)
{
//txtXVolt.Text = "data received 2";
data2 +=
Convert.ToChar( COM_Port.SCIPor t.ReadByte());
VoltX(data2);
}
data2 = "";
}
catch (InvalidOperati onException ex)
{
MessageBox.Show ("Unable to show data!" + ex.Message,
"Data Received Error", MessageBoxButto ns.OK, MessageBoxIcon. Error);
}
}
private void VoltX(string data)
{
txtXVolt.Invoke (new EventHandler(de legate
{
try
{
txtXVolt.Select edText = string.Empty;
char[] delimiters = { 'x', 'y', 'z' };
string[] words = data.Split(deli miters);
string xVolt = words[1];
txtXVolt.Text = "xVolt";
}
catch
{
MessageBox.Show ("Error Reading the Accelerometer
Voltage Value, incorrect data.", "Accelerome ter Value Error",
MessageBoxButto ns.OK, MessageBoxIcon. Error);
}
}));
}


*** Sent via Developersdex http://www.developersdex.com ***
Feb 22 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5443
by: mimisam | last post by:
hi, I am using Win XP and writing an application to check POS devices (printer and cash drawer).In the application I need to choose the port which the device is connected to. If the port chosen is wrong or the device is off power, a communication error message should be displayed. I am using FileOutputStream and FileInputStream to send and receive data from the ports. When parallel port is used, I got the feedback when the printer is...
1
8857
by: Andreas Horneff | last post by:
Hi @ all, I've got a problem with serial communication in Borland C++ Builder. I've already found a lot of stuff about serial communication in the internet, but it dosen't work. What I want to do: I want to connect only one button to my com port. If the button is pressed,
4
4738
by: Vidya Bhagwath | last post by:
Hello Experts, I am porting the C++ code into the Visual C#.NET. My C++ code is mainly based on the serial communication. So I am using the windows structure such as DCB.. etc and the windows functions frequently in my C++ code. I came to know how to import the windows functions into Visual C#.NET. But what is the method to import windows structure into Visual C#.NET? It will be very helpful for me if anybody can give me the WebID that...
4
11207
by: joe bloggs | last post by:
I am writing a mobile application to interface with a legacy system and I am planning to use web services to communicate with this system. The legacy system receives data through a serial port. What I would like to do is make the serial port accessible via a web service. The web service and the legacy application would be running on the same machine. The mobile application would access the web service via a network connection. It...
38
9670
by: shussai2 | last post by:
Hi, I am trying to access Serial Port in XP. I am using Dev-C++ IDE that uses Mingw as a compiler. I just want to know how I can open up serial port on COM1 and write some data. I have searched quite a bit over the web and could not find anything useful. I don't want to use Visual C++ or Cygwin, linux, etc. If any of you guys have some little tid bit of code that would be great to look at.
0
2007
by: usagimys | last post by:
Hi all, i'm very new in serial port communication.. here i got some problem in writing to the serial port.. i have done the connection successfully.. let me explain my situation.. i'm doing a system for toll.. here, there have a device name TFI (Toll Fare Indicator)..This device has to display 3 things (vehicle class, fare, and message).. now, i'm connecting my pc to this device using Serial Port.. i write to the serial port using byte.. now,...
3
11634
by: naveen.sabapathy | last post by:
Hi, I am trying to use virtual serial ports to develop/test my serial communication program. Running in to trouble... I am using com0com to create the virtual ports. The virtual ports seem to be working fine when I test it with Hyperterminal . I am using the example program that comes with pyserial, as below. --------------- import serial
4
4818
by: max_mont | last post by:
Hi all, I'm a newbie in .NET technology. I've already developed Serial communication applications in C++ (WIN32). And I wanted to migrate to .NET technology. There is a serial component in framework to read and write on serial port. I would like to make asynchronous reception. I saw that we can pass a delegate to the serial class which is call when some data is readen on the port.
9
14402
by: Hal Vaughan | last post by:
I've done a fair amount of Googling for information on reading the serial port in C++ (and in Linux). Unfortunately, out of every 4 hits, 1 seems to be an unanswered question, 1 is someone saying, "That's easy, there's a lot out there, Google it,", 1 is a discussion on it without examples and the other is who knows what. I did find some info on it and have been experimenting. The one example that I liked the best in terms of...
0
2907
by: Dhananjay | last post by:
Hi, I am working on an VB.Net application which I want to communicate to external device using comm port (Serial Port) . So for that first I am trying to simulate the communication on serial port between two computers using HYper terminal on both the machines. Both these computers are connected on serial port using DB9(f) null modem (Cross) cable.
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9952
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.