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


Wednesday, August 8, 2007

Using Matlab to generate a VHDL ASCII to Binary transcoder

ASCII character aren't so easy to manipulate in VHDL. In fact I started to use it as a native type of VHDL but in using QUARTUS VHDL compiler we can't output character. We can't build entity with "complex type" outputs or inputs. I try to cast "character" to "std_logic_vector" but we can't do it with "std_logic_1164.all", "std_logic_unsigned.all", "std_logic_arith.all" and "numeric_std.all" ieee library.

I saw
this 'quick&very dirty way' solution wich enable to create you own library. I find the idea good but the code is limited to a small amount of characters and its quite tiresome to add the other characters.

Here is a small Matlab script to generate the "case" structure with the whole ASCII characters.

FileID = fopen('transcoderASCII2Binary.h','w');

fprintf(FileID, '\t\tcase char is\n');

for i=32:127
fprintf(FileID, '\t\t when ''%s''=> \n\t\t\t data <= "%s"; \n',char(i), dec2bin(i,8)); end fprintf(FileID, '\t\t when others => \n\t\t\t data <= "00000000"; \n'); fprintf(FileID, 'end case;'); fclose(FileID); And here is the result :


case char is
when ' '=>
data <= "00100000";
when '!'=>
data <= "00100001";
when '"'=>
data <= "00100010";
when '#'=>
data <= "00100011";
when '$'=>
data <= "00100100";
[............]
when '~'=>
data <= "01111110";
when ''=>
data <= "01111111";
when others =>
data <= "00000000";
end case;

Tuesday, June 19, 2007

Script in perl to generate content

I'm new to perl and i'm trying to get used with it in wrinting a tiny script wich will help me to translate some article for aving.net in french. I just have to put information in the form and then clicking on write, and the script give the title, a summary and the content of the article.

For now it's quite simple, and it always give you back the same kind of article. But latter we can imagine to give the script several tamplates of articles and letting him chose randomly bettween those tamplates.

Anyways, it's a nice application to get familiar with perl.