当前位置:网站首页>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 ~
边栏推荐
- S4/HANA MM & SD EDI基于NAST的集成配置(ORDERS, ORDRSP, DESADV, INVOIC)
- 【代码案例】博客页面设计(附完整源码)
- [nodejs] nodejs create a simple server
- utility实用组件学习之swap,move,forward,exchange
- Topsis与熵权法
- 反射之类加载过程
- Payment terms in SAP message No. vg202 IDoc e1edk18 have been transferred: check data
- ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用
- LeetCode 0136. 只出现一次的数字:异或
- Interview focus - TCP protocol of transport layer
猜你喜欢

抽丝剥茧C语言(高阶)程序环境和预处理

Docker installation redis-5.0.12 (remote access)
![[Muduo] EventLoop event cycle](/img/80/824c7061d58796d454be0c438e257c.png)
[Muduo] EventLoop event cycle

utility实用组件学习之swap,move,forward,exchange

生成随机数random学习之uniform_int_distribution,uniform_real_distribution

Good news under the epidemic

Graph traversal DFS, BFS (code explanation)

下一代终端安全管理的关键特征与应用趋势

反射之类加载过程

LeetCode 0135. 分发糖果
随机推荐
Moment.js
LeetCode 0136. 只出现一次的数字:异或
Macro task, micro task and event cycle mechanism
JS synchronization and asynchrony
Wrote a little webapi knowledge points from 0 to 1
S4/HANA MM & SD EDI基于NAST的集成配置(ORDERS, ORDRSP, DESADV, INVOIC)
[Muduo] EventLoop event cycle
什么是奇偶校验?如何用C语言实现?
TS interface
WebMvcConfigurationSupport
图的遍历-DFS,BFS(代码详解)
utility实用组件学习之swap,move,forward,exchange
SAP Message No. VG202 IDoc E1EDK18 中付款条款已经转移:检查数据
Ratio of learning_ add,ratio_ subtract,ratio_ multiply,ratio_ Use of divide
Release of v6.5.1/2/3 series of versions of Xingyun housekeeper: the ability of database OpenAPI continues to be strengthened
【MUDUO】EventLoop事件循环
2022 Niuke multi School Game 2
Canada EE channel
Programmer interview Golden Classic 4.12 summation path
疫情之下的好消息