Moderator Control Panel ]

Port COM1 and COM2 with langage C

Forum sur le Micro2440 à base ARM9 s3c2440 256 MiB nand

Port COM1 and COM2 with langage C

Postby andromeda » 08 Mar 2010 0:56

Code: Select all
#include <stdio.h>
#include <termios.h>
#include <fcntl.h>
#include <string.h>

int main()
{
  int fd;
  struct termios options;
  int n;
  char buff[255];

  /* Open Port */
  fd = open("/dev/ttySAC1", O_RDWR | O_NOCTTY | O_NDELAY);  /* <--- YOUR
PORT */

  if(fd == -1) {
    printf("ERROR Open Serial Port!");
  }
 
  /* Serial Configuration */
  tcgetattr(fd, &options);   // Get Current Config
  cfsetispeed(&options, B9600); // Set Baud Rate
  cfsetospeed(&options, B9600);
  options.c_cflag = (options.c_cflag & ~CSIZE) | CS8;
  options.c_iflag =  IGNBRK;
  options.c_lflag = 0;
  options.c_oflag = 0;
  options.c_cflag |= CLOCAL | CREAD;
  options.c_cc[VMIN] = 1;
  options.c_cc[VTIME] = 5;
  options.c_iflag &= ~(IXON|IXOFF|IXANY);
  options.c_cflag &= ~(PARENB | PARODD);
  /* Save The Configure */
  tcsetattr(fd, TCSANOW, &options);
  /* Flush the input (read) buffer */
  tcflush(fd,TCIOFLUSH);


   write(fd,"YOUR COMMAND STRING HERE",24);

  do{
    n = read(fd,buff,255);   // Read Data From Serial Port
    buff[n] = 0;
    if(n>0)
    {
      printf("%s",buff); // Print Out
    }
  }while(strncmp(buff,"bye",3)); // If user say bye then Exit

  close(fd); // Close Port
  return 0; // End Program
}
Moderator - Forum
andromeda
 
Posts: 631
Joined: 29 Mar 2004 23:07

Return to Micro2440

Who is online

Users browsing this forum: No registered users and 1 guest

cron