Tuesday, September 25, 2007

Serial soft interface in VC++ using System.IO.Ports.SerialPort .net object

My aim is to build a small GUI-programme able to read and to write strings on the COM port. This stuff is a good way to get familiar with Visual Studio to build graphical user interface and with the .Net object System.IO.Ports.SerialPort. This small piece of code is based on :
- System.IO.Ports.SerialPort.Open();
- System.IO.Ports.SerialPort.Close();
- System.IO.Ports.SerialPort.ReadLine();
- System.IO.Ports.SerialPort.WriteLine().



The graphical quite minimalist : a textbox to get the string to write on the serial connection from the user,
3 buttons :
- to open/close the COM port,
- to write on the COM port,
- to read on the COM port,
3 label :
- to retrieve the string red on the COM port,
- to retrieve the string written on the COM port,
- to retrieve port COM status (open/close).

AccesPortCOM (name of the programme) is really basic and easy to use. When you start it the COM port is closed. Just click on "Ouvrir" to open the connection. The port COM status label displays "Port COM ouvert". Type "une chaine" in the textbox and click on "ecrire". The write status label displays "écriture de une chaine sur le port COM...". If the textbox is empty the write status label displays "(rien à écrire)". Click on "lire" to read the reception buffer. If the reception buffer is empty, the write status label displays "lecture port COM impossible" and read status displays "(tampon RX vide)".

Note : the computer I'm using at the moment offers only one COM port, thus the programme is build to run on the COM1 port. It's not really generic but it works and that's enough to test System.IO.Ports.SerialPort :) ...


Here is the code :


/// Procédures gérant la laison série
/// Ouverture du port COM
void OpenPortCom(void){
try{
this->serialPort1->Open();
this->label1->Text= "Port COM ouvert";
}
catch(...){
this->label1->Text= "ouverture port COM impossible";
}
}

///Fermeture du port COM
void ClosePortCom(void){
try{
this->serialPort1->Close();
this->label1->Text= "Port COM fermé";
}
catch(...){
this->label1->Text= "fermeture port COM impossible";
}
}

///Lecture sur le port COM
void ReadPortCom(void){
try{
this->valeurLue = this->serialPort1->ReadLine();
this->label->Text= "lecture du port COM...";
}
catch(...){
this->label->Text= "lecture port COM impossible";
}
}

///Ecriture sur le port COM
void WritePortCom(void){
try{
if (this->valeurEcrite!=""){
this->serialPort1->WriteLine(valeurEcrite);
this->label->Text= "écriture de " + this->valeurEcrite +" sur le port COM...";
}
else {
this->label->Text= "(rien à écrire)";
}
}
catch(...){
this->label->Text= "écriture port COM impossible";
}
}

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
this->valeurEcrite = this->txtBoxEcriture->Text;
this->label->Text = "";
this->labelLecture->Text = "";
WritePortCom();
this->txtBoxEcriture->Text ="";
}

private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
this->label->Text = "";
ReadPortCom();
try{
this->labelLecture->Text = this->valeurLue->ToString();
}
catch(...){
this->labelLecture->Text = "(tampon RX vide)";
}
}

private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
if (this->ouvert==false){
this->button3->Text = "fermer";
OpenPortCom();
this->ouvert=true;
}
else if (this->ouvert==true){
this->button3->Text = "ouvrir";
ClosePortCom();
this->label->Text = "";
this->labelLecture->Text = "";
this->txtBoxEcriture->Text ="";
this->ouvert=false;

}
}

private: System::Void panel1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {
}
};
}


3 comments:

Anonymous said...

Lire le blog en entier, pretty good

Anonymous said...

You could easily be making money online in the hush-hush world of [URL=http://www.www.blackhatmoneymaker.com]seo blackhat[/URL], You are far from alone if you don't know what blackhat is. Blackhat marketing uses little-known or misunderstood avenues to build an income online.

Anonymous said...

[url=http://www.realcazinoz.com]casinos online[/url], also known as remarkable casinos or Internet casinos, are online versions of commonplace ("crony and mortar") casinos. Online casinos ease gamblers to assess as site in and wager on casino games to a t the Internet.
Online casinos customarily enunciate odds and payback percentages that are comparable to land-based casinos. Some online casinos denominate higher payback percentages in the fall back on to of repute railway carriage games, and some position upon known payout deal out exposed audits on their websites. Assuming that the online casino is using an fittingly programmed indefinitely consolidate up generator, catalogue games like blackjack get an established repress edge. The payout wedge after these games are established in the forefront the rules of the game.
Uncounted online casinos contract d‚mod‚ or flatter their software from companies like Microgaming, Realtime Gaming, Playtech, Cosmopolitan Ploy Technology and CryptoLogic Inc.