Obtaining data through RS-232 port...

This is a discussion on Obtaining data through RS-232 port... within the C Programming forums, part of the General Programming Boards category; I am trying to do a C program that will be able to grab data from an RS-232 port to ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    5

    Obtaining data through RS-232 port...

    I am trying to do a C program that will be able to grab data from an RS-232 port to a PC and then save whatever data into a text file.... The RS-232 port will keep sending its data out to the PC, so I need to overwrite the text file when a new data is sent to the PC. Can someone tell me how to program this in C?
    Thanks...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Start with which OS and compiler you are using...
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    5
    windows xp and GNU C compiler

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Oh great, we get to play 20 questions just to figure out your environment
    Which gcc compiler
    • cygwin
      dev-c++
      DJGPP

    All have gcc at the heart of them
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    5
    hehe.... sorry.... it's cygwin

  6. #6
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    Check the existence of /dev/ttyS0, but I'm not sure.
    Umm..may be COM1 on Windows, just try to open it as a text file.
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    5
    how do u check it????

  8. #8
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    >>how do u check it????
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      char c,*fn=(char *)malloc(5);
      FILE *fp;
      fn="COM1:"
      if ((fp=fopen(fn,"r"))!=NULL) {
        while ((c=(char)getc(fp))!=EOF)
          putc(c,stdout);
        fclose(fp);
      } else
      printf("file not found.\n");
      free(fn);
      return 0;
    }
    Try it I don't have Windows installed, I'm not sure.
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > fn="COM1:"
    This loses the memory you malloc'ed
    The cast of malloc is not recommended either.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    >>The cast of malloc is not recommended either.

    Thanks for that as I use gcc 3.2.2 and sometimes gcc 2.95 (old redhat). The second one need casting. It never compile without casting. So it turns into my habit.
    Anyway I'll change
    Last edited by Jaguar; 10-08-2003 at 11:49 AM.
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 11:13 AM
  2. FTP program
    By jakemott in forum Linux Programming
    Replies: 14
    Last Post: 10-06-2008, 02:58 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  4. accessing my com port, writing and reading data
    By shoobsie in forum C Programming
    Replies: 7
    Last Post: 09-16-2005, 04:29 PM
  5. serial port data transmission
    By djarian in forum C Programming
    Replies: 2
    Last Post: 10-04-2003, 03:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21