ºìÁªLinuxÃÅ»§
Linux°ïÖú

¹ØÓÚLinuxÏ´®¿ÚͨÐŵÄÒ»µãÐĵÃ

·¢²¼Ê±¼ä:2007-10-29 00:41:35À´Ô´:ºìÁª×÷Õß:gfhlole
Õ⼸Ì죬ÓÉÓÚ³¤´ºÃżìϵͳÏîÄ¿µÄÐèÒª£¬Éæ¼°µ½Á˶Á¿¨Æ÷ÐÅÏ¢µÄ´®¿Ú¶ÁÈ¡£¬ËùÒÔÔÚLinuxÏ´®¿ÚÐÅÏ¢µÄ¶ÁÈ¡ÓÐÁËÒ»µãÐĵÃÌå»á¡£

1. ´ò¿ª´®¿Ú

ÓëÆäËûµÄ¹ØÓÚÉ豸±à³ÌµÄ·½·¨Ò»Ñù£¬ÔÚLinuxÏ£¬²Ù×÷¡¢¿ØÖÆ´®¿ÚÒ²ÊÇͨ¹ý²Ù×÷ÆðÉ豸Îļþ½øÐеġ£ÔÚLinuxÏ£¬´®¿ÚµÄÉ豸ÎļþÊÇ/dev/ttyS0»ò/dev/ttyS1µÈ¡£Òò´ËÒª¶Áд´®¿Ú£¬ÎÒÃÇÊ×ÏÈÒª´ò¿ª´®¿Ú£º

char *dev = "/dev/ttyS0"; //´®¿Ú1

int fd = open( dev, O_RDWR );

//| O_NOCTTY | O_NDELAY

if (-1 == fd)

{

perror("Can't Open Serial Port");

return -1;

}

else

return fd;



2. ÉèÖô®¿ÚËÙ¶È

´ò¿ª´®¿Ú³É¹¦ºó£¬ÎÒÃǾͿÉÒÔ¶ÔÆä½øÐжÁдÁË¡£Ê×ÏÈÒªÉèÖô®¿ÚµÄ²¨ÌØÂÊ£º

int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,

B38400, B19200, B9600, B4800, B2400, B1200, B300, };

int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,

19200, 9600, 4800, 2400, 1200, 300, };

void set_speed(int fd, int speed){

int i;

int status;

struct termios Opt;

tcgetattr(fd, &Opt);

for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {

if (speed == name_arr[i]) {

tcflush(fd, TCIOFLUSH);

cfsetispeed(&Opt, speed_arr[i]);

cfsetospeed(&Opt, speed_arr[i]);

status = tcsetattr(fd, TCSANOW, &Opt);

if (status != 0) {

perror("tcsetattr fd");

return;

}

tcflush(fd,TCIOFLUSH);

}

}

}

3. ÉèÖô®¿ÚÐÅÏ¢

ÕâÖ÷Òª°üÀ¨£ºÊý¾Ýλ¡¢Í£Ö¹Î»¡¢ÆæżУÑéλÕâЩÖ÷ÒªµÄÐÅÏ¢¡£

/**

*@brief ÉèÖô®¿ÚÊý¾Ýλ£¬Í£Ö¹Î»ºÍЧÑéλ

*@param fd ÀàÐÍ int ´ò¿ªµÄ´®¿ÚÎļþ¾ä±ú

*@param databits ÀàÐÍ int Êý¾Ýλ È¡Öµ Ϊ 7 »òÕß8

*@param stopbits ÀàÐÍ int ֹͣλ ȡֵΪ 1 »òÕß2

*@param parity ÀàÐÍ int ЧÑéÀàÐÍ È¡ÖµÎªN,E,O,,S

*/

int set_Parity(int fd,int databits,int stopbits,int parity)

