当前位置:网站首页>What is parity? How to use C language?
What is parity? How to use C language?
2022-07-25 23:48:00 【It Wolf~】
When configuring serial port in serial port tools and code , You often see parity . So what is parity , What is its function ? Let's listen to the blogger ~
1. Why should there be parity ?
It is a verification method introduced to solve the problem that the received data is inconsistent with the sent data due to environmental interference in the process of data transmission , Its advantage is that it's easy to implement , The disadvantage is that the accuracy of verification is not high , The error detection rate is only 50%, For example, even bits are disturbed and flipped at the same time , Then parity cannot be checked . And can only judge wrong , Data sending error cannot be corrected , Only resend ;
2. What is parity ?
The last bit of the sending end data is increased by parity bit , After converting the data into binary bit1 The number of is odd or even ; Determine at the receiving end bit1 Whether the number of is odd or even , This verifies whether the data is accurate in the transmission process ;
It is divided into odd check and even check ;
Odd check : After the sent data is converted into binary , Add check bit ,bit1 The number of must be odd . If the data sent is even , Then the last parity bit is added 1, On the contrary, it is odd , Then add 0; The receiver judges that the data is bit1 Whether the number of is odd , If so, the data is valid , Otherwise, the data is invalid ;
Even check : After the sent data is converted into binary , Add check bit ,bit1 The number of must be even ; If the data sent is even , Then the last parity bit is added 0, On the contrary, it is odd , Then add 1; The receiver judges that the data is bit1 Whether the number of is even , If so, the data is valid , Otherwise, the data is invalid ;
As shown in the following results :
6666 After converting to binary, it is 1101000001010, among bit1 For the 5, Odd number . therefore , Parity check needs to be filled in its last digit 1; Odd check needs to be filled in its last digit 0;
3. Parity checked C Language implementation ?
Understand the meaning of parity , Then use C Language implementation is also easy , See the following code for details :
#include <stdio.h>
#include <stdlib.h>
#define uint8_t unsigned char
#define uint16_t unsigned int
/* * PARITY_CHECK_DATA_TYPE: Type of data bit * PARITY_CHECK_PACK_TYPE: The type of packet encapsulated after parity * explain : These two aliases can be used to define data bits of different lengths , Better applicability . * However, it should be noted that a parity bit should be reserved for data bits ( For example, the type of data bit is unsigned 16 When a , Its data range is 0 ~ 2^15) */
typedef uint16_t PARITY_CHECK_DATA_TYPE;
typedef uint16_t PARITY_CHECK_PACK_TYPE;
// Binary conversion of integers
void FuncOutputBin(uint16_t value)
{
char string[17]; // parameter types uint32 The maximum is 16 position , So here I define the size as 17 The string array is used to store
itoa(value, string, 2);
printf("%s\r\n",string);
}
/* * value: Data to be sent * mode : 1 Indicates odd parity ,0 Indicates even parity * Return value : Use parity to encapsulate the data */
PARITY_CHECK_PACK_TYPE ParityCheckSend(PARITY_CHECK_DATA_TYPE value, uint8_t mode)
{
uint8_t i = 0;
PARITY_CHECK_PACK_TYPE result = 0;
uint8_t count = 0;
for(i = 0; (value >> i) != 0; i++) {
if((value >> i) & 0x01) {
count++; // It is used for statistical integer conversion to binary 1 The number of
}
}
if(count % 2 == 0) {
//1 The number of is even
switch(mode)
{
case 0:// Even check is adopted
// Last place to fill 0
result = ((PARITY_CHECK_PACK_TYPE)value << 1);
break;
case 1:// Odd check
// Last place to fill 1
result = ((PARITY_CHECK_PACK_TYPE)value << 1) | 0x01;
break;
default:
return 0;
}
} else {
//1 The number of is odd
switch(mode)
{
case 0:// Even check is adopted
// Last place to fill 1
result = ((PARITY_CHECK_PACK_TYPE)value << 1) | 0x01;
break;
case 1:// Odd check
// Last place to fill 0
result = ((PARITY_CHECK_PACK_TYPE)value << 1);
break;
default:
return 0;
}
}
return result;
}
/* * value: Received data to be parsed * mode : 1 Indicates odd parity ,0 Indicates even parity * Return value : Data parsed with parity , Invalid data returned 0 */
PARITY_CHECK_DATA_TYPE ParityCheckRecieve(PARITY_CHECK_PACK_TYPE value, uint8_t mode)
{
uint8_t i = 0;
PARITY_CHECK_DATA_TYPE result = 0;
uint8_t count = 0;
for(i = 0; (value >> i) != 0; i++) {
if((value >> i) & 0x01) {
count++; // It is used for statistical integer conversion to binary 1 The number of
}
}
switch(mode)
{
case 0:// Even check
if(count % 2 == 0) {
// If the data obtained 1 The bits of are even , Description is valid data
result = (PARITY_CHECK_DATA_TYPE)(value >> 1);
return result;
} else {
return 0;
}
case 1:// Odd check
if(count % 2 == 1) {
// If the data obtained 1 The number of bits of is odd , Description is valid data
result = (PARITY_CHECK_DATA_TYPE)(value >> 1);
return result;
} else {
return 0;
}
default:
return 0;
}
}
int main(void)
{
uint16_t input = 0;
printf(" Please enter a number :");
scanf("%d", &input);
printf(" The original value is :");
FuncOutputBin(input);
printf(" The value after even check is :");
FuncOutputBin(ParityCheckSend(input, 0));
printf(" The value after odd check is :");
FuncOutputBin(ParityCheckSend(input, 1));
printf(" The value after parsing odd check is :");
FuncOutputBin(ParityCheckRecieve(ParityCheckSend(input, 1), 1));
printf(" The value after parsing even check is :");
FuncOutputBin(ParityCheckRecieve(ParityCheckSend(input, 0), 0));
return 0;
}
4. Operation result display

At the end , Welcome to pay attention to our official account “ Brain filling space ”, Will push more tweets for you ~
边栏推荐
猜你喜欢

面试重点——传输层的TCP协议

Duplicate numbers in array

LeetCode 0135. 分发糖果

Node Foundation

VSCode格式化Json文件

2022-07-18 study notes of group 5 self-cultivation class (every day)
![[nodejs] nodejs create a simple server](/img/00/7ab5629bf67777da21b41f44665880.png)
[nodejs] nodejs create a simple server
![[testing technology automated testing pytest] basic summary of pytest](/img/30/7c632cd6ad93c9294745dda7642f17.png)
[testing technology automated testing pytest] basic summary of pytest

Zhiniu stock -- 09

Constructors and prototypes
随机推荐
String functions and memory operation functions
Moment.js
arcgis根据矢量范围裁取tif影像(栅格数据)、批量合并shp文件、根据矢量范围裁取区域内的矢量,输出地理坐标系、转换16位TIF影像的像素深度至8位、shp文件创建和矢量框标绘设置
Array merge method: concat()
LeetCode 0136. 只出现一次的数字:异或
TS union type
What is the difference between'1 and'b1 when assigning values
Matchmaker's words
numeric学习之iota,accumulate
Same origin strategy and cross domain
开放API生态系统面临的十个威胁
[learning notes] solid works operation record
什么是奇偶校验?如何用C语言实现?
Deep and shallow copies
十大排序之快速排序
Scroll case: return to top with animation
【MUDUO】EventLoopThreadPool
redis-基本数据类型(String/list/Set/Hash/Zset)
1913. Maximum product difference between two number pairs - no sorting required
统计之歌 歌词