注明:我是个新手,程序不是我自己写的,我只是稍加修改。
首先把我的串口程序贴上来,然后再说明一下问题:
接收程序:
/********************head file define***********************/
#include
#include
#include
#include
#include
#include
#include
#include
#include "math.h"
#define max_buffer_size 100 /*define the max buffer*/
/*********************************************************/
int fd,s;
int open_serial(int k)
{
if(k==0) /*serial port*/
{
fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY);
perror("open /dev/ttyS0");
}
else
{
fd = open("/dev/ttyS1",O_RDWR|O_NOCTTY);
perror("open /dev/ttyS1");
}
printf("%d\n",fd);
if(fd == -1)
return -1;
}
/********************************************************************/
int main(int argc , char* argv[])
{
char hd[max_buffer_size]; /*define the receive buffer*/
int flag_close,retv,i,ncount = 0;
struct termios opt;
int realdata = 0;
/*******************************************************************/
open_serial(0); /*open the serial 1*/
/*******************************************************************/
tcgetattr(fd,&opt);
cfmakeraw(&opt);
/*****************************************************************/
cfsetispeed(&opt,B9600); /*bound rate is set 9600*/
cfsetospeed(&opt,B9600);
/*******************************************************************/
tcsetattr(fd,TCSANOW,&opt);
printf("ready for receiving data...\n");
retv = read (fd,hd,50); /* receive data is stored int the fd,
retv is the receive bytes numbers */
if(retv == -1)
perror("error!!!"); /* read status biaozhi judge */
/******************************************************************/
printf("The data received is:\n"); /* output the received data*/
for(i = 0;i < 50;i++)
{
printf("%c",hd[i]);
}
printf("\n");
flag_close = close(fd);
if(flag_close == -1)
printf("Close the Device failure\n");
return 0;
}
下面是发送程序
#include
#include
#include
#include
#include
#include
#include
#include
/*******************************************/
int fd;
int flag_close;
int open_serial(int k)
{
if(k==0)
{
fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY);
perror("open /dev/ttyS0");
}
else
{
fd = open("/dev/ttyS1",O_RDWR|O_NOCTTY);
perror("open /dev/ttyS1");
}
if(fd == -1)
return -1;
}
/********************************************************************/
int main(int argc, char *argv[ ] )
{
char sbuf[]={"Serial_Port test!\n"};
int sfd,retv,i;
struct termios opt;
int length = sizeof(sbuf);
/*******************************************************************/
open_serial(0);
/*******************************************************************/
printf("ready for sending data...\n");
tcgetattr(fd,&opt);
cfmakeraw(&opt);
/*****************************************************************/
cfsetispeed(&opt,B9600);
cfsetospeed(&opt,B9600);
/*******************************************************************/
tcsetattr(fd,TCSANOW,&opt);
retv = write(fd,sbuf,length);
if(retv == -1)
{
perror("write");
}
printf("the char numbers sent is %d\n",retv);
flag_close = close(fd);
if(flag_close == -1)
printf("Close the Device failure\n");
return 0;
}
运行了程序,发送是正常的,而接收是乱码,不知是怎么回事。我是在一个用户下,同一个终端中观看的,不知这种方式对不对,还请高手指导。
gogo11 于 2011-06-23 11:25:05发表:
能修改的都是高手,因为俺就不会修改程序。在俺以上的都是高手~~~~~~
lgcHR 于 2011-06-23 10:01:07发表:
不知道是怎么回事帖子被编辑了一下,字体竟然称斜体了,怪哉!