大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Modbus两种协议的编程方法:
我们提供的服务有:成都网站设计、网站制作、微信公众号开发、网站优化、网站认证、荥阳ssl等。为近1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的荥阳网站制作公司
1、LRC校验
LRC域是一个包含一个8位二进制值的字节。LRC值由传输设备来计算并放到消息帧中,接收设备在接收消息的过程中计算LRC,并将它和接收到消息中LRC域中的值比较,如果两值不等,说明有错误。
LRC校验比较简单,它在ASCII协议中使用,检测了消息域中除开始的冒号及结束的回车换行号外的内容。它仅仅是把每一个需要传输的数据按字节叠加后取反加1即可。下面是它对应的代码:
BYTE GetCheckCode(const char * pSendBuf, int nEnd)//获得校验码
{
BYTE byLrc = 0;
char pBuf[4];
int nData = 0;
for(i=1; iend; i+=2) //i初始为1,避开“开始标记”冒号
{
//每两个需要发送的ASCII码转化为一个十六进制数
pBuf [0] = pSendBuf [i];
pBuf [1] = pSendBuf [i+1];
pBuf [2] = '\0';
sscanf(pBuf,"%x", nData);
byLrc += nData;
}
byLrc = ~ byLrc;
byLrc ++;
return byLrc;
}
2、CRC校验
CRC域是两个字节,包含一16位的二进制值。它由传输设备计算后加入到消息中。接收设备重新计算收到消息的CRC,并与接收到的CRC域中的值比较,如果两值不同,则有误。
CRC是先调入一值是全“1”的16位寄存器,然后调用一过程将消息中连续的8位字节各当前寄存器中的值进行处理。仅每个字符中的8Bit数据对CRC有效,起始位和停止位以及奇偶校验位均无效。
CRC产生过程中,每个8位字符都单独和寄存器内容相或(OR),结果向最低有效位方向移动,最高有效位以0填充。LSB被提取出来检测,如果LSB为1,寄存器单独和预置的值或一下,如果LSB为0,则不进行。整个过程要重复8次。在最后
lrc可以通过如下util工具类进行转换,如果想知道结果是否读取的有问题,可以直接用记事本打开lrc文件的,之后和输出结果比对一下就行。
package com.routon.utils;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.util.Log;
/**
* parse lrc file tool
* eg:
* utilLrc lrc = new utilLrc("/sdcard/test.lrc");
* get song name : String title = lrc.getTitle();
* get performer name : String artist = lrc.getArtist();
* get album name: String album = lrc.getAlbum();
* get lrcmaker name: String lrcMaker = lrc.getLrcMaker();
* get song list: ListStatement list = lrc.getLrcList();
*
* @author xuweilin
*
*/
public class utilLrc {
private static String TAG = "utilLrc";
public class Statement {
private double time = 0.0; //time, 0.01s
private String lyric = ""; //song word
/*
* get time
*/
public double getTime() {
return time;
}
/*
* set time
*/
public void setTime(double time) {
this.time = time;
}
/*
* set time.format:mm:ss.ms
*/
public void setTime(String time) {
String str[] = time.split(":|\\.");
this.time = Integer.parseInt(str[0])*60+Integer.parseInt(str[1])+Integer.parseInt(str[2])*0.01;
}
/*
* get lrc word
*/
public String getLyric() {
return lyric;
}
/*
* set lrc word
*/
public void setLyric(String lyric) {
this.lyric = lyric;
}
}
private BufferedReader bufferReader = null;
private String title = "";
private String artist = "";
private String album = "";
private String lrcMaker = "";
private ListStatement statements = new ArrayListStatement();
/*
*
* fileName
*/
public utilLrc(String fileName) throws IOException{
FileInputStream file = new FileInputStream(fileName);
bufferReader = new BufferedReader(new InputStreamReader(file, "utf-8"));
readData();
}
/*
* read the file
*/
private void readData() throws IOException{
statements.clear();
String strLine;
while(null != (strLine = bufferReader.readLine()))
{
if("".equals(strLine.trim()))
{
continue;
}
if(null == title || "".equals(title.trim()))
{
Pattern pattern = Pattern.compile("\\[ti:(.+?)\\]");
Matcher matcher = pattern.matcher(strLine);
if(matcher.find())
{
title=matcher.group(1);
continue;
}
}
if(null == artist || "".equals(artist.trim()))
{
Pattern pattern = Pattern.compile("\\[ar:(.+?)\\]");
Matcher matcher = pattern.matcher(strLine);
if(matcher.find())
{
artist=matcher.group(1);
continue;
}
}
if(null == album || "".equals(album.trim()))
{
Pattern pattern = Pattern.compile("\\[al:(.+?)\\]");
Matcher matcher = pattern.matcher(strLine);
if(matcher.find())
{
album=matcher.group(1);
continue;
}
}
if(null == lrcMaker || "".equals(lrcMaker.trim()))
{
Pattern pattern = Pattern.compile("\\[by:(.+?)\\]");
Matcher matcher = pattern.matcher(strLine);
if(matcher.find())
{
lrcMaker=matcher.group(1);
continue;
}
}
int timeNum=0;
String str[] = strLine.split("\\]");
for(int i=0; istr.length; ++i)
{
String str2[] = str[i].split("\\[");
str[i] = str2[str2.length-1];
if(isTime(str[i])){
++timeNum;
}
}
for(int i=0; itimeNum;++i)
{
Statement sm = new Statement();
sm.setTime(str[i]);
if(timeNumstr.length)
{
sm.setLyric(str[str.length-1]);
}
statements.add(sm);
}
}
sortLyric();
}
/*
* judge the string is or not date format.
*/
private boolean isTime(String string)
{
String str[] = string.split(":|\\.");
if(3!=str.length)
{
return false;
}
try{
for(int i=0;istr.length;++i)
{
Integer.parseInt(str[i]);
}
}
catch(NumberFormatException e)
{
Log.e(TAG, "isTime exception:"+e.getMessage());
return false;
}
return true;
}
/*
* sort the word by time.
*/
private void sortLyric()
{
for(int i=0;istatements.size()-1;++i)
{
int index=i;
double delta=Double.MAX_VALUE;
boolean moveFlag = false;
for(int j=i+1;jstatements.size();++j)
{
double sub;
if(0=(sub=statements.get(i).getTime()-statements.get(j).getTime()))
{
continue;
}
moveFlag=true;
if(subdelta)
{
delta=sub;
index=j+1;
}
}
if(moveFlag)
{
statements.add(index, statements.get(i));
statements.remove(i);
--i;
}
}
}
/**
* get title
* @return
*/
public String getTitle(){
return title;
}
/**
* get artist
* @return
*/
public String getArtist(){
return artist;
}
/**
* get album
* @return
*/
public String getAlbum(){
return album;
}
/**
* get lrc maker
* @return
*/
public String getLrcMaker(){
return lrcMaker;
}
/**
* get song list
* @return
*/
public ListStatement getLrcList(){
return statements;
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Lyric2Text {
public void doWork(String lyric) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(lyric), "utf8"));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(lyric.substring(0, lyric.lastIndexOf('.')) + ".txt"), "utf8"));
String line;
while((line = reader.readLine()) != null){
line = line.replaceAll("\\[[0-9:\\.]+\\]", "");
writer.write(line + "\n");
}
reader.close();
writer.close();
}
public static void main(String[] args) throws IOException {
Lyric2Text tmp = new Lyric2Text();
tmp.doWork("C:\\Users\\admin\\Desktop\\一帘幽梦.lrc");
}
}
byte[] code = { 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1 };
int sum = 0;
foreach (byte b in code)
{
sum += b;
}
sum =sum % 255;//模FF
sum = ~sum + 1;//取反+1
string lrc = Convert.ToString(sum, 16);
sum最后的结果是-5,转成16进制的字符串是fffffffc,因为sum是用int表示的,是32位的。