C
collinm
hi
we use linux and c language
under bash i do
echo -e \\000\\000\\000\\000\000\\001Z00\\002AA LINUX \\004 >/dev/ttyS2
that send command to a led display (alpha sign communication)
i need to do convert this code to a c programm under linux
i tried this code:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <stdio.h>
/* File descriptors for the ports */
int fd1, fd2;
char *buff,*buffer,*bufptr;
/* Opening port one for read and write */
int open_port1(void)
{
fd1 = open("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd1 == -1)
{
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
{
fcntl(fd1, F_SETFL, 0);
printf(" Port 1 has been sucessfully opened and %d is the file
descriptor\n",fd1);
}
return (fd1);
}
int open_port2(void)
{
fd2 = open("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd2 == -1)
{
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
{
fcntl(fd2, F_SETFL,FNDELAY);
printf(" Port 2 has been sucessfully opened and %d is the file
descriptor\n",fd2);
}
return (fd2);
}
int main()
{
int wr,rd;
open_port1();
open_port2();
char msg[]="\\000\\000\\000\\000\\000\\001Z00\\002AA LINUX \\004";
wr=write(fd1,msg,100);
printf(" Bytes sent are %d \n",wr);
if (wr < 0)
fputs("write() of 4 bytes failed!\n", stderr);
else
printf(" Stuff has been written into port 1\n");
sleep(1);
fcntl(fd2, F_SETFL,FNDELAY);
rd=read(fd2,buff,100);
printf(" Bytes recieved are %d \n",rd);
printf("%s",buff);
close(fd1);
close(fd2);
return 1;
}
but nothing happen on the led display
any idea?
we use linux and c language
under bash i do
echo -e \\000\\000\\000\\000\000\\001Z00\\002AA LINUX \\004 >/dev/ttyS2
that send command to a led display (alpha sign communication)
i need to do convert this code to a c programm under linux
i tried this code:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <stdio.h>
/* File descriptors for the ports */
int fd1, fd2;
char *buff,*buffer,*bufptr;
/* Opening port one for read and write */
int open_port1(void)
{
fd1 = open("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd1 == -1)
{
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
{
fcntl(fd1, F_SETFL, 0);
printf(" Port 1 has been sucessfully opened and %d is the file
descriptor\n",fd1);
}
return (fd1);
}
int open_port2(void)
{
fd2 = open("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd2 == -1)
{
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
{
fcntl(fd2, F_SETFL,FNDELAY);
printf(" Port 2 has been sucessfully opened and %d is the file
descriptor\n",fd2);
}
return (fd2);
}
int main()
{
int wr,rd;
open_port1();
open_port2();
char msg[]="\\000\\000\\000\\000\\000\\001Z00\\002AA LINUX \\004";
wr=write(fd1,msg,100);
printf(" Bytes sent are %d \n",wr);
if (wr < 0)
fputs("write() of 4 bytes failed!\n", stderr);
else
printf(" Stuff has been written into port 1\n");
sleep(1);
fcntl(fd2, F_SETFL,FNDELAY);
rd=read(fd2,buff,100);
printf(" Bytes recieved are %d \n",rd);
printf("%s",buff);
close(fd1);
close(fd2);
return 1;
}
but nothing happen on the led display
any idea?