Electrix
Member
Hi - I have seen this code in a magazine for communication with PC using RS 232 and C. I plan to use this code for my PIC to transfer data to PC.
But I have a doubt---
In the if(((out=_bios_serialcom(_COM_RECEIVE,port,0))&127)!=0)
why are we 'and'ing the received(return value) with 127 ??. It is understood that if all the upper 8 bits of the return value are 0, there is no error in the received byte.
#include <stdio.h>
#include <conio.h>
#include <bios.h>
#define SETTINGS (_COM_9600 | _COM_CHR8 | _COM_NOPARITY | _COM_STOP1)
/*BAUD RATE=9600, 8 BITS, NO PARITY, 1 STOP BIT */
void main(void)
{
unsigned in,out,status;
int port;
clrscr();
printf("Select Port(Enter '0' for COM1 and '1' for COM2):");
scanf("%d",&port);
printf("Press ESC to exit");
textcolor(YELLOW);
cprintf("\nData Received:");
_bios_serialcom(_COM_INIT,port,SETTINGS);
for(;
{
status=_bios_serialcom(_COM_STATUS,port,0);
if(status&256) /*if data ready */
{
if(((out=_bios_serialcom(_COM_RECEIVE,port,0))&127)!=0)
putch(out); }
if(kbhit()) /*if a keystroke is currently available*/
{
in=getch(); /*get a character without echoing onto the screen*/
if(in==27)/*ESC*/
break;
_bios_serialcom(_COM_SEND,port,in);
}
}
}
P.S.: Ignore the smiley, its beacuse of the for loop syntax.
But I have a doubt---
In the if(((out=_bios_serialcom(_COM_RECEIVE,port,0))&127)!=0)
why are we 'and'ing the received(return value) with 127 ??. It is understood that if all the upper 8 bits of the return value are 0, there is no error in the received byte.
#include <stdio.h>
#include <conio.h>
#include <bios.h>
#define SETTINGS (_COM_9600 | _COM_CHR8 | _COM_NOPARITY | _COM_STOP1)
/*BAUD RATE=9600, 8 BITS, NO PARITY, 1 STOP BIT */
void main(void)
{
unsigned in,out,status;
int port;
clrscr();
printf("Select Port(Enter '0' for COM1 and '1' for COM2):");
scanf("%d",&port);
printf("Press ESC to exit");
textcolor(YELLOW);
cprintf("\nData Received:");
_bios_serialcom(_COM_INIT,port,SETTINGS);
for(;
{
status=_bios_serialcom(_COM_STATUS,port,0);
if(status&256) /*if data ready */
{
if(((out=_bios_serialcom(_COM_RECEIVE,port,0))&127)!=0)
putch(out); }
if(kbhit()) /*if a keystroke is currently available*/
{
in=getch(); /*get a character without echoing onto the screen*/
if(in==27)/*ESC*/
break;
_bios_serialcom(_COM_SEND,port,in);
}
}
}
P.S.: Ignore the smiley, its beacuse of the for loop syntax.