I am using linux, and I need to write a program in C for creating pulses through the o/p of the serial port, which can be viewed by an oscilloscope. How to do it? Also how to configure a serial port through C?
Asked
Active
Viewed 658 times
2
-
this might be of use to you http://yyao.ca/projects/ParallelPortLinux/ – tesseract Feb 26 '14 at 05:22
-
Also you can check here http://ulisse.elettra.trieste.it/services/doc/serial/basics.html and http://stackoverflow.com/questions/6947413/how-to-open-read-and-write-from-serial-port-in-c – Jayesh Bhoi Feb 26 '14 at 05:26
-
Thanks , but is there a similar tutorial for serial port? – Bas Feb 26 '14 at 05:27
-
not off the top of my head, but the concepts for the serial port should be similar, but JKB's links are pretty good. – tesseract Feb 26 '14 at 05:32
-
What you need to do? If you need to generate a pulse then the first step is designing a (very simple indeed) electronic circuit with a microcontroller and programming it order to generate the required pulse. On the other hand, if you already have a 'pulse generator' and need to communicate with it using the serial port then may find many tutorials available. The third option, namely "generating the pulse using the PC serial port" is NOT an option. – Jayesh Bhoi Feb 26 '14 at 05:46
-
@JKB the actual reason why i need to generate pulse using the serial port of the pc is to check the real time capabilities of my newly installed rt linux. – Bas Feb 26 '14 at 05:53
1 Answers
1
Finally found a way to get it done
#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);
}
}
}

Bas
- 469
- 8
- 22