Thread: RS232/DB9 in C

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    3

    RS232/DB9 in C

    Hi everybody,
    how are you?
    I'd like to ask you few questions about the C programming. I have a signal TTL pulse arrive on the PIN RxD of the serial port COM1 (DB9). Normally, the program will be in waiting mode (the TTL signal is 0), then, once the TTL raise to 1 (a pulse)(and go back to 0 just after-about 4ms) for the first time, the program start to count. After 20 times that the TTL signal raises, the program will send a message through the port COM1. I'm really newbie in C, so, if someone can explain me how to do that, that will be really great.
    Thank you very much

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by minhdung_hoang View Post
    Hi everybody,
    how are you?
    I'd like to ask you few questions about the C programming. I have a signal TTL pulse arrive on the PIN RxD of the serial port COM1 (DB9). Normally, the program will be in waiting mode (the TTL signal is 0), then, once the TTL raise to 1 (a pulse)(and go back to 0 just after-about 4ms) for the first time, the program start to count. After 20 times that the TTL signal raises, the program will send a message through the port COM1. I'm really newbie in C, so, if someone can explain me how to do that, that will be really great.
    Thank you very much

    try this

    Code:
    #include <stsdio.h>
     
    FILE* pFile = fopen("COM1:" , "w+b");
    for(x=0;x<20;x++){
       fread(...);  // read one character
       }
    fwrite(...);
    fclose(pFile);
    Last edited by abachler; 01-19-2009 at 04:08 PM.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Reading/writing serial ports is totally system dependant, and without further information as to what your hardware is, we can not help you.

    Also, you probably can not send 20 pulses up and down on the serial port - it sends data in blocks of (usually) 8 bits, with a defined number of start and stop bits at either end of the 8 bits. The start and stop bits are always going to be a constant value (I'm sorry, can't remember if start bit is 1 and stop bit 0 or the other way around).

    If your machine has a modern OS, such as Linux or Windows, then you will not be able to access the serial port directly through instructions that touch the real hardware - you will need to use the OS's system calls (possibly via upper layer C library like abachler suggests - but you may also NOT want to do that, as it adds another layer of abstraction that may get in the way of what you want to do - again, it depends on what you are actually trying to achieve).

    And before this turns into a 20 questions session, I'd like to point out that if you explain what the OVERALL goal of your project is, we can most likely help you a whole lot more than if you drip feed us exactly what we ask for at each level - it will take longer that way, and we may very well say "Well, if THAT is what you are doing, you probably shouldn't do it that way, but THIS way" after several posts. So to get the best advice, give us as much of the BIG picture as possible.

    --
    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.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    3
    Thank you for your answer quickly, but i think you misunderstood me (my English is bad). I'll receive a TTL signal through the RS232 port (system Windows XP). I have to count each time that the TTL pass to 1 (it'll turn back to 0 just after). I think that the rest of program i can handle by myself. Please just tell me how can i count each time the TTL raises.
    Thank you

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What is receiving these TTL signals? You can not count transitions on the serial port on Windows XP, but if you have some sort of embedded processor, you may well be able to do that. Unfortunately HOW you go about doing that will depend on exactly how the serial port is connected, and what the actual hardware is, and possibly some other factors.

    --
    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.

Popular pages Recent additions subscribe to a feed