- 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 :
- 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) {
}
};
}