Thread: Serial port Communication

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    99

    Serial port Communication

    Dear Experts,

    i trying to read the data from serial port COM1,
    though my program is correct i am not understanding how to provide the inputs and how to check its output,

    Code:
    #include <dos.h>
    #include <stdio.h>
    #include <conio.h>
    
    #define PORT1 0x3F8  /* Defines Serial Port Base Address (COM1 */
    
    void main(void){
        unsigned char c = 0;
        unsigned char chrctr = 0;
        /*int exit = 1; */
    
        outportb(PORT1 + 1, 0); /* Turn off interrupts */
    
        /* PORT1 Communication Settings */
    
        outportb(PORT1 + 3, 0x80); /* Set DLAB ON */
        outportb(PORT1 + 0, 0x0C); /* Set the baud rate to 9600 */
    
        outportb(PORT1 + 1, 0x00); /* Set Baud - Divisor latch HIGH */
        outportb(PORT1 + 3, 0x03); /* 8 bits, no parity, 1 stop */
        outportb(PORT1 + 2, 0xC7); /* FIFO Control Register */
        outportb(PORT1 + 4, 0x0B); /* Turn on DTR, RTS, and OUT2) */
    
        printf("Waiting on transmission from source.\nPress ESC to quit.\n");
    
        while(chrctr != 27){      /* Execute the loop if ESC has been hit */
        c = inportb(PORT1 + 5);
        if (c & 0x01){
            chrctr = inportb(PORT1);
            printf("%d",chrctr);
            }
        if (kbhit()){
            chrctr = getch();
            outportb(PORT1, chrctr);
            }
        }
        }"

    when i execute this program at dos prompt it says
    Code:
    Waiting on transmission from source.\nPress ESC to quit.
    
    and when i press escape it will come out as the loop suggest
    but i want communicate through that port please tell me how to check the outputs for this program as i am not much aeware of it please experts tell me how read the data from the serial COM1.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    What version of DOS is this running under?

    gg

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    From other threads, it seems a safe bet that their real OS is something like XP, and the compiler is turboc.
    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.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I believe legacy mode of reading serial port is supported in DOS mode - of course, it assumes that COM1 is actually allocated to 0x3F8 (check in the Device Manager).

    How have you set the other end up? What is sending to your serial port?


    Of course for Windows XP (or any other DOS version post Windows 2.x) it will be MUCH more efficient to read and write the data through Windows (because the way that the DOS mode is handled, the data will still be sent through the Windows API, just detouring around the DOS emulations subsystem first).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    99

    serial port communication

    Dear experts,

    thank you for your reply,
    yes i am widows xp and installed the turbo c here and trying to run that program , is it is right way,
    please help me i want to communicate to serial port
    COM1, what i have to do , or what modifications has to be done, in device manager which properties need to be checked,
    whether it is General, Driver,Port Settings,Details or Resources.

    please help me,

    with regards,
    vidya.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to check that the port-address of COM1 is 0x3F8, I think it's under resources.

    You still haven't answered how you are connecting your XP system to something on the serial port.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    99

    Serial port communication

    Dear experts,

    thank you for u r reply,
    ya that COM1
    i/o range it shows:03F8-03FF

    what i am doing is i am working on windows Xp and installed the turbo c here,
    and went through run->cmd (command line)

    c:\tc>tc

    i got the editor where i typed this program and compiled it compiled fine,
    and when executed it simply shows nothing ,
    so how to communicate properly please advice me,
    how to modify this so that we can communicate please advice me.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Before you decide that it doesn't work, I would suggest you run hyperterminal to see if there is actually anything coming in on COM1.
    As matsp says, printers are not overly chatty, there may be nothing to 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.

  9. #9
    Registered User
    Join Date
    Feb 2004
    Posts
    29
    first use hyper terminal to check your com port is actually doing anything

    http://technet.microsoft.com/en-us/l.../cc728155.aspx

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    99

    serial port communication

    Thank you for u r reply,

    i will definitely check the hyper terminal with COM1 port,
    but my actual question is how to connect the printer to the COM1 port,
    still i have not connected it,what is the procedure,

    as i have set of procedure how to connect hyper terminal , how to configure it through COM1 is there any way to connect the printer,


    please tell me.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Did you read the link that Pico posted: http://technet.microsoft.com/en-us/l.../cc728155.aspx

    As to "how you connect the printer to serial port", you'd have to have the right cable first of all - probably comes with the printer, but I don't know - whoever sold you the printer may know... This is really quite far from C++ programming...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    99

    serial port communication

    dear experts,
    thank u for reply,

    ya i understood know,as u suggested i used the Hyperterminal to check whether COM ports are working, for that i used one serial cable ,

    but it worked for following configuration of COM1

    Bits per second :9600
    Data bits :8
    Parity :none
    Stopbits :2
    Flow control :xon/xoff

    so my question is whether i can use this configuration and use that serial cable
    and and use my program to communicate between 2pcs, and also what is the configuration that i have to replace for Flow control:xon/xoff which was 0 previously.

    what are u r suggestions insead of printer is it possible to make communication between two pcs, please give me u r suggestion, how to do, whether one
    hyperterminal and one program or running program on both the PCs,
    please help me.
    Last edited by vin_pll; 01-06-2009 at 04:06 AM.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What are you ACTUALLY trying to achieve?

    And you can use full words in this forum - it is not terribly limited in space, so saving two characters by typing "u" instead of "you" is just annoying - that makes sense when you are trying to get the most out of a 16-character display on an old mobile phone, or try to get the most out of a 240-byte packet for SMS. Not much point here - I don't know what the limit for a post is, but it's certainly several kilobytes, and most people have a 800 x 600 or better resolution.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplex communication thro serial port
    By Priyachu in forum Networking/Device Communication
    Replies: 1
    Last Post: 05-30-2009, 04:24 AM
  2. Duplex communication thro serial port
    By Priyachu in forum Linux Programming
    Replies: 1
    Last Post: 05-30-2009, 04:03 AM
  3. Serial Port Communication
    By maxorator in forum C++ Programming
    Replies: 11
    Last Post: 04-27-2006, 03:13 PM
  4. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM
  5. Need help or info about serial port communication
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 01-08-2002, 01:48 PM