Modbus兩種協(xié)議的編程方法:
我們提供的服務(wù)有:成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、滎陽(yáng)ssl等。為近1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的滎陽(yáng)網(wǎng)站制作公司
1、LRC校驗(yàn)
LRC域是一個(gè)包含一個(gè)8位二進(jìn)制值的字節(jié)。LRC值由傳輸設(shè)備來計(jì)算并放到消息幀中,接收設(shè)備在接收消息的過程中計(jì)算LRC,并將它和接收到消息中LRC域中的值比較,如果兩值不等,說明有錯(cuò)誤。
LRC校驗(yàn)比較簡(jiǎn)單,它在ASCII協(xié)議中使用,檢測(cè)了消息域中除開始的冒號(hào)及結(jié)束的回車換行號(hào)外的內(nèi)容。它僅僅是把每一個(gè)需要傳輸?shù)臄?shù)據(jù)按字節(jié)疊加后取反加1即可。下面是它對(duì)應(yīng)的代碼:
BYTE GetCheckCode(const char * pSendBuf, int nEnd)//獲得校驗(yàn)碼
{
BYTE byLrc = 0;
char pBuf[4];
int nData = 0;
for(i=1; iend; i+=2) //i初始為1,避開“開始標(biāo)記”冒號(hào)
{
//每?jī)蓚€(gè)需要發(fā)送的ASCII碼轉(zhuǎn)化為一個(gè)十六進(jìn)制數(shù)
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校驗(yàn)
CRC域是兩個(gè)字節(jié),包含一16位的二進(jìn)制值。它由傳輸設(shè)備計(jì)算后加入到消息中。接收設(shè)備重新計(jì)算收到消息的CRC,并與接收到的CRC域中的值比較,如果兩值不同,則有誤。
CRC是先調(diào)入一值是全“1”的16位寄存器,然后調(diào)用一過程將消息中連續(xù)的8位字節(jié)各當(dāng)前寄存器中的值進(jìn)行處理。僅每個(gè)字符中的8Bit數(shù)據(jù)對(duì)CRC有效,起始位和停止位以及奇偶校驗(yàn)位均無效。
CRC產(chǎn)生過程中,每個(gè)8位字符都單獨(dú)和寄存器內(nèi)容相或(OR),結(jié)果向最低有效位方向移動(dòng),最高有效位以0填充。LSB被提取出來檢測(cè),如果LSB為1,寄存器單獨(dú)和預(yù)置的值或一下,如果LSB為0,則不進(jìn)行。整個(gè)過程要重復(fù)8次。在最后
lrc可以通過如下util工具類進(jìn)行轉(zhuǎn)換,如果想知道結(jié)果是否讀取的有問題,可以直接用記事本打開lrc文件的,之后和輸出結(jié)果比對(duì)一下就行。
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\\一簾幽夢(mèng).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最后的結(jié)果是-5,轉(zhuǎn)成16進(jìn)制的字符串是fffffffc,因?yàn)閟um是用int表示的,是32位的。