当前位置: 技术问答>linux和unix
关于串口的应用程序问题
来源: 互联网 发布时间:2016-11-16
本文导语: 鄙人刚接触嵌入式linux 想编写个串口应用程序 自发自收 但是总是收不到 下面是程序,希望高手指点下 #include #include #include #include #include #include #include #include #include #include #include #include #include...
鄙人刚接触嵌入式linux 想编写个串口应用程序 自发自收 但是总是收不到 下面是程序,希望高手指点下
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_TTYS "/dev/ttyS1"
#define DATAS_CONTENT "HELLO"
#define MY_BAUD_RATE B115200
#define RECEIVE_BUF_WAIT_1S 1
int main (void)
{
int fd;
printf("n RS485 TRANSFERS DATAS nn");
fd=open(DEVICE_TTYS,O_RDWR);
if (fd == -1){
printf("error");
}
else
{
struct termios options;
bzero (&options,sizeof(options));
cfsetispeed (&options,MY_BAUD_RATE);
cfsetospeed (&options,MY_BAUD_RATE);
options.c_cflag |= (CS8 | CLOCAL | CREAD);
options.c_iflag = IGNPAR;
tcflush(fd,TCIFLUSH);
tcsetattr(fd,TCSANOW,&options);
func_485_transfer(fd);
if(close(fd)!=0) printf ("error");
}
return 0;
}
int func_485_transfer (int fd)
{
ssize_t ret;
char rcv_buf [1024];
int i;
char *send_buf = "hello";
bzero(rcv_buf,sizeof(rcv_buf));
ret=write(fd,send_buf,strlen(send_buf));
int retval;
fd_set rfds;
struct timeval tv;
tv.tv_sec = RECEIVE_BUF_WAIT_1S;
tv.tv_usec = 0;
while (1)
{
FD_ZERO(&rfds);
FD_SET(fd,&rfds);
retval = select (fd+1,&rfds,NULL,NULL,&tv);
ret=read (fd,rcv_buf,20);
printf ("%s!!n",rcv_buf);
break;
}
return 0;
}
只显示 !! 老手们 这哪里有问题啊
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_TTYS "/dev/ttyS1"
#define DATAS_CONTENT "HELLO"
#define MY_BAUD_RATE B115200
#define RECEIVE_BUF_WAIT_1S 1
int main (void)
{
int fd;
printf("n RS485 TRANSFERS DATAS nn");
fd=open(DEVICE_TTYS,O_RDWR);
if (fd == -1){
printf("error");
}
else
{
struct termios options;
bzero (&options,sizeof(options));
cfsetispeed (&options,MY_BAUD_RATE);
cfsetospeed (&options,MY_BAUD_RATE);
options.c_cflag |= (CS8 | CLOCAL | CREAD);
options.c_iflag = IGNPAR;
tcflush(fd,TCIFLUSH);
tcsetattr(fd,TCSANOW,&options);
func_485_transfer(fd);
if(close(fd)!=0) printf ("error");
}
return 0;
}
int func_485_transfer (int fd)
{
ssize_t ret;
char rcv_buf [1024];
int i;
char *send_buf = "hello";
bzero(rcv_buf,sizeof(rcv_buf));
ret=write(fd,send_buf,strlen(send_buf));
int retval;
fd_set rfds;
struct timeval tv;
tv.tv_sec = RECEIVE_BUF_WAIT_1S;
tv.tv_usec = 0;
while (1)
{
FD_ZERO(&rfds);
FD_SET(fd,&rfds);
retval = select (fd+1,&rfds,NULL,NULL,&tv);
ret=read (fd,rcv_buf,20);
printf ("%s!!n",rcv_buf);
break;
}
return 0;
}
只显示 !! 老手们 这哪里有问题啊
|
建议你还是先知道串口的那几根针的定义吧。
从串口发出去的数据想着都不可能又回到串口(好吧,这是我猜的)
从串口发出去的数据想着都不可能又回到串口(好吧,这是我猜的)
|
其实有的UART chip可以内部有loopback模式,这样驱动开启后可以进行自回环。