After installing null modem, then we are ready to send character data through serial port. I have an example of sending data through the null modem that we have prepared before. Lets have a look at the source code :
#include "termios.h" /*this file contains declaration of required functions and types*/
#include "stdio.h" /*you know what this is*/
#include "fcntl.h" /*this file constains declaration for O_RDWR and O_NOCTTY)*/
#include "time.h" /*this file contains time's operatives*/
int main(void)
{
int i, j, s;
char t[9];
time_t ttrs;
struct tm *ti;
int fd; /*variable of type integer used as descriptor to a serial port*/
char c;
struct termios termiosA; /*declaring a variable of type termios*/
/* Turn on the null modem */
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY); /* I have done : sudo chmod 777 /dev/ttyS0 */
/* We don't care about incoming bytes with parity on data input */
termiosA.c_iflag = IGNPAR;
/*
Raw output.
*/
termiosA.c_oflag = CR0; /* output modes */
/*
we are going to set a connection with serial port to the receiver (this computer is a sender)
1. Baudrate is 4800 BPS (B9600)
2. Enable RTS/CTS (hardware) flow control (CRTSCTS)
3. Ignore modem control lines (CLOCAL)
4. Character size mask is 8 (CS8)
*/
termiosA.c_cflag = B4800 | CRTSCTS | CLOCAL | CS8; /* control flags
*/
/* flush the modem line */
tcflush(fd, TCIFLUSH);
/* initiate the modem */
tcsetattr(fd,TCSANOW,&termiosA);
/* Now its time to send something. */
i = 0;
for (;;)
{
time(&ttrs);
ti=localtime(&ttrs);
if (s != ti->tm_sec)
{
printf("Sending time data : %s", t);
sprintf(t, "%d:%d:%d\n", ti->tm_hour, ti->tm_min, ti->tm_sec);
s = ti->tm_sec;
for (j = 0; j < 9;j++)
{
write(fd, t[j], 1);
}
}
}
return 0;
}

No comments:
Post a Comment