#include <stdio.h>
#define SYNC 0xAA
#define EXCODE 0x55
int parsePayload( unsigned char payload, unsigned char pLength ) {
unsigned char bytesParsed = 0;
unsigned char code;
unsigned char length;
unsigned char extendedCodeLevel;
int i;
/ Loop until all bytes are parsed from the payload[] array… /
while( bytesParsed < pLength ) {
/ Parse the extendedCodeLevel, code, and length /
extendedCodeLevel = 0;
while( payload[bytesParsed] == EXCODE ) {
extendedCodeLevel++;
bytesParsed++;
}
code = payload[bytesParsed++];
if( code & 0x80 ) length = payload[bytesParsed++];
else length = 1;
/ TODO: Based on the extendedCodeLevel, code, length,• and the [CODE] Definitions Table, handle the next
• “length” bytes of data from the payload as
• appropriate for your application.
/
printf( “EXCODE level: %d CODE: 0x%02X length: %d\n”,
extendedCodeLevel, code, length );
printf( “Data value(s):” );
for( i=0; i<length; i++ ) {
printf( " %02X", payload[bytesParsed+i] & 0xFF );
}
printf( “\n” );
/ Increment the bytesParsed by the length of the Data Value */
bytesParsed += length;
}
return( 0 );
}
int main( int argc, char *argv ) {
int checksum;
unsigned char payload[256];
unsigned char pLength;
unsigned char c;
unsigned char i;
/ TODO: Initialize ‘stream’ here to read from a serial data• stream, or whatever stream source is appropriate for your
• application. See documentation for “Serial I/O” for your
• platform for details.
*/
FILE stream = 0;
stream = fopen( “COM4”, “r” );
/ Loop forever, parsing one Packet per loop… /
while( 1 ) {
/ Synchronize on [SYNC] bytes /
fread( &c, 1, 1, stream );
if( c != SYNC ) continue;
fread( &c, 1, 1, stream );
if( c != SYNC ) continue;
/ Parse [PLENGTH] byte /
while( true ) {
fread( &pLength, 1, 1, stream );
if( pLength ~= 170 ) break;
}
if( pLength > 169 ) continue;
/ Collect [PAYLOAD…] bytes /
fread( payload, 1, pLength, stream );
/ Calculate [PAYLOAD…] checksum /
checksum = 0;
for( i=0; i<pLength; i++ ) checksum += payload[i];
checksum &= 0xFF;
checksum = ~checksum & 0xFF;
/ Parse [CKSUM] byte /
fread( &c, 1, 1, stream );
/ Verify [CKSUM] byte against calculated [PAYLOAD…] checksum /
if( c != checksum ) continue;
/ Since [CKSUM] is OK, parse the Data Payload */
parsePayload( payload, pLength );
}
return( 0 );
}
写自定义目录标题)
c语言转java在线转换 c语言转化为java语言
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
初识C语言
简述初识c语言的心得
初识c语言 -
c语言转java转换 c语言转换语句
转移语句:goto语句,break语句和continue 语句Ⅰ :转移语句介绍 C语言中的转移语句包括goto 语句,break 语句和continue语句,当程序有时出现某种情况,需要提早结束或转移执行其他指定语句时,就需要使用转移语句,转移语句可以使程序的流程按照这三种转移语句
c语言转java转换 转移语句 goto语句 break语句 continue语句