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
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
Always assuming you have some method of accessing the serial port. Do you have some method of accessing the serial port?
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:
(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].Code:int count = 0; unsigned char data = DataFromSerialPort(); for(i = 0; i < 8;i++) { count += data & 1; data >>= 1; }
--
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.
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 03:17 PM.
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.