Vous êtes sur la page 1sur 9

RENFORCEMENT DE CAPACITÉ sur

Programmation avec l'API Win32 et UART

Objectifs :
+ UART en général
+ UART pour les MCU ATmega
+ UART pour le système Windows + API Win32
+ GUI: sur le contrôle COMBOBOX

Ny UART dia protocole de communication na hoe fomba fampi-


fandraisana circuit intégré numérique anankiroa na maromaro. Pro-
tocôles hafa: USB, I2C na two wire interface = TWI, ethernet, etc.
Io no anisan'ny protocole efa ela indrindra, ary mora ampiasaina
ihany koa. Betsaka @ fitaovana taloha no mampiasa azy (MODEM, ap-
pareils de mesure divers, ...)
Antsoina hoe asynchrone izy satria tsy mampiasa signal de synchro-
nisation mitokana. Seriel izy satria fil vitsy no ampiasaina ka alefa mi-
fanesy (sahala @ tetezana tsy mahazaka afa-tsy olon-tokana ireny) izany
hoe @ fotoana iray dia tension iray (un bit à chaque fois).
Ny faharetan'ny tension ts1/1 eo @ fil no ifanarahan'ny émetteur sy
récepteur. Io no antsoina hoe dt = 1/baud.

Noho ny mbola tsy fahafahan'ny fitaovana maro mampiasa USB, ary


noho ny lazan'ny UART hatrizay dia misy ny:
- création de PORTS série virtuels grâce aux pilotes
- logiciels de capture de données sur ports série na terminal
Ohatra: RealTerm, CoolTerm, HyperTerminal, ...
- circuit appelé: serial engine, circuit mamadika USB ←→ serial
Ohatra: FTDI232, CP210x, CH340, ...
NB. Miova arakaraka io circuit serial engine io ihany koa ny pilote ilaina
ahafantaran'ny système d'exploitation azy.

NB. Tsy mitovy mihitsy ny anaran'ny PORTS série @ Windows sy @


Linux.
@ Windows: COM1, COM2, ..., COM25, ... (ny anarana fichier tsy azo
atao hoe COM @ Windows)

1
@ Linux: tty, ...

Misy logiciels maro ahafahana mi-gérer ports série. PySerial ohatra dia
bibliothèque Python multi-plateforme, azo ampiasaina tsara @ capture
de données na zavatra hafa mifandraika @ ports séries.

sary 1. interface de CoolTerm sous Windows

Faharoa.
UART ho an'ny ATmega. Ny ankamaroan'ny ATmega dia misy pé-
riphérique UART ary tsisy USB natif. Ireo dia itovizany @ microcon-
trôleurs rehetra misy, satria sarotra ny protocôle USB.
Sahala @ périphérique rehetra dia misy SFR ho an'ny contrôle, ho
an'ny statut, ho an'ny data.
NB. Afaka mi-simuler UART tsotra daholo ny MCU rehetra, na misy na
tsisy UART ny périphérique ao aminy (ohatra: PIC16F84, PIC12F675,
etc. ) Ary @ Arduino moa dia misy ny Software Serial izay afaka
mampiasa broches tsotra ho lasa Tx sy Rx

2
ATmega32
Serial Engine
DTR
TXD

Vers PC RXD
+5V
GND
3V3
Gnd

TxD

ISP
sary 2. fampiasana ny CP2101 miaraka amina MCU

3
sary 3. ireo erreur de baud rate araka ny Fquartz

Fahatelo. UART pour le système Windows grâce à l'API Win32


Rehefa misy ny port série virtuel, dia tsy miova ny fomba fampiasa
an'ireo fonctions API Win32 mikasika ny ports séries ireo.
NB. Ho an'ny Windows manokana dia ny article-n'i Allen Denver 1995
tao @ MSDN no fanazavana ampy indrindra mahakasika ny gestion de
port série. Misy codes sources miaraka aminy moa io article io natao
démonstration. Ka tsy vitsy ny olona naka traikefa tam'izany, omeko
eto ireo artcile + fichiers ireo. Ankehitriny, efa tsy eken'ny Microsoft
ireo, ary porofon'izany ny fanafoanany ilay codes sources tao @ site
MSDN efa taona maro lasa. MTTTY na koa hoe MultiThreaded Terminal
no anaran'ilay programme.

