电路设计,软件开发,专业项目团队承接电子产品开发!


ISPRO下载型编程器
火爆销售中

RS232/TTL转换器
全IC串口窃电

RS232/TTL转换线
全IC串口窃电

ByteBlasterMV Cable
CPLD/FPGA并口下载线

ByteBlaster II Cable ISPRO下载型编程器
支持S51/AVR/PIC
超级51开发板套件
前卫实用超值

PC/MCU/CPLD
三合一学习套件

全新万年历方案
AVR单片机开发板
 

Min51单片机仿真机
迷你型

ARM7开发板
仅售260元!
 

RS-232/RS-485双向接口电平转换器  

51单片机升级STM32
火爆销售中
MPLAB-ICD2
火爆销售中
LED51开发板
玩具又名图文显示棒

面板模块/开发板
经济危机明智决择

       

GSM MODEM GSM猫

SMS短信模块 工业级/全自动
火爆销售中

大功率数传电台

高速数传电台

您当前位置:电子技术资料 -> 外围驱动 -> 网口 ->

祝您阅读愉快!

ISPRO下载型编程器

RS232 TO TTL转换器
↑↑本文相关产品介绍↑↑

pocsag协议在vc下面的编码函数


类别:技术文章
来源:未知
作者:未知
关键字:协议
加入日期:2002-9-15

pocsag协议在vc下面的编码函数

在vc下面的编码函数。:)
没有经过优化的。
//pocsag.cpp
#include "stdafx.h"
#include "pocsag.h"

const unsigned char sync_code[4] = {(char)0x3e,(char)0x4b,(char)0xa8,(char)0x1b};
const unsigned char empty_code[4] = {(char)0x5e,(char)0x91,(char)0x83,(char)0xe9};
const unsigned char prefix_code = (char)0xaa;
const unsigned char prefix_num = (char)72;

//encode message/address to pocsag code
//inputs:
// msg: char [5],source data,including address and message,the lowest 4 bits including data
// flag: char,input mode,
// 8th bit of flag:message(1) or address(0)
// 1st and 2nd bits of flag:function bits
// pocsag:char [4],out put of pocsag code
void en_pocsag(char *msg,char flag,char *pocsag)
{
char i;
unsigned char x;
short y;
// clear the dest bits first
for(i = 0;i < 4;i++)
{
pocsag[i] = 0;
}

if(flag & 0x80)
//message mode
{
pocsag[0] |= 1;
//store the message bits into pocsag stream
//first byte
x = msg[0] & 0x0f;
x <<= 1;
pocsag[0] |= x;
//second byte
x = msg[1] & 0x07;
x <<= 5;
pocsag[0] |= x;
x = msg[1] & 0x08;
x >>= 3;
pocsag[1] |= x;
//third byte
x = msg[2] & 0x0f;
x <<= 1;
pocsag[1] |= x;
//fourth byte
x = msg[3] & 0x07;
x <<= 5;
pocsag[1] |= x;
x = msg[3] & 0x08;
x >>= 3;
pocsag[2] |= x;
//fifth byte
x = msg[4] & 0x0f;
x <<= 1;
pocsag[2] |= x;
}
else
//address mode
{
//store address in pocsag stream
//first byte
x = msg[0] & 0x0f;
x <<= 1;
pocsag[0] |= x;
//second byte
x = msg[1] & 0x07;
x <<= 5;
pocsag[0] |= x;
x = msg[1] & 0x08;
x >>= 3;
pocsag[1] |= x;
//third byte
x = msg[2] & 0x0f;
x <<= 1;
pocsag[1] |= x;
//fourth byte
x = msg[3] & 0x07;
x <<= 5;
pocsag[1] |= x;
x = msg[3] & 0x08;
x >>= 3;
pocsag[2] |= x;
//fifth byte
x = msg[4] & 0x03;
x <<= 1;
pocsag[2] |= x;
//func byte
x = flag & 0x03;
x <<= 3;
pocsag[2] |= x;
}
//caculate crc bits
y = calc_crc(pocsag);
//merge all crc bits to pocsag stream
//pocsag byte 2
x = y & 0x07;
x <<= 5;
pocsag[2] |= x;
//pocsag byte 3
x = y & 0xf8;
x >>= 3;
pocsag[3] |= x;

x = (y & 0xff00) >> 8;
x <<= 5;
pocsag[3] |= x;
}
//calculating pocsag crc
//input:pocsag first 21 bits in char string
//output:pocsag crc in short lower 11 bits
short calc_crc(char *src)
{
short gen_word = 0x4b7;

char i = 0,j = 0,k = 0;
short x = 0,y = 0;

y = src[2];
x |= src[1];
x &= 0xff;
x <<= 8;
x |= src[0];
//caculate odd crc
while(j != 21)
{
x ^= gen_word;
for(i = 0;;)
{
if((x & (short)0x01) == 0)
{
x >>= 1;
i++;
j++;
if(y & 0x01)
{
x |= 0x8000;
}
else
{
x &= 0x7fff;
}
y >>= 1;

if(j == 21)break;
}
else
{
break;
}
}
}

//caculate the even bit
y = x;
j = 0;
//count how many bits in odd crc
for(i = 0;i < 10;i++)
{
if(y & 0x01)
{
j++;
}
y >>= 1;
}
for(k = 0;k < 3;k++)
{
y = src[k];
y &= 0xff;
for(i = 0;i < 8;i++)
{
if(y & 0x01)
{
j++;
}
y >>= 1;
}
}
//set the even crc bit
if(j & 1)
{
x |= 0x400;
}
else
{
x &= 0x3ff;
}
return x;
}

