LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to send byte from serial port (https://www.linuxquestions.org/questions/linux-software-2/how-to-send-byte-from-serial-port-631237/)

neutron001 03-28-2008 08:26 AM

How to send byte from serial port
 
Hello all.. I am working on an embedded project and I would like to send single bytes over /dev/ttyS0. The problem is, these terminal programs are set up to transmit ascii, not non-ascii data - at least as far as I know.
I tried using libserial and c++, but having issues sending raw data as well.
I don't care what method is used, I just need to send a few bytes.. (nrz format) 0xfd, 0xc2 to debug my code on a pic microcontroller.
Does anyone have any examples of code or know if theres a method to do this in linux? Maybe minicom, kermit or straight from the command line?
Thanks
Derek

theNbomr 03-28-2008 09:10 AM

I think C-Kermit provides the 'transmit' command to send a file of arbitrary byte content. There is always the route of a good old hand-crafted purpose-built tool based on examples from Serial Programming Guide for POSIX Operating Systems. Using tools like setserial and stty may allow you to configure a serial port to the point where you can use shell commands to send arbitrary bytes to the serial device, without any interference from the device driver.
--- rod.

neutron001 03-28-2008 10:50 AM

Excellent. I like the hand crafted approach, but am still having issues working from the POSIX link you sent me. When write is called, i'm getting an error from the compiler ->
"invalid conversion from ;int' to 'const void*'.

please see below code:

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 */


void 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);


    int n = write(fd, 0xfd, 8);
    if (n < 0)
      fputs("write() of 8 bytes failed!\n", stderr);


    close(fd);

}

Obviously the write function doesn't want an integer.. how do I make this work?
Thanks!

JudyL 03-28-2008 02:30 PM

the prototype is
Code:

int write(int fd, const void *buffer, unsigned int count);
You need to match it

Code:

write(fd, 0xfd, 8);
1st param is ok,
2nd param should be a pointer to your data, even if it's only one byte long
3rd param is the length in bytes

try something like:
Code:

unsigned char data;
data = 0xfd;
write (fd, &data, 1);


neutron001 03-28-2008 03:03 PM

Phew, that worked like a charm. Thank you so much!

neutron001 03-31-2008 03:08 PM

I hate to restart this thread, but I'm still having an issue apparently..
I thought this worked ok, but it seems that when I execute the code up above my serial port locks up. I changed to code to just send one character "f".
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 */


void 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);


    int n = write(fd, "f", 1);
    if (n < 0)
      fputs("write() of 1 byte failed!\n", stderr);


    close(fd);

}

When I run minicom everything works fine.. I send an f and my little microchip blinks an LED.
When I run the above code nothing happens. I have a scope connected to my serial TX line and I don't see anything. So.. I tried this after a reboot:
1. command line -> "echo blah > /dev/ttyS0"
I see nice pulses on my scope.

2. minicom - I can send anything and see nice pulses.

3. Run my program - I see nothing. I close my program and then try to do the "echo..." from the command line and nothing happens. The echo just hangs there and I have to do a ctrl-c to kill it.

It seems that I lose the ability to send anything after I run my program. Except minicom - it works after I run my program.
Any ideas on this one? lockfiles? permissions?

I noticed also that when I run my program, after I do a write, it hangs for about 30 seconds and then returns to the command line.

D

JudyL 03-31-2008 03:18 PM

You've probably messed up the port settings. All those functions where you set an option return the old option. First, have it working with your external tools. Next, run your program and step through it with a debugger and see what all those returned values are. Those returned values are the ones that were last set by your external tools. You should be setting then in your program to match those working ones.

theNbomr 03-31-2008 05:39 PM

For some more good examples, see http://tldp.org/HOWTO/Serial-Program...OWTO/x115.html. These show how to restore the serial port when your program exits.
--- rod.

neutron001 03-31-2008 05:44 PM

Thank you Judy for helping me out so far..
I think I'm in over my head here in setting these bits. One thing I noticed was that I wasn't doing this:
tcgetattr(fd, &options) before doing all the bitmasking.
It seems that 0xcbd is the magical combo for the cflags (I printed out that value right after the tcgetattr), but I just can't seem to configure anything to come close to it. I am close to throwing in the towel though.

neutron001 03-31-2008 05:48 PM

Rod, that link is where I came up with my non-working code. I think I'm just not "getting it".
All of the examples that I've tried from the internet don't seem to work for me... and I've tried many.


All times are GMT -5. The time now is 07:14 PM.