Hello Am335x,
I am new to embedded Linux. I need to send multiple commands and process their response. I have written a test application in polling which is bit time taking and unreliable .
I need an interrupt based serial port. My code goes like this:-
#include <stdio.h>
#include <unistd.h> /* UNIX standard function definitions */
#include <sys/types.h> /* data types */
#include <sys/stat.h> /* data returned by the stat() function */
#include <fcntl.h> /* File control definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <errno.h> /* Error number definitions */
#include <time.h>
#include <sys/ioctl.h>
#include <string.h>
char battery_status[15] =
{ some command };
char response[25];
int i;
int len = 0;
int
main ()
{
msg ();
static int fd = 0;
struct termios options;
//==================================================================
// Port Opening and Checking (opens the correct port)
//==================================================================
fd = open ("/dev/ttyO3", O_RDWR | O_NOCTTY | O_NDELAY);
//================================================================
// non blocking
//================================================================
fcntl (fd, F_SETFL, O_NONBLOCK);
//==================================================================
// Error Handling
//==================================================================
if (fd < 0)
{
printf ("Serial open error %d %s\n", errno, strerror (errno));
return errno;
}
//==================================================================
// Get the current options for the port...
//==================================================================
tcgetattr (fd, &options);
//==================================================================
// Set the baud rates to 115200...
//==================================================================
cfsetispeed (&options, B115200);
cfsetospeed (&options, B115200);
//=================================================================
// Enable the receiver and set local mode..
//==================================================================
options.c_cflag |= (CLOCAL | CREAD);
//==================================================================
// Set the new options for the port...
//==================================================================
tcsetattr (fd, TCSANOW, &options);
//===================================================================================
// transmit and recieve starts here
//===================================================================================
while (1)
{
tcflush (fd, TCIFLUSH); // Flush Uart Driver
write (fd, battery_status, strlen ((char *) battery_status)); // serial port write
sleep (3);
printf ("\n\n\n Awaiting Response ...\n");
len = read (fd, response, 20);
if (len < 0)
{
// retry
}
else
{
//do some thing
}
} // while 1
close (fd);
return 0;
}
How I can do the same using interrupt.
Thanking you in anticipation.
Regards