cant able to read serial Port

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



hello All,
  i am very new in Linux , and i made a small program in C that jst sends and tries to receive character from serial port.
  it is working fine for sending, but not receiving character from other end .. on other end Windows Xp running.  Read() function retuns error
  no = 11, i m putting my small code,
        plz help  me ...
thank in advance
 
CODE:
 
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

int readport(int fd, char *result)
{
 int iIn = read(fd, result, 254);
 result[iIn-1] = 0x00;
 if (iIn < 0) {
  
   printf("SERIAL EAGAIN ERROR\n");
   printf("\nerro NO =%d\n",errno);
   printf("SERIAL read error %d-----> %s\n", errno, strerror(errno));
   return 0;
        }
                    
 return 1;
}
 

main()
{
int fd;
int nread;
char c;
char bufptr[5];
char buffer[5];
buffer[0]= 'S';
buffer[1]= 'S';
buffer[2]= 'S';
buffer[3]= 'S';
buffer[4]= 'S';
int i;
char sResult[254];
struct termios options;
// buffer = "Sorry";
fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
 if (fd == -1)
 {
 printf("\n\n\t\t Sorry Yaar Port Nahi Khul Raha \n\n\n");
 return 1;
 }
 else
 {
 printf("\n\n\t\t Port Khul gaya.... \n\n\n");
 fcntl(fd, F_SETFL, 0);
 }
tcgetattr(fd, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD);
 options.c_cflag &= ~PARENB;
 options.c_cflag &= ~CSTOPB;
 options.c_cflag &= ~CSIZE;
 options.c_cflag |= CS8;
tcsetattr(fd, TCSANOW, &options);
write(fd,"devesh",6);
usleep(500000);
/*
fcntl(fd, F_SETFL, FNDELAY);
nread = read(fd,buffer,5);
//if (nread < 0)
//write(2,"ERROR in read\n",20);
printf("\nnread = %d\n",nread);
printf("tong = %s\n",buffer);
 

close(fd);
*/
 fcntl(fd, F_SETFL, FNDELAY); // don't block serial read
 if (!readport(fd,sResult)) {
  printf("read failed\n");
  close(fd);
  return 1;
 }
 printf("readport=%s\n", sResult);
 close(fd);
 return 0;
}
 

        [DEV'H]

   Devesh Sharma

"Have A Nice Time"

 

 



Unlimited freedom, unlimited storage. Get it now
-- 
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux