Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Trying to receive a data from a comport and display it in richtextbox using windows form. I know only the basic c++ . I have created a windows form with two button(read and exit) and rich text box . When i click on read button . richtextbox display values from serial port

Can someone help me do it or suggest some tutorials .

P.S I have created this topic after searching . I hope someone can suggest something for me
Posted

You don't actually say which bit is causing you the biggest problem, but this[^] is a useful page on serial port handling.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-May-11 9:44am    
Hope it will be helpful, too, especially after downloading it and removing patterned violet background :-) My 5.
--SA
Do you accept Rich text format string coming out from a serial port? :-)
I doubt. Why not using a TexBox? Or, better yet, a ListBox (convenient to append one message at a time).

Don't try to mix serial port communication and System.Windows.Forms. Separate concerns. Find out how to work with the serial ports, find out how to do UI — separately. Don't even play with the idea of not using threading — create a separate thread for communications.

For serial ports coding samples, libraries and directions, look at these CodeProject articles:
Creating a Serial communication on Win32[^],
CSerialIO - A Useful and Simple Serial Communication Class[^],
Serial Port R/W With Read Thread[^] (look how to use a separate thread and why),
Serial Communication in Windows[^],
Simple Serial Communication[^],
CSerialPort v1.03 - Serial Port Wrapper[^],
CSerialCom - A Simple Class for Implementing Serial Communication in Win-9X/2000[^],
Serial library for C++[^].

Google or search CodeProject to find more.

Good luck,
—SA
 
Share this answer
 
Comments
Richard MacCutchan 25-May-11 9:17am    
+5 excellent suggestions and links.
Sergey Alexandrovich Kryukov 25-May-11 9:43am    
Thank you, Richard.
--SA
This is the design

http://s282.photobucket.com/albums/kk269/dxm4i/?action=view¤t=untitled.jpg[^]

I should be able to select the port in combo box and when i click on read data button . Data should be displayed on textbox .

I know only the very basic of windows form like adding stuffs like that . Can somebody help me or give me tutorial from which i can get familiar with tools .

Thanks in advance . Code for the above program will be appreciated . Usually i click on the button and write codes for this i dont know how to write
 
Share this answer
 
Comments
S Houghtelin 26-May-11 8:29am    
You asked for tutorials and some very good links have been provided already, you should read through them and try to apply what they tell you.

You will be a better coder for having solved it out on your own.

If you have been trying to do this, let us know where you are having problems by providing the code that you have come up with. We are not going to just write your program for you.
steven8Gerrard 27-May-11 4:21am    
Its not that i haven't tried . I couldn't understand a thing in those above articles . This is what i did . After creating form clicked on the read data button. It took me to code .

Added
using namespace System::IO::Ports;
using namespace System::Threading;
To header file

Added this below initialise components
static bool _continue;
static SerialPort^ _serialPort;

Wrote this in button eventhandler

_serialPort = gcnew SerialPort();
_serialPort->PortName = SetPortName(_serialPort->COM3);
_serialPort->BaudRate = SetPortBaudRate(_serialPort->9600);
_serialPort->Parity = SetPortParity(_serialPort->NOPARITY);
_serialPort->DataBits = SetPortDataBits(_serialPort->8);
_serialPort->StopBits = SetPortStopBits(_serialPort->ONESTOPBIT);
_serialPort->Open();
_continue = true;
_serialPort->ReadByte();
textBox1->Text = ReadByte.ToString();
}

And ran the program got 23 errors saying serial port undeclared identifier..


Someone help me


Richard MacCutchan 27-May-11 4:48am    
See my suggestions below. Also you say 'ran the program', but these are compiler errors, you are nowhere near running the program.
The first error above is the clue:
Error   1   error C3145: '_serialPort' : global or static variable may not have managed type 'System::IO::Ports::SerialPort ^'  d:\forms\rd\rd\Form1.h  32

You have declared a managed type as static which is illegal in C++/CLI, according to this message. You should make the variable a member of your class.

However, having read some of your other comments here I suspect that this subject is somewhat advanced for you. I would suggest you go back to your programming guides/ tutorials or other notes and try to get the whole concept of classes, objects and properties clear in your mind before attempting something like this.
 
Share this answer
 
I mean debug the program . Suggest me some basic tutorials .
I'm using VS 2008
 
Share this answer
 
Comments
Richard MacCutchan 27-May-11 8:54am    
If you are serious about becoming a developer in Windows technology then you really need to start by getting familiar with the MSDN site, and how to research information. For Visual Studio you can try this link to start with: http://msdn.microsoft.com/en-us/vstudio/default.aspx. From there you can also find your way to the Windows SDK and the C++ language.
I have managed to develop this program but have a doubt i hope someone can help me .

After creating the form .

SerialPort^ mySerialPort = gcnew SerialPort("COM3");

mySerialPort->BaudRate = 9600;
mySerialPort->Parity = Parity::None;
mySerialPort->StopBits = StopBits::One;
mySerialPort->DataBits = 8;
mySerialPort->Handshake = Handshake::None;
mySerialPort->Open();

int i=0;
richTextBox1->Text = "Testing\n";

while(i<10)
{
String^ read;
richTextBox1->AppendText(mySerialPort->ReadLine());
i++;
}
mySerialPort->Close();


This is what i have written for button . but i don't the values from serial port immediately . I get the value in the richtextbox only after while loop is finished .

What should i do to get value from port instantly and one of my seniors asked me to use callback instead of while loop . Search in web about callback . I dont know how to replace while with callback . I couldn't contact my senior thats why asking here .

Thanks in advance
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900