int getnum(char x)
{
if(x >= '0' && x <= '9')
{
return x & 0x0f;
}
if(x >= 'a' && x <= 'f')
{
return x - 'a' + 10;
}
if(x >= 'A' && x <= 'F')
{
return x - 'A' + 10;
}
return 0;
}

char ASCII2Pocsag(unsigned char x)
{
switch(x)
{
case '!': return 0x10;
case '?': return 0x11;
case ',': return 0x12;
case ':': return 0x13;
case '.': return 0x14;
case '+': return 0x15;
case '-': return 0x16;
case '$': return 0x18;
case '%': return 0x1a;
default:
break;
}
if(x >= '0' && x <= '9')
return (x - '0') + 0x1b;
if(x >= 'A' && x <= 'Z')
return (x - 'A') + 0x25;
if(x >= 'a' && x <= 'z')
return (x - 'a') + 0x25;
return x & 0x7f;
}

int Enc7Bitstr(char *src,char *dest,int len)
{
unsigned char x = 0;
unsigned char y = 0;
int i = 0,j = 0,k = 0;
int l = 0;
bool b;
bool quit = 0;
if(len <= 0)return 0;

x = src[0];
y = 0;
while(1)
{
for(i = 0; i < 8; i++)
{
b = x & 1;
x >>= 1;
j++;
if(j == 7)
{
j = 0;
k++;
if(k >= len)
{
quit = 1;
x = 0;
}
else x = src[k];
}
y |= b ? 0x80 : 0x00;
if(i != 7)
y >>= 1;
}
dest[l] = y;
y = 0;
l++;
if(quit)return l;
}
}
//pocsag.h
#ifndef _POCSAG_H
#define _POCSAG_H

#define CODE_SI 1
#define CODE_SO 2

short calc_crc(char *src);
void en_pocsag(char *msg,char flag,char *pocsag);

extern const unsigned char sync_code[4];
extern const unsigned char empty_code[4];
extern const unsigned char prefix_code;
extern const unsigned char prefix_num;

int getnum(char x);
char ASCII2Pocsag(unsigned char x);
int Enc7Bitstr(char *src,char *dest,int len);

#endif

您可能还会关注的相关技术资料:

中国电子网
www.EC66.com
QQ:34740080 EMAIL:iloveaoe@163.com
最佳浏览:1024*768
Copyright (C) 2001-2010 EC66.COM All Rights Reserved
Aoe/帝国 制作维护
粤ICP备18061901号