c - 如何获取串口RS232输出的脉冲?

标签 c linux

我用的是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/

相关文章:

c - 使用 poll 在 C 中进行套接字连接超时

c - 实现一个好的 "itoa()"函数的正确方法是什么?

c - 套接字事件如何传播/转换为 epoll?

linux - 计算文本文件中的一致性和相关性

linux - 如何使用现有环境变量通过 echo 命令写入 $BASH_ENV?

Linux 脚本以秒为单位获取上次修改文件夹的时间

c - 访问结构体非数组元素

c - "Illegal hardware instruction"来自非常简单的代码

c++ - 在 Windows 中进行 C 或 C++ 编程时如何操作 GUID?

linux - AWS 简单广告 : SSH based key Authentication for Linux instances