Hello to all,
I am doing a serial communication program in which i want to send '80' (in hex ) to DSP and then DSP will reply '1B' . But i am not able to set proper communication.
Anybody please help me in my software
My Program is given below

************************************************** ************************************************** ****

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/signal.h>
#include <sys/types.h>

int fd;
char iIn[1];

int open_port (void)
{

// int fd; /* File descriptor for the port */
int ser_cmd, ser_stat;
ser_cmd = TIOCM_RTS;

fd = open("/dev/ttyS0", O_RDONLY); // Open the serial port.

if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyS0 -%s\n ");
return 1;
} else{
printf("Port 0 opened\n");
fcntl(fd, F_SETFL, 0);
}

// Read the RTS pin status.....

ioctl(fd, TIOCMGET, &ser_stat);
if (ser_stat & TIOCM_RTS)
{
printf("RTS pin is set.\n");
}
else
{
printf("RTS pin is reset.\n");
ioctl(fd, TIOCMBIC, &ser_cmd); // set the RTS pin.
if (ser_stat & TIOCM_RTS)
printf("RTS pin is set.\n");
else
printf("RTS pin is reset.\n");

}
return (fd);
}




int writeport(int fd, char *chars)

{
int len =strlen(chars);

int n = write(fd, chars,strlen(chars));
if (n < 0)
{
fputs("write failed!\n", stderr);

return 0;
}
fcntl(fd, F_SETFL,0);//changed
return 1;
}




int readport(int fd, char *result)
{
int iIn = read(fd, result, 1);
printf("Value Received: %x\n",*result);


return 1;
}


int initport(int fd){

struct termios options;
printf("Setting Parameters...\n");
tcgetattr(fd, &options);
cfsetispeed(&options, B9600); /* Set the baud rates to 9600 */
cfsetospeed(&options, B9600);

/* Enable the receiver and set local mode */
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB; /* Mask the character size to 8 bits, no parity */
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8; /* Select 8 data bits */
options.c_cflag &= ~CRTSCTS; /* Disable hardware flow control */

/* Enable data to be processed as raw input */
options.c_lflag &= ~(ICANON | ECHO | ISIG);

options.c_cc[VMIN]=0;//it will wait for one byte at a time.
options.c_cc[VTIME]=10;// it will wait for 0.1s at a time.
/* Set the new options for the port */
tcsetattr(fd, TCSANOW, &options);
printf("Parameters SET!!!...\n");
return 1;
}






int main()

{

int mainfd=0;

//struct termios options;


mainfd = open_port();
close(mainfd);

fd = open( "/dev/ttyS0" ,O_RDWR |O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open_port: Unable to open /dev/ttyS0 - ");
return 1;
} else {
fcntl(fd, F_SETFL,0);
}
/* Clear the line */
tcflush(fd,TCIFLUSH);

initport(fd);


char sCmd[1];
sCmd[0] =0200;//0010;
//sCmd[1] =0000;//0000;
//writeport(fd,sCmd);
if (!writeport(fd, sCmd)) {
printf("write failed\n");
close(fd);
return 1;
}


printf("written: %d%d\n", sCmd[0]);
close(fd);

usleep(30000);
fd = 0;
fd = open( "/dev/ttyS0" ,O_RDONLY |O_NOCTTY | O_NDELAY);
char sResult[1];
fcntl(fd, F_SETFL, FNDELAY); // don't block serial read

if (!readport(fd,sResult)) {
printf("read failed\n");
close(fd);
return 1;
}
else

//printf("readport=%d%d\n",sResult[0],sResult[1]);
printf("readport=%d%d\n",iIn[0],iIn[1]);
close(fd);
return 0;
}

***********************************************************************
*****************************************************************
I think i am making some mistakes in reading or in writing or in both functions...
thanking you...


Regards
Sneha

Where does it break?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.