Hello,

This is similar to the thread I did a while back, except this time I need to do it under linux. I'm using the latest version of Ubuntu. Can some some one show me how to send information (an array of char) to the rs232 port (synchronous communication) . It's going to be one way communication (PC to device). An example would be nice. It's been a while since I did anything in C, I've been mostly using Java.

Thanks

Thanks. Well I found some code online that semi-works, but there's a number of things I need to know. (BTW I'm still a novice at C).

1. What does " O_RDWR | O_NOCTTY | O_NDELAY" mean and what does the " | " in between mean?
2. I see where the baud rate is set, but where is the parity and end bit information set?
3. I need to send hex values, how do I go about it?
4. Where in the code is the serial port initialized? The device I'm communicating to will receive garbage data if it's turned on before the port is initialized.

#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 main()
{
      int fd; /* File descriptor for the port */


      fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
      if (fd == -1)
      {
       /*
	* Could not open the port.
	*/

	perror("open_port: Unable to open /dev/ttyS0 - ");
      }
      else
	fcntl(fd, F_SETFL, 0);



    struct termios options;

    /*
     * Get the current options for the port...
     */

    tcgetattr(fd, &options);

    /*
     * Set the baud rates to 9600...
     */

    cfsetispeed(&options, B9600);
    cfsetospeed(&options, B9600);
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    options.c_oflag &= ~OPOST; //raw output

    /*
     * Enable the receiver and set local mode...
     */

    options.c_cflag |= (CLOCAL | CREAD);

    /*
     * Set the new options for the port...
     */

    tcsetattr(fd, TCSANOW, &options);

    char* temp = (char){0xf,0xf,0xf,0xf,'\0'};
    int n = write(fd, "FFFF", 4);
    if (n < 0)
      fputs("write() of 1 byte failed!\n", stderr);


    close(fd);
    return 0;
}

Well I just figured out how to send with hex values, so #3 is answered.

I got it to work, but now I'd like know what each line of code is really doing.

BTW does anyone know an accurate way to wait? usleep(int ) seems to be a bit inaccurate.

the best way to figure out RS232 is, as you have found out, to hack through it yourself.

those constants you asked about are #defined in the header file.

to get an accurate delay, you can run a huge for loop and measure the realtime elapsed. then derive a value based on the results that will allow you to delay an arbitrary amount.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.