#include <bios.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <mem.h>
#include <windows.h>
#define BAUD2400 0
#define BIT7PAR 0 /* 7 bits paridad PAR */
int lin,status;
long precio,peso;
char tx[11],indmen;
char rx[21];
char aux[6];
char recibi;
unsigned char lrc,errorp;
#define COM1 0 /* 0-COM1 1-COM2 */
#define CR 0x0d
#define LF 0x0a
#define TRUE 1
#define FALSE 0
#define DATA_READY 0x100
#define PARI_ERROR 0x400
#define FRAM_ERROR 0x800
char mensaje (void);
int main(void)
{
int i;
unsigned int nerror,ntx,nrx;
char vez,error,tecla;
vez = 0;
nerror = 0;
ntx = 0;
nrx = 0;
recibi = 0;
clrscr();
lin = 1;
#if BAUD2400
bioscom(0,0xa0 | 0x03 | 0x00 | 0x00,COM1); /* 2400, 8 datos, 1 stop */
#else
#if BIT7PAR
bioscom(0,0xe0 | 0x02 | 0x18 | 0x00,COM1); /* 9600, 7 datos, par, 1 stop */
#else
bioscom(0,0xe0 | 0x03 | 0x00 | 0x00,COM1); /* 9600, 8 datos, 1 stop */
/* bioscom(0,0xe0 | 0x03 | 0x00 | 0x00,COM1); 9600, 8 datos, 1 stop */
#endif
#endif
while (1)
{
if (vez == 0)
{
gotoxy( 2,lin); cprintf("Precio = ");
gotoxy(11,lin); scanf("%05ld",&precio);
vez = 1;
}
if (kbhit() != 0) /* Cualquier tecla para salir */
{
if ((tecla = getch()) != 0x20)
break;
}
error = 0;
if (tecla == 0x20)
{
tecla = 0x00;
precio++;
sprintf(tx,"98%05ld",precio);
lrc = 0x00;
for (i=0;i<7;i++)
lrc ^= tx[i];
tx[7] = lrc;
tx[8] = CR;
tx[9] = LF;
for (i=0;i<10;i++) /* Enviar tx */
// bioscom(1,tx[i],COM1);
ntx++;
indmen = 0;
}
while (mensaje() == FALSE)
{
if (kbhit() != 0)
break;
}
if (recibi == TRUE)
{
indmen = 0;
recibi = FALSE;
nrx++;
memmove(aux,&rx[3],5);
aux[5] = 0x00;
peso = atol(aux);
gotoxy( 2,lin); cprintf("Precio = ");
gotoxy(11,lin); cprintf("%05ld",precio);
gotoxy(20,lin); cprintf("Peso = %.1s %.5s",&rx[2],&rx[3]);
gotoxy(40,lin); cprintf("Imp. = %.1s %.6s",&rx[8],&rx[9]);
if (rx[0] != '9' || rx[1] != '9')
error = 1;
lrc = 0x00;
for (i=0;i<15;i++)
lrc ^= rx[i];
if (lrc != rx[15])
error = 1;
if (error == 1)
{
nerror++;
gotoxy(58,lin); cprintf("ERROR");
}
if (errorp == 1) /* Error de paridad */
{
gotoxy(67,lin);
cprintf("ERROR");
errorp = 0;
}
gotoxy(1,22); cprintf("NTX = %drnNRX = %drnERR = %d",ntx,nrx,nerror);
lin++;
if (lin > 20)
{
clrscr();
lin = 1;
}
}
Sleep(100);
}
}
char mensaje(void)
{
int i;
status = bioscom(3,0,COM1);
if (status & DATA_READY)
{
if (status & PARI_ERROR)
errorp = 1;
i = bioscom(2,0,COM1); /* Recibir dato */
}
else
return(FALSE);
rx[indmen++] = i;
if (i == 'n')
{
recibi = TRUE;
return(TRUE);
}
recibi = FALSE;
return(FALSE);
}