我用的是linux,需要用C写一个程序,通过串口的o/p产生脉冲,可以用示波器查看。怎么做?还有如何通过C配置串口?
最佳答案
终于找到了完成它的方法
#include<time.h>
#include<unistd.h>
#include<stdio.h>
#include<errno.h>
#include<termios.h>
#include<fcntl.h>
#include<sys/ioctl.h>
int main()
{
struct t`enter code here`imespec ts;
ts.tv_nsec =999999999;
ts.tv_sec=0;
int fd,ctr,j1,j2,j3,j4;
fd =open("/dev/ttyS0", O_RDWR | O_NOCTTY);
j1=ioctl(fd, TIOCMGET, &ctr);
if(j1<0)
perror("\nfailed1 not able to get status\n");
else
{
ctr=ctr|TIOCM_DTR;
j2=ioctl(fd,TIOCMSET,&ctr);
if(j2<0)
perror("\nfailed2 not able to set \n");
else
nanosleep(&ts,0);
j3=ioctl(fd,TIOCMGET,&ctr);
if(j3<0)
perror("failed 3 not able to get status2\n");
else
{
ctr&=~TIOCM_DTR;
j4=ioctl(fd,TIOCMSET,&ctr);
if(j4<0)
perror("faliure 4 not able to set 2");
else
nanosleep(&ts,0);
}
}
}
关于c - 如何获取串口RS232输出的脉冲?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22032131/