Count number of 1 in a binary data trame through the serial port DB9

This is a discussion on Count number of 1 in a binary data trame through the serial port DB9 within the C Programming forums, part of the General Programming Boards category; Hi guys, i have a question. can we count the number of 1 in the data trame continuous incoming from ...

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

    Count number of 1 in a binary data trame through the serial port DB9

    Hi guys,
    i have a question. can we count the number of 1 in the data trame continuous incoming from the RxD pin of the serial port DB9 (it a binary signal 0 and 1)?
    thank you very much

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Always assuming you have some method of accessing the serial port. Do you have some method of accessing the serial port?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Normal asynchronous serial ports (RS232 compatible for example) give you 8 bits of data at a time, in a packet of 10 bits of start + 8 data + stop bits. If you receive a 8-bit data packet, you can of course count the number of ones in it:
    Code:
    int count = 0;
    unsigned char data = DataFromSerialPort();
    for(i = 0; i < 8;i++)
    {
      count += data & 1;
      data >>= 1;
    }
    (There are ways to do a table that holds for example 16 values, with the corresponding number of 1s of each 4-bit half of an 8 bit number, which speeds up the process a bit. But it's a lot more to write].

    --
    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
    Oct 2008
    Location
    TX
    Posts
    2,047
    If on UNIX, open the device file that corresponds to the serial port, read data from it and bitwise AND it with the binary equivalent of the RxD pin set.
    Last edited by itCbitC; 01-28-2009 at 02:17 PM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    If on UNIX, open the device file that corresponds to the serial port, read data from it and bitwise AND it with the binary equivalent of the RxD pin set.
    Huh?
    Reading the device-file will give you, a byte at a time, from the serial port's RxD pin - since that's the pin that receives the data from the other end. There is no need for any AND operation in this place. Of course, to count the number of 1s, we'd have to use the AND operator (at least, if we use the method from above).

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

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by matsp View Post
    Huh?
    Reading the device-file will give you, a byte at a time, from the serial port's RxD pin - since that's the pin that receives the data from the other end.
    Need not be a single byte but a number of them depending on the characters available in the serial input buffers though the read() can be constrained to get one character at a time.
    Quote Originally Posted by matsp View Post
    There is no need for any AND operation in this place. Of course, to count the number of 1s, we'd have to use the AND operator (at least, if we use the method from above).

    --
    Mats
    What did you think I was implying, if not the obvious??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serial port: How to know when data has been completely sent
    By arjunajay in forum Windows Programming
    Replies: 8
    Last Post: 03-25-2009, 08:18 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. Writing data to serial port
    By HAssan in forum C Programming
    Replies: 4
    Last Post: 10-16-2006, 12:07 PM
  4. Serial Port - Problem with binary frames
    By estratos in forum Linux Programming
    Replies: 2
    Last Post: 01-18-2006, 05:22 AM
  5. my serial port data reading program isn't working
    By gnychis in forum C Programming
    Replies: 5
    Last Post: 06-02-2005, 08:40 AM

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