use of serial and parrallel port using C

Discussion in 'C Programming' started by The Man With The Harmonica, Aug 18, 2004.

  1. Hi All,

    What kind of commands can I use to control the serial and parallel port of
    my pc. I want to use my laptop instead of microcontrollers and write my
    software in ANSI C to be used for control purposes of sensors and other
    electronic devices.

    I haven't done any C for a while now so I would assume my knowledge is that
    of a beginner now.

    A thousand thanks for any help offered or intended.

    Naveed
     
    The Man With The Harmonica, Aug 18, 2004
    #1
    1. Advertising

  2. "The Man With The Harmonica" <> wrote in
    news:cfvpon$dvf$:

    > Hi All,
    >
    > What kind of commands can I use to control the serial and parallel port
    > of my pc.


    None. ISO C doesn't have any commands nor does it know anything about
    serial or parrallel ports. You'll need to ask this in a newsgroup that
    programs for the platform you are using, e.g. DOS, Linux, Win32, etc.

    --
    - Mark ->
    --
     
    Mark A. Odell, Aug 18, 2004
    #2
    1. Advertising

  3. The Man With The Harmonica wrote:

    > Hi All,
    >
    > What kind of commands can I use to control the serial and parallel port of
    > my pc. I want to use my laptop instead of microcontrollers and write my
    > software in ANSI C to be used for control purposes of sensors and other
    > electronic devices.
    >
    > I haven't done any C for a while now so I would assume my knowledge is that
    > of a beginner now.
    >
    > A thousand thanks for any help offered or intended.
    >
    > Naveed
    >
    >


    news:comp.arch.embedded

    One can use ANSI C to access serial ports and parallel ports
    as well as other devices as long as those devices are memory
    mapped. Whether the operating system, if there is one, allows
    this is a different issue.

    However, this code would be platform specific and not portable
    to platforms that do not have the same memory mapping.

    The technique is to access the hardware devices by dereferencing
    pointers. For example, one would assign a pointer to an integer
    with the value of the device's address:
    volatile unsigned char * Serial_Receive_Register =
    (volatile unsigned char *) 0x40000;

    unsigned char Read_Serial_Port(void)
    {
    return * Serial_Receive_Register;
    }

    Before reading the receive register, one should check that
    the status first and also have a design to handle when
    nothing has been received.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Aug 18, 2004
    #3
  4. The Man With The Harmonica

    SM Ryan Guest

    # What kind of commands can I use to control the serial and parallel port of
    # my pc. I want to use my laptop instead of microcontrollers and write my
    # software in ANSI C to be used for control purposes of sensors and other
    # electronic devices.

    Depends in part on whether the operating system provides file names for
    those devices that you can fopen. For example on unices, you can open
    serial ports with file names like "/dev/ttyS0".

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    The whole world's against us.
     
    SM Ryan, Aug 18, 2004
    #4
  5. The Man With The Harmonica

    Dan Pop Guest

    In <6iKUc.4812$> Thomas Matthews <> writes:

    >The Man With The Harmonica wrote:
    >
    >> Hi All,
    >>
    >> What kind of commands can I use to control the serial and parallel port of

    ^^
    >> my pc. I want to use my laptop instead of microcontrollers and write my

    ^^^^^ ^^^^^^^^^

    >> software in ANSI C to be used for control purposes of sensors and other
    >> electronic devices.
    >>
    >> I haven't done any C for a while now so I would assume my knowledge is that
    >> of a beginner now.
    >>

    >news:comp.arch.embedded


    Since when do PC's count as embedded control systems?

    >One can use ANSI C to access serial ports and parallel ports
    >as well as other devices as long as those devices are memory
    >mapped.


    Chapter and verse, please.

    >Whether the operating system, if there is one, allows
    >this is a different issue.


    If ANSI C supported the feature, the OS would be irrelevant.

    >However, this code would be platform specific and not portable
    >to platforms that do not have the same memory mapping.


    Then, it wouldn't be code blessed by ANSI C.

    >The technique is to access the hardware devices by dereferencing
    >pointers. For example, one would assign a pointer to an integer
    >with the value of the device's address:
    > volatile unsigned char * Serial_Receive_Register =
    > (volatile unsigned char *) 0x40000;


    According to ANSI C, any attempt to use this pointer value results in
    undefined behaviour.

    The *right* thing is to use the OS interface to the serial port. The
    kind of code you're showing belongs exclusively to device drivers and
    is highly unlikely to work in userland programs. Imagine what happens
    when two programs attempt to access the same port directly, at the same
    time (assuming that it would be possible): complete chaos.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Aug 18, 2004
    #5
  6. "Mark A. Odell" <> wrote in message
    news:Xns95496D1A74290CopyrightMarkOdell@130.133.1.4...
    > "The Man With The Harmonica" <> wrote in
    > news:cfvpon$dvf$:
    >
    > > Hi All,
    > >
    > > What kind of commands can I use to control the serial and parallel port
    > > of my pc.

    >
    > None. ISO C doesn't have any commands nor does it know anything about
    > serial or parrallel ports. You'll need to ask this in a newsgroup that
    > programs for the platform you are using, e.g. DOS, Linux, Win32, etc.
    >
    > --
    > - Mark ->
    > --


    Hi Mark,

    I'm using an old Toshiba laptop with win95. I want to use the parallel port
    to control some circuits and to read the state of some control lines.
    Basically its going to be controlling a four wheeled buggy and I want to do
    this using C and through the parallel port.

    thanks

    Naveed
     
    The Man With The Harmonica, Aug 19, 2004
    #6
  7. The Man With The Harmonica

    Flash Gordon Guest

    On Thu, 19 Aug 2004 16:51:06 +0100
    "The Man With The Harmonica" <> wrote:

    > "Mark A. Odell" <> wrote in message
    > news:Xns95496D1A74290CopyrightMarkOdell@130.133.1.4...
    > > "The Man With The Harmonica" <> wrote
    > > in news:cfvpon$dvf$:
    > >
    > > > What kind of commands can I use to control the serial and parallel
    > > > port of my pc.

    > >
    > > None. ISO C doesn't have any commands nor does it know anything
    > > about serial or parrallel ports. You'll need to ask this in a
    > > newsgroup that programs for the platform you are using, e.g. DOS,
    > > Linux, Win32, etc.

    >
    > I'm using an old Toshiba laptop with win95. I want to use the
    > parallel port to control some circuits and to read the state of some
    > control lines. Basically its going to be controlling a four wheeled
    > buggy and I want to do this using C and through the parallel port.


    So go and ask on a a group for you platform as Mark suggested. Standard
    C does not provide you with any way to to what you want and this group
    talks about standard C.
    --
    Flash Gordon
    Sometimes I think shooting would be far too good for some people.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Aug 19, 2004
    #7
  8. The Man With The Harmonica

    SM Ryan Guest

    # So go and ask on a a group for you platform as Mark suggested. Standard
    # C does not provide you with any way to to what you want and this group
    # talks about standard C.

    Still shooting from the hip and blowing off your own toes. Depending on
    the operating system, it may be possible to fopen a device name and
    then use stdio to read/write the port.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Why are we here?
    whrp
     
    SM Ryan, Aug 19, 2004
    #8
  9. The Man With The Harmonica

    Default User Guest

    SM Ryan wrote:
    >
    > # So go and ask on a a group for you platform as Mark suggested. Standard
    > # C does not provide you with any way to to what you want and this group
    > # talks about standard C.
    >
    > Still shooting from the hip and blowing off your own toes. Depending on
    > the operating system, it may be possible to fopen a device name and
    > then use stdio to read/write the port.



    Exactly how stupid are you? There is no standard way to do that. There
    are only platform-specific ways, and so the person seeking the
    information should go ask on a newsgroup dedicated to the specific
    platform.

    Since you first graced our newsgroup, you've been a fount of bad
    information and bad attitude, as well as continuing to use those
    non-standard reply delimiters.



    Brian Rodenborn
     
    Default User, Aug 19, 2004
    #9
  10. The Man With The Harmonica

    Flash Gordon Guest

    On Thu, 19 Aug 2004 21:33:23 -0000
    SM Ryan <> wrote:

    > # So go and ask on a a group for you platform as Mark suggested.
    > Standard# C does not provide you with any way to to what you want and
    > this group# talks about standard C.
    >
    > Still shooting from the hip and blowing off your own toes. Depending

    ^^^^^^^^^
    > on the operating system, it may be possible to fopen a device name and

    ^^^^^^^^^^^^^^^^^^^^^^^
    > then use stdio to read/write the port.


    You said it yourself, it DEPENDS ON THE OPERATING SYSTEM. Therefor it
    needs to be asked in a group dealing with the operating system. If the
    OP find it can be done using fopen and friends, then questions can be
    asked about the things which can be done in standard C. However, if the
    OP knew that was possible then the question would not have been asked.
    --
    Flash Gordon
    Sometimes I think shooting would be far too good for some people.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Aug 20, 2004
    #10
  11. The Man With The Harmonica

    SM Ryan Guest

    Default User <> wrote:
    # SM Ryan wrote:
    # >
    # > # So go and ask on a a group for you platform as Mark suggested. Standard
    # > # C does not provide you with any way to to what you want and this group
    # > # talks about standard C.
    # >
    # > Still shooting from the hip and blowing off your own toes. Depending on
    # > the operating system, it may be possible to fopen a device name and
    # > then use stdio to read/write the port.
    #
    #
    # Exactly how stupid are you? There is no standard way to do that. There

    Since when did fopen, fgets, fputs, iovbuf, and system stop being part
    of ANSI C? How stupid are you?

    # are only platform-specific ways, and so the person seeking the

    Every mapping of a file name to some external entity is platform
    specific. Does that mean you forbid discussion of all programs which
    call fopen?

    # Since you first graced our newsgroup, you've been a fount of bad
    # information and bad attitude, as well as continuing to use those
    # non-standard reply delimiters.

    Tough shit. I'm not here for the approval of your clique. I'm here
    for the lost who get chased away because of your kneejerk responses.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    I'm not even supposed to be here today.
     
    SM Ryan, Aug 20, 2004
    #11
  12. The Man With The Harmonica

    SM Ryan Guest

    Flash Gordon <> wrote:
    # On Thu, 19 Aug 2004 21:33:23 -0000
    # SM Ryan <> wrote:
    #
    # > # So go and ask on a a group for you platform as Mark suggested.
    # > Standard# C does not provide you with any way to to what you want and
    # > this group# talks about standard C.
    # >
    # > Still shooting from the hip and blowing off your own toes. Depending
    # ^^^^^^^^^
    # > on the operating system, it may be possible to fopen a device name and
    # ^^^^^^^^^^^^^^^^^^^^^^^
    # > then use stdio to read/write the port.
    #
    # You said it yourself, it DEPENDS ON THE OPERATING SYSTEM. Therefor it

    If you actually botherred to think about it for a moment, openning any
    file DEPENDS ON THE OPERATING SYSTEM. Disk file, terminal, any file at
    all. So is openning a disk file something you cannot do in standard C,
    just because it requires a system to map file names into disk files?

    # needs to be asked in a group dealing with the operating system. If the
    # OP find it can be done using fopen and friends, then questions can be
    # asked about the things which can be done in standard C. However, if the
    # OP knew that was possible then the question would not have been asked.

    That's a silly claim. I don't know if the OP knows whether his system
    maps devices to file names or not. That part is system specific. But if
    it is possible, then the device can be openned and read and written in
    standard C. Explain exactly what part of the following code is not ANSI C.

    #include <stdio.h>

    static FILE *openport(int port,int speed,int csize,char parity) {
    char path[100],conf[100];
    sprintf(path,"/dev/ttyS%d",port);
    sprintf(conf,"stty -f /dev/stty -f /dev/ttyS%d speed %d cs%d %s",
    port,speed,csize,
    parity=='n' ? "-parenb"
    : parity=='o' ? "parenb parodd"
    : "parenb -parodd");
    if (!system(0)) return 0;
    if (system(conf)) return 0;
    return fopen(path,"r+");
    }

    And then tell me again how it is impossible to talk to a device in
    ANSI C.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Wow. A sailboat.
     
    SM Ryan, Aug 20, 2004
    #12
  13. The Man With The Harmonica

    John Smith Guest

    "SM Ryan" <> wrote in
    message news:...
    > Default User <> wrote:
    > # SM Ryan wrote:
    > # >
    > # > # So go and ask on a a group for you platform as Mark suggested.

    Standard
    > # > # C does not provide you with any way to to what you want and this

    group
    > # > # talks about standard C.
    > # >
    > # > Still shooting from the hip and blowing off your own toes. Depending

    on
    > # > the operating system, it may be possible to fopen a device name and
    > # > then use stdio to read/write the port.
    > #
    > #
    > # Exactly how stupid are you? There is no standard way to do that. There
    >
    > Since when did fopen, fgets, fputs, iovbuf, and system stop being part
    > of ANSI C? How stupid are you?
    >
    > # are only platform-specific ways, and so the person seeking the
    >
    > Every mapping of a file name to some external entity is platform
    > specific. Does that mean you forbid discussion of all programs which
    > call fopen?
    >
    > # Since you first graced our newsgroup, you've been a fount of bad
    > # information and bad attitude, as well as continuing to use those
    > # non-standard reply delimiters.
    >
    > Tough shit. I'm not here for the approval of your clique. I'm here
    > for the lost who get chased away because of your kneejerk responses.
    >
    > --
    > SM Ryan http://www.rawbw.com/~wyrmwif/
    > I'm not even supposed to be here today.


    Well said. I agree with your philosophy: Usenet should be a useful place to
    all comers, not just those 'elite' who think they are above the rest.

    I tend to think they respond with "wrong group" because they either don't
    know the answer or couldn't be bothered to work out the problems posed.
     
    John Smith, Aug 20, 2004
    #13
  14. The Man With The Harmonica

    John Smith Guest

    "Flash Gordon" <> wrote in message
    news:-gordon.me.uk...
    > On Thu, 19 Aug 2004 21:33:23 -0000
    > SM Ryan <> wrote:
    >
    > > # So go and ask on a a group for you platform as Mark suggested.
    > > Standard# C does not provide you with any way to to what you want and
    > > this group# talks about standard C.
    > >
    > > Still shooting from the hip and blowing off your own toes. Depending

    > ^^^^^^^^^
    > > on the operating system, it may be possible to fopen a device name and

    > ^^^^^^^^^^^^^^^^^^^^^^^
    > > then use stdio to read/write the port.

    >
    > You said it yourself, it DEPENDS ON THE OPERATING SYSTEM. Therefor it
    > needs to be asked in a group dealing with the operating system. If the
    > OP find it can be done using fopen and friends, then questions can be
    > asked about the things which can be done in standard C. However, if the
    > OP knew that was possible then the question would not have been asked.
    > --
    > Flash Gordon
    > Sometimes I think shooting would be far too good for some people.
    > Although my email address says spam, it is real and I read it.


    Uhhhh.... the OP said he was rusty, and so didn't know the answer in the
    first place. Dunce.
     
    John Smith, Aug 20, 2004
    #14
  15. The Man With The Harmonica

    Flash Gordon Guest

    On Fri, 20 Aug 2004 12:50:53 +0100
    "John Smith" <> wrote:

    > Well said. I agree with your philosophy: Usenet should be a useful
    > place to all comers, not just those 'elite' who think they are above
    > the rest.


    If a group is filled with off-topic posts then it becomes useless
    because you can't find the on-topic posts.

    > I tend to think they respond with "wrong group" because they either
    > don't know the answer or couldn't be bothered to work out the problems
    > posed.


    I know a bit about Win95, which is what the OP is using. However, a lot
    more expertise will be found on a Win95 programming group than here. So
    it is actually in the OPs interest to go to a Win95 programming group in
    order to get the best possible advice.

    Also, the OP wants to read from the parallel port, something which may
    not be as simple as using fopen because you may need to switch the
    hardware in to it's bidirectional mode (either EPP or ECP), something
    early centronics interfaces did not support. Help on this won't be
    obtained here and any responses with errors suggesting how to do this
    won't get corrected.
    --
    Flash Gordon
    Sometimes I think shooting would be far too good for some people.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Aug 20, 2004
    #15
  16. The Man With The Harmonica

    Default User Guest

    SM Ryan wrote:

    > Since when did fopen, fgets, fputs, iovbuf, and system stop being part
    > of ANSI C? How stupid are you?


    Since when are they guaranteed to have anything to do with serial and
    parallel ports? They are for opening files.

    > Tough shit. I'm not here for the approval of your clique. I'm here
    > for the lost who get chased away because of your kneejerk responses.



    The "lost" will be much better served by redirection to correct
    newsgroups where they will receive complete, peer-reviewed answers
    rather than your half-assed, unhelpful crap.




    Brian Rodenborn
     
    Default User, Aug 20, 2004
    #16
  17. The Man With The Harmonica

    CBFalconer Guest

    John Smith wrote:
    > "SM Ryan" wrote:
    > > Default User <> wrote:

    >

    .... snip ...
    >>
    >># Since you first graced our newsgroup, you've been a fount of
    >># bad information and bad attitude, as well as continuing to use
    >># those non-standard reply delimiters.
    >>
    >> Tough shit. I'm not here for the approval of your clique. I'm
    >> here for the lost who get chased away because of your kneejerk
    >> responses.

    >
    > Well said. I agree with your philosophy: Usenet should be a useful
    > place to all comers, not just those 'elite' who think they are
    > above the rest.
    >
    > I tend to think they respond with "wrong group" because they
    > either don't know the answer or couldn't be bothered to work out
    > the problems posed.


    If you want to discuss off-topic things you are quite free to get
    your own newsgroup going. The alt hierarchy is quite easy to set
    up. Then you have to persuade ISPs to carry it.

    Ryan has already been plonked by many people for refusal to
    co-operate. Do not emulate his idiocy.

    If a news group discusses anything at all it becomes a morass and
    totally meaningless. That is the purpose of having multiple
    groups - each can stick to a general subject.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Aug 20, 2004
    #17
  18. The Man With The Harmonica

    SM Ryan Guest

    Default User <> wrote:
    # SM Ryan wrote:
    #
    # > Since when did fopen, fgets, fputs, iovbuf, and system stop being part
    # > of ANSI C? How stupid are you?
    #
    # Since when are they guaranteed to have anything to do with serial and
    # parallel ports? They are for opening files.

    What exactly is a file?

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    You face forward, or you face the possibility of shock and damage.
     
    SM Ryan, Aug 20, 2004
    #18
  19. In article <cg019n$8f7$>, Dan Pop <> wrote:
    ....
    (Totally O/T)
    >The *right* thing is to use the OS interface to the serial port.


    I would like to have heard you say that 20 years ago, in DOS days, when it
    was well understood by everybody that the standardized, officially
    supported, OS-supplied serial port APIs simply did not work, and thus any
    program that needed performance had to talk directly to the hardware.

    >The kind of code you're showing belongs exclusively to device drivers and
    >is highly unlikely to work in userland programs. Imagine what happens
    >when two programs attempt to access the same port directly, at the same
    >time (assuming that it would be possible): complete chaos.


    I often get the feeling that these (obviously OT) posts that we keep seeing
    here are coming from people in obscure corners of the world, where they
    don't realize that the world has, indeed, changed in the last 20 years
    (i.e., that the assertions made above are, in fact, no longer true).
     
    Kenny McCormack, Aug 21, 2004
    #19
  20. The Man With The Harmonica

    Old Wolf Guest

    SM Ryan <> wrote:
    > Flash Gordon <> wrote:
    > # On Thu, 19 Aug 2004 21:33:23 -0000
    > # SM Ryan <> wrote:
    > #
    > # > # So go and ask on a a group for you platform as Mark suggested.
    > # > Standard# C does not provide you with any way to to what you want and
    > # > this group# talks about standard C.


    > Explain exactly what part of the following code is not ANSI C.


    Your code does not open a serial port on my fully ANSI-C conforming
    MS windows system. Also it does not open a serial port on my
    Linux system, where my usual username does not have permission to
    open those devices. Please respond when you have fixed your code
    so that it works on all ANSI-C conforming systems (including those
    with no serial ports).

    > #include <stdio.h>
    >
    > static FILE *openport(int port,int speed,int csize,char parity) {
    > char path[100],conf[100];
    > sprintf(path,"/dev/ttyS%d",port);
    > sprintf(conf,"stty -f /dev/stty -f /dev/ttyS%d speed %d cs%d %s",
    > port,speed,csize,
    > parity=='n' ? "-parenb"
    > : parity=='o' ? "parenb parodd"
    > : "parenb -parodd");


    Buffer overflow, on 64-bit systems

    > if (!system(0)) return 0;


    Undefined behaviour: system() in the C standard library expects
    a pointer and you gave it an int. (You should fix this by
    including stdlib.h)

    > if (system(conf)) return 0;
    > return fopen(path,"r+");
    > }
    >
    > And then tell me again how it is impossible to talk to a device in
    > ANSI C.


    It is impossible to portably talk to a device in ANSI C.
     
    Old Wolf, Aug 23, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GARY CHAN
    Replies:
    0
    Views:
    572
    GARY CHAN
    Sep 3, 2004
  2. Pom
    Replies:
    2
    Views:
    1,668
    Bas-i
    Jan 31, 2007
  3. Norm
    Replies:
    7
    Views:
    454
  4. msalerno
    Replies:
    3
    Views:
    412
    Ilmari Karonen
    Jul 14, 2005
  5. Replies:
    4
    Views:
    311
Loading...

Share This Page