{

struct termios options;

if ( tcgetattr( fd,&options) != 0) {

perror("SetupSerial 1");

return(FALSE);

}

options.c_cflag &= ~CSIZE;

options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/

options.c_oflag &= ~OPOST; /*Output*/



switch (databits) /*ÉèÖÃÊý¾ÝλÊý*/

{

case 7:

options.c_cflag |= CS7;

break;

case 8:

options.c_cflag |= CS8;

break;

default:

fprintf(stderr,"Unsupported data size\n"); return (FALSE);

}

switch (parity)

{

case 'n':

case 'N':

options.c_cflag &= ~PARENB; /* Clear parity enable */

options.c_iflag &= ~INPCK; /* Enable parity checking */

break;

case 'o':

case 'O':

options.c_cflag |= (PARODD | PARENB); /* ÉèÖÃΪÆæЧÑé*/

options.c_iflag |= INPCK; /* Disnable parity checking */

break;

case 'e':

case 'E':

options.c_cflag |= PARENB; /* Enable parity */

options.c_cflag &= ~PARODD; /* ת»»ÎªÅ¼Ð§Ñé*/

options.c_iflag |= INPCK; /* Disnable parity checking */

break;

case 'S':

case 's': /*as no parity*/

options.c_cflag &= ~PARENB;

options.c_cflag &= ~CSTOPB;break;

default:

fprintf(stderr,"Unsupported parity\n");

return (FALSE);

}

/* ÉèÖÃֹͣλ*/

switch (stopbits)

{

case 1:

options.c_cflag &= ~CSTOPB;

break;

case 2:

options.c_cflag |= CSTOPB;

break;

default:

fprintf(stderr,"Unsupported stop bits\n");

return (FALSE);

}

/* Set input parity option */

if (parity != 'n')

options.c_iflag |= INPCK;

tcflush(fd,TCIFLUSH);

options.c_cc[VTIME] = 0; /* ÉèÖó¬Ê±0 seconds*/

options.c_cc[VMIN] = 13; /* define the minimum bytes data to be readed*/

if (tcsetattr(fd,TCSANOW,&options) != 0)

{

perror("SetupSerial 3");

return (FALSE);

}

return (TRUE);

}

ÔÚÉÏÊö´úÂëÖУ¬ÓÐÁ½¾ä»°ÌرðÖØÒª£º

options.c_cc[VTIME] = 0; /* ÉèÖó¬Ê±0 seconds*/

options.c_cc[VMIN] = 13; /* define the minimum bytes data to be readed*/

ÕâÁ½¾ä»°¾ö¶¨Á˶Դ®¿Ú¶ÁÈ¡µÄº¯Êýread()µÄһЩ¹¦ÄÜ¡£ÎÒ½«×ÅÖؽéÉÜÒ»ÏÂËûÃǶÔread()º¯ÊýµÄÓ°Ïì¡£

¶Ô´®¿Ú²Ù×÷µÄ½á¹¹ÌåÊÇ

Struct{

tcflag_t c_iflag; /*ÊäÈëģʽ±ê¼Ç*/

tcflag_t c_oflag; /*Êä³öģʽ±ê¼Ç*/

tcflag_t c_cflag; /*¿ØÖÆģʽ±ê¼Ç*/

tcflag_t c_lflag; /*±¾µØģʽ±ê¼Ç*/

cc_t c_line; /*Ïß·¹æ³Ì*/

cc_t c_cc[NCCS]; /*¿ØÖÆ·ûºÅ*/

}£»

ÆäÖÐcc_t c_lineÖ»ÓÐÔÚһЩÌØÊâµÄϵͳ³ÌÐò(±ÈÈ磬ÉèÖÃͨ¹ýttyÉ豸À´Í¨ÐŵÄÍøÂçЭÒé)ÖвŻáÓá£ÔÚÊý×éc_ccÖÐÓÐÁ½¸öϱê(VTIMEºÍVMIN)¶ÔÓ¦µÄÔªËز»ÊÇ¿ØÖÆ·û£¬²¢ÇÒÖ»ÊÇÔÚԭʼģʽÏÂÓÐЧ¡£Ö»ÓÐÔÚԭʼģʽÏ£¬ËûÃǾö¶¨ÁËread()º¯ÊýÔÚʲôʱºò·µ»Ø¡£ÔÚ±ê׼ģʽÏ£¬³ý·ÇÉèÖÃÁËO_NONBLOCKÑ¡Ï·ñÔòÖ»Óе±Óöµ½Îļþ½áÊø·û»ò¸÷ÐеÄ×Ö·û¶¼ÒѾ­±à¼­Íê±Ïºó²Å·µ»Ø¡£

