Code:
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h> /*atoi, system, getenv */
#include <unistd.h> /*sleep, close */
#include <dirent.h> /*opendir, readdir, closedir */
#include <string.h>
#include <fcntl.h> /*open */
#include <sys/stat.h>
#include <sys/errno.h>
char *ledisplay;
/* Opening port one for read and write */
int writeopen(char msg[])
{
int fd1;
int wr;
fd1 = open(ledisplay, O_RDWR | O_NOCTTY | O_NDELAY );
if (fd1 == -1)
{
fprintf(stderr, " %s open_port: Unable to open %s\n", strerror(errno), ledisplay);
}
else
{
fcntl(fd1, F_SETFL, 0);
printf(" Port 1 has been sucessfully opened and %d is the file descriptor\n",fd1);
printf("sizeof %d\n", sizeof(msg));
wr=write(fd1, msg, sizeof(msg));
printf(" Bytes sent are %d \n",wr);
if (wr < 0)
fputs("write() of n bytes failed!\n", stderr);
else
printf(" Stuff has been written into port 1\n");
close(fd1);
}
return (fd1);
}
int readopen()
{
int fd1;
int rd;
char *buff=NULL;
fd1 = open(ledisplay, O_RDONLY | O_NOCTTY | O_NDELAY );
if (fd1 == -1)
{
fprintf(stderr, "%s open_port: Unable to open %s\n", strerror(errno), ledisplay);
}
else
{
fcntl(fd1, F_SETFL, 0);
printf(" Port 1 has been sucessfully opened and %d is the file descriptor\n",fd1);
rd=read(fd1, buff, 100);
printf(" Bytes recieved are %d \n",rd);
printf("%s\n",buff);
close(fd1);
}
return (fd1);
}
int main()
{
int result;
//echo -e \\000\\000\\000\\000\000\\001Z00\\002AA TRIPLEX \\004 >/dev/ttyS2
char msg[] = {'\0', '\0', '\0', '\0', '\0', '\1', 'Z', '0', '0', '\2', 'A', 'A', ' ', 'T', 'R', 'I', 'P', 'L', 'E', 'X', ' ', '\4' };
if((result=(readRtuConfig()))!=-1)
{
printf("port série utilisé: %s\n", ledisplay);
printf("msg = %s\n",msg);
if((result=(writeopen(msg)))!=-1)
{
printf("ok\n");
}
}
return 1;
}
when i run this program i get