Thread: Serial Ports

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    21

    Serial Ports

    Hello,

    I haven't done programming and a bit and am a little rusty. I have a project where I have to read from a serial port (USB) and write it to a file, but I'm having a hard time starting. I know I have to first initialize the port, and then read it. Can anyone please point me in the right direction? I'm reading from a linux laptop if that makes a difference.

    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Along with the Serial howto provided above, I find this page helpful.

    Jim

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    21
    Quote Originally Posted by jimblumberg View Post
    Along with the Serial howto provided above, I find this page helpful.

    Jim
    I tried the code on the posted page but for some reason I am getting nothing. I know that the code opens the port because the error "failed to open port" prints only when the usb is not connected to the computer but I am only getting my other printf functions (printing the falue of fd) as

    3

    3

    Here is the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <termios.h>
    
    int fd;
    
    int
    open_port(void)
         {
    fd = open("/dev/serial/by-path/name.of.usb.port0", O_RDWR | O_NOCTTY | O_NDELAY);
    printf("%d\n\n", fd);
    
    if (fd == -1)
    {
    printf("open_port:  Unable to open usb -");
    }
    else
    fcntl(fd, F_SETFL, 0);
    return (fd);
    }
    main()
    {
    open_port();
    printf("%d\n\n",fd);
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Printing the value of errno ( say by using perror() ), would tell you WHY it failed to open.
    Then you might be able to figure out what to do about it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    21
    Quote Originally Posted by Salem View Post
    Printing the value of errno ( say by using perror() ), would tell you WHY it failed to open.
    Then you might be able to figure out what to do about it.

    I didn't know that. But it's not failing to open, so that information isn't necessary. The issue is that I'm not getting any information from the port other than the value of fd from the printf function.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you're not going to either, until you start calling read()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also you will have to set the port parameters correctly before you can get any information from the port.

    Jim

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    21
    Quote Originally Posted by Salem View Post
    Well you're not going to either, until you start calling read()
    Ha, I did say I hadn't programmed in a while.

    Jim, how do I set port parameters?

    And I'm missing the syscalls.h directory any idea how to install that on Fedora 14?

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    how do I set port parameters?
    That is covered in Chapter 2 of the link I provided. You must set the baud, parity, number of bits, etc.

    Why do you need syscalls.h? I think that is an include file that would be part of the kernel sources.

    Jim

  11. #11
    Registered User
    Join Date
    May 2011
    Posts
    21
    It's missing. Go figure. I had to install gcc and vim too but that was fairly painless once I got it hooked up to the internet.

    Thanks Jim.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polling serial ports
    By Nevyn in forum Windows Programming
    Replies: 3
    Last Post: 06-12-2002, 03:02 AM
  2. Polling serial ports
    By Nevyn in forum Windows Programming
    Replies: 0
    Last Post: 06-06-2002, 08:11 AM
  3. Serial Ports
    By SavesTheDay in forum C Programming
    Replies: 4
    Last Post: 02-04-2002, 03:52 PM
  4. Serial Ports
    By camposv in forum C Programming
    Replies: 2
    Last Post: 01-30-2002, 12:18 PM
  5. please help serial ports again
    By alcoholic in forum C Programming
    Replies: 1
    Last Post: 10-11-2001, 06:00 PM