¿ØÖÆ·ûVTIMEºÍVMINÖ®¼äÓÐן´ÔӵĹØϵ¡£VTIME¶¨ÒåÒªÇóµÈ´ýµÄÁãµ½¼¸°ÙºÁÃëµÄʱ¼äÁ¿(ͨ³£ÊÇÒ»¸ö8λµÄunsigned char±äÁ¿£¬È¡Öµ²»ÄÜ´óÓÚcc_t)¡£VMIN¶¨ÒåÁËÒªÇóµÈ´ýµÄ×îС×Ö½ÚÊý(²»ÊÇÒªÇó¶ÁµÄ×Ö½ÚÊý----read()µÄµÚÈý¸ö²ÎÊý²ÅÊÇÖ¸¶¨ÒªÇó¶ÁµÄ×î´ó×Ö½ÚÊý)£¬Õâ¸ö×Ö½ÚÊý¿ÉÄÜÊÇ0¡£

l Èç¹ûVTIMEÈ¡0£¬VMIN¶¨ÒåÁËÒªÇóµÈ´ý¶ÁÈ¡µÄ×îС×Ö½ÚÊý¡£º¯Êýread()Ö»ÓÐÔÚ¶ÁÈ¡ÁËVMIN¸ö×Ö½ÚµÄÊý¾Ý»òÕßÊÕµ½Ò»¸öÐźŵÄʱºò²Å·µ»Ø¡£

l Èç¹ûVMINÈ¡0£¬VTIME¶¨ÒåÁ˼´Ê¹Ã»ÓÐÊý¾Ý¿ÉÒÔ¶ÁÈ¡£¬read()º¯Êý·µ»ØÇ°Ò²ÒªµÈ´ý¼¸°ÙºÁÃëµÄʱ¼äÁ¿¡£Õâʱ£¬read()º¯Êý²»ÐèÒªÏñÆäͨ³£Çé¿öÄÇÑùÒªÓöµ½Ò»¸öÎļþ½áÊø±êÖ¾²Å·µ»Ø0¡£

l Èç¹ûVTIMEºÍVMIN¶¼²»È¡0£¬VTIME¶¨ÒåµÄÊǵ±½ÓÊÕµ½µÚÒ»¸ö×Ö½ÚµÄÊý¾Ýºó¿ªÊ¼¼ÆËãµÈ´ýµÄʱ¼äÁ¿¡£Èç¹ûµ±µ÷ÓÃreadº¯Êýʱ¿ÉÒԵõ½Êý¾Ý£¬¼ÆʱÆ÷ÂíÉÏ¿ªÊ¼¼Æʱ¡£Èç¹ûµ±µ÷ÓÃreadº¯Êýʱ»¹Ã»ÓÐÈκÎÊý¾Ý¿É¶Á£¬ÔòµÈ½ÓÊÕµ½µÚÒ»¸ö×Ö½ÚµÄÊý¾Ýºó£¬¼ÆʱÆ÷¿ªÊ¼¼Æʱ¡£º¯Êýread¿ÉÄÜ»áÔÚ¶ÁÈ¡µ½VMIN¸ö×Ö½ÚµÄÊý¾Ýºó·µ»Ø£¬Ò²¿ÉÄÜÔÚ¼ÆʱÍê±Ïºó·µ»Ø£¬ÕâÖ÷Ҫȡ¾öÓÚÄĸöÌõ¼þÊ×ÏÈʵÏÖ¡£²»¹ýº¯ÊýÖÁÉÙ»á¶ÁÈ¡µ½Ò»¸ö×Ö½ÚµÄÊý¾Ý£¬ÒòΪ¼ÆʱÆ÷ÊÇÔÚ¶ÁÈ¡µ½µÚÒ»¸öÊý¾Ýʱ¿ªÊ¼¼ÆʱµÄ¡£

l Èç¹ûVTIMEºÍVMIN¶¼È¡0£¬¼´Ê¹¶ÁÈ¡²»µ½ÈκÎÊý¾Ý£¬º¯ÊýreadÒ²»áÁ¢¼´·µ»Ø¡£Í¬Ê±£¬·µ»ØÖµ0±íʾreadº¯Êý²»ÐèÒªµÈ´ýÎļþ½áÊø±êÖ¾¾Í·µ»ØÁË¡£

Õâ¾ÍÊÇÕâÁ½¸ö±äÁ¿¶Ôreadº¯ÊýµÄÓ°Ïì¡£ÎÒʹÓõĶÁ¿¨Æ÷ÿ´Î´«Ë͵ÄÊý¾ÝÊÇ13¸ö×Ö½Ú£¬Ò»¿ªÊ¼£¬ÎÒ°ÑËüÃÇÉèÖóÉ

options.c_cc[VTIME] = 150

options.c_cc[VMIN] = 0;

½á¹û£¬Ã¿´Î¶ÁÈ¡µÄÐÅÏ¢Ö»ÓÐ8¸ö×Ö½Ú£¬Ê£ÏµÄ5¸ö×Ö½ÚÒªµÈµ½ÏÂÒ»´Î´ò¿¨Ê±²ÅÄÜÊÕµ½¡£¾ÍÊÇÓÉÓÚÕâ¸öÔ­ÒòÔì³ÉµÄ¡£¸ù¾ÝÉÏÃæ¹æÔòµÄµÚÒ»Ìõ£¬ÎÒ°ÑVTIMEÈ¡0£¬VMIN=13£¬Ò²¾ÍÊÇÕýºÃµÈÓÚÒ»´ÎÐèÒª½ÓÊÕµÄ×Ö½ÚÊý¡£ÕâÑù¾ÍʵÏÖÁËÒ»´Î¶ÁÈ¡13¸ö×Ö½ÚÖµ¡£Í¬Ê±£¬µÃ³öÕâÑùµÄ½áÂÛ£¬Èç¹û¶Á¿¨Æ÷ËͳöµÄÊý¾ÝΪn¸ö×Ö½Ú£¬ÄÇô¾Í°ÑVMIN=n£¬ÕâÑùÒ»´Î¶ÁÈ¡µÄÐÅÏ¢ÕýºÃΪ¶Á¿¨Æ÷ËͳöµÄÐÅÏ¢£¬²¢ÇÒ¶ÁÈ¡µÄʱºò²»ÐèÒª½øÐÐÑ­»·¶ÁÈ¡¡£



4. ¶ÁÈ¡Êý¾Ý

ÓÐÁËÉÏÃæµÄº¯Êýºó£¬ÎÒÉèÖÃÁË´®¿ÚµÄ»ù±¾ÐÅÏ¢£¬¸ù¾ÝÎÒÃÇ×Ô¼ºµÄʵ¼ÊÇé¿ö£¬ÉèÖÃÁËÏàÓ¦µÄ²ÎÊý£¬¾Í¿ÉÒÔ¶ÁÈ¡Êý¾ÝÁË¡£

void getcardinfo(char *buff){

int fd;

int nread,count=0;

char tempbuff[13];

char *dev = "/dev/ttyS0"; //´®¿Ú1

fd = OpenDev(dev);

set_speed(fd,9600);

if (set_Parity(fd,8,1,'N') == FALSE) {

printf("Set Parity Error\n");

//return -1;

}

while (1) //Ñ­»·¶ÁÈ¡Êý¾Ý

{

count=0;

//sleep(5000);

while(1)

{

if((nread = read(fd, tempbuff, 13))>0)

{

//printf("\nLen %d\n",nread);

memcpy(&buff[count],tempbuff,nread);

count+=nread;

}

if(count==13)

{

buff[count+1] = '\0';

//printf( "\n%s", buff);

break;

}

}

//break;

}

//return buff;

close(fd);

pthread_exit(NULL);

//close(fd);

// exit (0);

}

ÕâÊÇÎÒÔ­À´µÄ³ÌÐò£¬Æäʵ°ÑVMINÉèÖÃÒԺ󣬿ÉÒԸijɣº

void getcardinfo(char *buff){

int fd;

int nread,count=0;

char tempbuff[13];

char *dev = "/dev/ttyS0"; //´®¿Ú1

fd = OpenDev(dev);

set_speed(fd,9600);

if (set_Parity(fd,8,1,'N') == FALSE) {

printf("Set Parity Error\n");

//return -1;

}

nread = read(fd, buff, 13)

close(fd);

}
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 2 ÌõÆÀÂÛ

  1. zhong110109 ÓÚ 2010-08-26 13:07:44·¢±í:

    ѧϰ£¡£¡

  2. ausername ÓÚ 2010-01-06 17:55:27·¢±í:

    ²»´í