4
sary 4. interface du MTTTY sous Windows

Misy kosa article vaovao AN595: migrating from UART to USB, navoakan'i
Microchip momba ny fampiasana ny PIC18F misy USB natif.

Inspiré tao @ article io ny ankamaroan'ny fanazavana eto.


Fonctions de l'API:
* CreateFile: manokatra fichier, na io fichier disque, na io port de
communication

Syntaxe de CreateFile API


Doc officiel

HANDLE CreateFile(

LPCTSTR lpFileName, // pointer to name of the file


DWORD dwDesiredAccess, // access (read-write) mode
DWORD dwShareMode, // share mode
LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security attribu
DWORD dwCreationDistribution, // how to create

5
DWORD dwFlagsAndAttributes, // file attributes
HANDLE hTemplateFile // handle to file with attributes to copy
);

Toy izao ireo paramètres fito:

1. nom du port ←→ chaîne de char

2. mode d'ouverture : en lecture, en écriture, etc. ←→ constantes


prédédinies, ex: GENERIC_READ, GENERIC_WRITE

3. mode d'accès partagé avec d'autres processus ←→ constantes


prédéf, ex: FILE_SHARE_READ

4. SECURITY_ATTRIBUTES

5. mode de création: ←→ constantes prédéfinies. ohatra hoe fichier


hovakiana, dia mi-exister izany, dia atao hoe: OPEN_EXISTING.
Raha fichier vao hoforonina dia : CREATE_NEW, raha sendra efa
misy ao dia manao erreur. Fa afaka atao hoe CREATE_ALWAYS ,
raha sendra efa misy ao dia voafafa.

6. attributs du fichier ←→ ohatra hoe archive, caché, normal, etc.


FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_HIDDEN,
FILE_ATTRIBUTE_NORMAL, etc.
Mbola afaka ampiana hoe: FILE_FLAG_OVERLAPPED ireo rehefa
opérations asynchrones no atao, izany hoe tsy miandry opérations
ho tapitra ny fonctions E/S fa tonga dia mi-return (fanazavana eo
ambany)

7. resaka fichier temporaire io hTemplateFile io

Fa mila arahina ireto paramètres ireto (am' ity démo ity):


- GENERIC_READ | GENERIC_WRITE
- OPEN_EXISTING
- hTemplateFile = NULL
=⇒ araho io exemple eo ambany io
* WriteFile, ReadFile: mamaky na manoratra na koa hoe recevoir/émet-
tre des données sur le port série
* CloseHandle : fermer le fichier, izany hoe fermer le PORT raha port
ilay raha

6
BOOL WriteFile(
HANDLE hFile, // handle to file to write to
LPCVOID lpBuffer, // pointer to data to write to file
DWORD nNumberOfBytesToWrite, // number of bytes to write
LPDWORD lpNumberOfBytesWritten, // pointer to number of bytes written
LPOVERLAPPED lpOverlapped // pointer to structure needed for overlapped
);

dans l'ordre:
handle du fichier retourné par CreateFile,
lpBuffer: pointeur vers les données à écrire
nNumberOfBytesToWrite: nombre d'octets
lpNumberOfBytesWritten: pointeur vers une variable pour stocker
le nombre d'octets écrits
lpOverlapped: pointeur vers une strucuture OVERLAPPED pour une
opération asynchrone

Ny ataon'ity fonction ity dia mi-lister ireo ports séries disponibles. Mar-
ihina fa ny ordinateur de bureau taloha na ordi portable taloha no misy
ports séries tena izy, na ilay DB9. Ny portable @ zao fotoana dia tsisy
ports séries intsony fa rehefa mitsatoka ny Arduino na matériel hafa
mampiasa ports séries virtuels, izay vao misy COM5 na COMx hafa hita
après an'ity.
Fiasany: andramana daholo COM1 à COM255 dia tazomina anaty
tableau ireo izay VALID izany hoe 6= INVALID_HANDLE_VALUE.
NB. mila asiana an'io préfixe io ny anarany raha te hahita ny ports
rehetra ohatra hoe COM25 : ("\\\\.\\COM%u"), i);

