LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 03-28-2008, 08:26 AM   #1
neutron001
LQ Newbie
 
Registered: Mar 2008
Posts: 19

Rep: Reputation: 0
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
 
Old 03-28-2008, 09:10 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
Old 03-28-2008, 10:50 AM   #3
neutron001
LQ Newbie
 
Registered: Mar 2008
Posts: 19

Original Poster
Rep: Reputation: 0
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!
 
Old 03-28-2008, 02:30 PM   #4
JudyL
LQ Newbie
 
Registered: Aug 2007
Location: Florida, USA
Distribution: Ubuntu
Posts: 29

Rep: Reputation: 15
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);
 
Old 03-28-2008, 03:03 PM   #5
neutron001
LQ Newbie
 
Registered: Mar 2008
Posts: 19

Original Poster
Rep: Reputation: 0
Phew, that worked like a charm. Thank you so much!
 
Old 03-31-2008, 03:08 PM   #6
neutron001
LQ Newbie
 
Registered: Mar 2008
Posts: 19

Original Poster
Rep: Reputation: 0
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
 
Old 03-31-2008, 03:18 PM   #7
JudyL
LQ Newbie
 
Registered: Aug 2007
Location: Florida, USA
Distribution: Ubuntu
Posts: 29

Rep: Reputation: 15
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.
 
Old 03-31-2008, 05:39 PM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
Old 03-31-2008, 05:44 PM   #9
neutron001
LQ Newbie
 
Registered: Mar 2008
Posts: 19

Original Poster
Rep: Reputation: 0
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.
 
Old 03-31-2008, 05:48 PM   #10
neutron001
LQ Newbie
 
Registered: Mar 2008
Posts: 19

Original Poster
Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to copy an entire disk byte-for-byte Pawprint Linux - Software 6 06-16-2011 11:01 AM
linux serial port to router console port connection? frankie_fix Linux - General 3 02-26-2007 09:32 PM
Parallel Port & Serial Port device identification helpmeforlinux Linux - Hardware 3 01-02-2007 01:15 AM
Using serial port card(PCMCIA) with IPAQ running Linux, can't find ttyS0 port d2army Linux - Laptop and Netbook 0 11-12-2005 08:07 PM
Send an .hex file by serial port ODSunal Programming 2 12-09-2004 08:12 PM


All times are GMT -5. The time now is 10:12 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration