1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
#ifdef CODEBLOCKS
#include <windows.h>
#include <conio2.h>
#include <ctype.h>
#include "ed/inc/fifo.h"
#undef EOL
#define BASE 0x2F8
static unsigned char uart[8];
static sFIFO *fifo_rx;
static sFIFO *fifo_lsr;
static void sim_init (void)
{
ASSERT (FIFO_mode () == FIFO_MODE_DYN);
fifo_rx = FIFO_init (32);
fifo_lsr = FIFO_init (32);
ASSERT (fifo_rx != NULL);
ASSERT (fifo_lsr != NULL);
}
static void sim_end (void)
{
FIFO_end (fifo_rx);
FIFO_end (fifo_lsr);
}
static void sim_puts (char const *s)
{
char const *p = s;
ASSERT (fifo_rx != NULL);
ASSERT (fifo_lsr != NULL);
while (*p != 0)
{
FIFO_put (fifo_rx, *p);
FIFO_put (fifo_lsr, 0x01);
p++;
}
}
static void outportb (int port, int data)
{
printf ("[%04X]<-%02X\n", (unsigned) (port), (unsigned) (data));
uart[(port) - BASE] = data;
}
static int inportb (int port)
{
int data;
switch (port - BASE)
{
case 0: /* RX */
if (!FIFO_empty (fifo_rx))
{
data = FIFO_get (fifo_rx);
}
else
{
data = 0;
}
break;
case 5: /* LSR */
if (!FIFO_empty (fifo_lsr))
{
data = FIFO_get (fifo_lsr);
}
else
{
data = 0;
}
break;
default:
data = uart[(port) - BASE];
}
printf ("[%04X]==%02X", (unsigned) (port), (unsigned) data);
if (isprint (data))
{
printf (" '%c'", data);
}
printf ("\n");
return data;
}
#endif
/* ========================================================================= */
#include <stdio.h>
#include <string.h>
char trame_lue[41];
char resultat[6] = "";
char erreur[5] = "0000";
/*****************************************************************/
/* Init port srie : 8 bits donnes, 1 bit stop, parit aucune, */
/* 9600 bauds et positionnement de DLAB pour faire la slection */
/* de la vitesse de transmission */
/*****************************************************************/
void init_com2 (void)
{
/****************************/
/* Registres UART port COM2 */
/****************************/
#define ESC 0x1b
#define THR_RBR 0x2F8
#define IER 0x2F9
#define IIR 0x2FA
#define LCR 0x2FB
#define MCR 0x2FC
#define LSR 0x2FD
#define MSR 0x2FE
/**********************/
/* Bites de registres */
/**********************/
#define DLAB 0x80
#define RAZ_DLAB (~DLAB)
/****************************/
/* Paramtres de la liaison */
/****************************/
#define DONNE_8 0x03
#define STOP_1 0x00
#define NONE 0x00
#define BAUDS_9600 12
unsigned init_liaison;
outportb (IER, 0x01);
init_liaison = DONNE_8 | STOP_1 | NONE | DLAB;
outportb (LCR, init_liaison);
/******************************************/
/* Configuration de la vitesse 9600 bauds */
/* et RAZ bit 7 du LCR */
/******************************************/
outportb (THR_RBR, BAUDS_9600);
init_liaison &= RAZ_DLAB;
/********************************************/
/* Envoie a nouveau la configuration au LCR */
/* avec le bit 7 (DLAB) a 0 */
/********************************************/
outportb (LCR, init_liaison);
}
/*****************************************************/
/* Sous programme ecriture de la tame dans 1 fichier */
/*****************************************************/
void ecriture_fichier (void)
{
#ifdef CODEBLOCKS
FILE *fichier = fopen ("C:/dev/forums2/data.txt", "w");
#else
FILE *fichier = fopen ("C:\\aauser\\data.txt", "w");
#endif
if (fichier == NULL)
{
printf (" impossible d'ecrire\n");
}
else
{
printf ("'%s'\n", resultat);
fprintf (fichier, "%s", resultat);
fclose (fichier);
}
}
/**********************************/
/* Controle des caracteres perdus */
/**********************************/
static void controle_caract (void)
{
char const trame_ref1[] = "<16>:(PB): ";
char const trame_ref2[] = " l/m";
int compare = strncmp (trame_lue + 0, trame_ref1, sizeof trame_ref1 - 1);
if (compare == 0)
{
compare = strncmp (trame_lue + 15, trame_ref2, sizeof trame_ref2 - 1);
if (compare != 0)
{
strcpy (resultat, erreur);
}
}
else
{
strcpy (resultat, erreur);
}
ecriture_fichier ();
}
/************************/
/* Programme principale */
/************************/
int main (void)
{
init_com2 ();
clrscr ();
/***********************/
/* Boucle de reception */
/***********************/
/* Declaration des variables et de la trame
mise a 0 de l'indice */
#define EOL 10
int i = 0;
#ifdef CODEBLOCKS
sim_init ();
sim_puts ("<16>:(PB): 1234 l/m\n");
#endif
/* Boucle infinie */
while (1)
{
int c;
/* Reception caractere */
if (inportb (LSR) & 0x01)
{
c = inportb (THR_RBR);
/* Si EOL atteint */
if (c == EOL)
{
/* Fermeture de la chaine */
trame_lue[i] = 0;
/* Pres a recevoir une nouvelle trame */
i = 0;
/* Traitement de la trame */
strncat (resultat, trame_lue + 11, 4);
controle_caract ();
}
else
{
trame_lue[i] = c;
i++;
/* Programmation du garde fou */
if (i > 41)
{
i = 0;
}
}
}
/* clavier */
if (kbhit ())
{
int c = getch ();
if (c == ESC)
{
break;
}
}
/* delay */
#ifdef CODEBLOCKS
Sleep (1000);
#endif
}
#ifdef CODEBLOCKS
sim_end ();
#endif
return 0;
} |
Partager