source d'inspiration:

Enumerating com ports in Windows


original by PJ Naughter, 1998 - 2013
http://www.naughter.com/enumser.html
(Web: www.naughter.com, Email: pjna@naughter.com)

/* if ret = TRUE
- pNumber store the number of COM port found
- pPortName contains their names
*/
INT EnumerateComPortByCreateFile(
TCHAR pPortName[8][20],

7
int strMaxLen)

{
INT i, jj=0, pNumber=0;
TCHAR pTempPortName[256];

for (i = 1; i<= 255 && (pNumber<8); i++)


{
//_stprintf_s(pTempPortName, strMaxLen,...
wsprintf(pTempPortName, TEXT("\\\\.\\COM%u"), i);

hSerial[jj] = CreateFile(pTempPortName,
GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL);

if (hSerial[jj] == INVALID_HANDLE_VALUE)
continue;

lstrcpy(pPortName[jj], pTempPortName);
jj++;
pNumber = jj;
}
//HeapFree(GetProcessHeap(), 0, pTempPortName);
//pTempPortName = NULL;
// *pNumber = jj;
return jj; //ret;

}/*EnumerateComPortByCreateFile*/

Momba ny COMBOBOX. Izy ity dia contrôle ahafahana mi-lister dy-


namiquement, liste déroulante. Eto izy dia afaka ampiasaina hi-lister ny
ports existants
NB. Mila comctl32 izy ity (common controle) vao mandeh
ALohan'io création io dia mila mi-initialiser variable:
INITCOMMONCONTROLSEX iccex; // ,

ary mila ampiana comctl32.a ny option d'édition de lien


// combo ctrl creation
combo_Commport = CreateWindow(
"COMBOBOX",

8
"...",
WS_CHILD|WS_VISIBLE|CBS_AUTOHSCROLL|CBS_DROPDOWN,
xpos, // ex: 200
ypos, // ex: 200
largeur, // ex: 120
hauteur, // ex: 24 ou 340
hwnd, // handle de la fenetre parent (=principale)
(HMENU) 0,
(HINSTANCE) NULL,
(LPVOID) 0
);

NB. Eto dia tsara marihina ny hoe OVERLAPPED operation. @ Windows


no misy an'io vocabulaire io fa misy équivalent tsara hoe : OVERLAPPED
= ASYNCHRONE.
Ny opération dia asynchrone rehefa tsy eo no ho eo: na hoe tsy
immédiat. Ohatra: écriture de données dans un fichier. Rehefa atao
ny WriteFile() dia tsy voatery andrasana ilay écriture définitive. Sa-
tria ny contrôleur de disque mbola manao opération matérielle anazy
manokana izay tsy misy idiran'ny processeur. Ka azo atao ny miandry
an'iny ho vita vao mi-retourne ilay fonction WriteFile. Amin'izay dia sa-
hala @ hoe bloqué mandritra fotoana fohy ilay processus miandry ilay
matériel.
Izay no antsoina hoe NON OVERLAPPED na koa hoe SYNCHRONE.
Mifanohitra amin'izay kosa ny hoe: mi-retourne avy hatrany ilay
WriteFile fa misy événement kosa afaka récupérer-na avy eo milaza ny
succés/erreur an'ilay opération. Izay no antsoina hoe: OVERLAPPED na
koa hoe ASYNCHRONE.
Raha tena hanao réception de données haingana dia io OVERLAPPED
io no atao, saingy tsy idirana lalina eto.

Vous aimerez peut-être aussi