Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Check out this C code

Status
Not open for further replies.

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.
 
You should have surrounded your code with the Code button on the posting form, that would have prevented your 'smiley' problem, and keeps the formatting correct.

Anyway:

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.

I'm presuming it's ANDing the value with 127 because standard ASCII is only seven bit, so the eighth bit should always be zero. This is why binary files have to be encoded to send as email, because binary files are 8 bit, and many email systems will only pass seven bits!.

I'm also presuming the code is for DOS?.
 
I'm presuming it's ANDing the value with 127 because standard ASCII is only seven bit, so the eighth bit should always be zero. This is why binary files have to be encoded to send as email, because binary files are 8 bit, and many email systems will only pass seven bits!.

Well this code is intended to run under Turbo C++ 3.0 under the 16 bit MS DOS system.
I guess then I need to truncate the 8th bit in my PIC before I send it..so that it will be received...but can i not receive the byte as a binary format..cause we are specifying 8 bit char, even parity..1 stop bits etc. I think C should take 8 bits also... or no ??
 
I think the MSB only gets stripped of to get a printable ASCII character that the putch() function can output...

The serial receive function actually receives the complete byte, but ASCII char's over 128 have a lot of non printable characters...
 
Exo said:
I think the MSB only gets stripped of to get a printable ASCII character that the putch() function can output...

The serial receive function actually receives the complete byte, but ASCII char's over 128 have a lot of non printable characters...

Well, i need to send out bits from the PIC that represent numbers. ie: I need to send out 0.1, 0.7, 1.0 and so on. I shall take care of the decimal part by sending the ascii value and then I'll send the decimal digit as 8 bits.
eg: For displaying 0.7 , i'll send 2E (which in ascii is '.')first and then 7(00000111)
So my question is will the 'printf' command be able to do this ?? :roll:
 
Electrix said:
Exo said:
I think the MSB only gets stripped of to get a printable ASCII character that the putch() function can output...

The serial receive function actually receives the complete byte, but ASCII char's over 128 have a lot of non printable characters...

Well, i need to send out bits from the PIC that represent numbers. ie: I need to send out 0.1, 0.7, 1.0 and so on. I shall take care of the decimal part by sending the ascii value and then I'll send the decimal digit as 8 bits.
eg: For displaying 0.7 , i'll send 2E (which in ascii is '.')first and then 7(00000111)
So my question is will the 'printf' command be able to do this ?? :roll:

Easiest thing to do (and to ensure maximum compatibility) is to send ASCII characters from the PIC, the PC program above will display it perfectly then, simply as it is (as would HyperTerminal under Windows).

If you check my RS232 PIC tutorials I have routines which do just that!.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top