Jump to content

Communicating with serial In Linux

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
azip_ahmadz

azip_ahmadz

    Newbie

  • Members
  • Pip
  • 2 posts
hi all,,

anybody can help me,,

I have a problem with my serial program.
My serial program connect to one of hardware,but the program can't send data.

I think the problem is structure of termios is'nt correct.
I want to connect to the that hardware with :

1. BAUD = 19200
2. DATABITS = 8
3. PARITY = EVEN
4. STOPBITS = 1
AND
5. FLOW CONTROL = NONE


This my procedure :
int openSerial(char addr[])
{
  int fd,res,ii,i;
  char devicename[30];
  char data[10];
  int ret=-1;
  struct termios newio; 

  data[0]=4;
  data[1]=addr[0];
  data[2]=81;
  data[3]=10;
  data[4]=0;

  strcpy(devicename,"/dev/ttyS0");
  fd = open(devicename, O_RDWR | O_NOCTTY | O_NONBLOCK);
  if (fd < 0)
  {
   perror(devicename);
  }
  else {
   if (tcgetattr(fd, &newio)) printf("Error Get Attr\n");
   memset(&newio, 0, sizeof(struct termios));
   cfmakeraw(&newio);
   cfsetspeed(&newio, (speed_t)B19200);
   newio.c_cflag |= CREAD | CLOCAL
   newio.c_cflag |= (newio.c_cflag & ~CSIZE) | CS8;//databits
   newio.c_cflag |= 0;//stopbits=2

   //parity=even
   newio.c_cflag &= ~(PARENB | PARODD);
   newio.c_cflag |= PARENB;

   newio.c_iflag =0;
   newio.c_oflag =0;   
   newio.c_cc[VMIN] =1;
   newio.c_cc[VTIME] =0;
   //tcflush(newio,TCIFLUSH);

   if (tcsetattr(fd, TCSANOW, &newio)) printf("Set Error\n");
   if (tcgetattr(fd, &newio)) printf("Error Get Attr\n");
 
   printf("Sending data\n");
   write(fd,data,strlen(data));
   close(fd);
  }
  return ret;
}

Thanx all...
I wait the answer now..hehe

Edited by WingedPanther, 10 April 2008 - 07:59 AM.
add code tags


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It's not clear to me what all the code is doing, perhaps a few comments would help (or it could be my lack of device coding). BTW, please don't forget to include code tags and error messages to help us out :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog