基于Java开发北斗RDSS的CCTXA【通讯申请】和BDTXR【通讯信息】的报文发送和接收java开发代码段
1)发送CCTXA【通讯申请】
2)接收到BDFKI【反馈信息】和BDTXR【通讯信息】
发送CCTXA后,会接收到BDFKI反馈信息,如果反馈信息为正确,则对方可能会接收到BDTXR【通讯信息】(也可能存在丢包,非100%准确,和北斗终端的位置和灵敏度有关);如果BDFKI反馈信息表示为错误,则对方不会接收到BDTXR
(1)发送CCTXA【通讯申请】
public class TXASender implements Runnable
{
MySerialPort sp_;
int destCardNum_; //目标用户的卡号(此次通信的收信方地址)
int txType_; //通讯类别:0-特快通信;1-普通通信
int transType_; //传输方式:0-汉字;1-代码;2-混合传输。
String data_="313131"; //通信电文内容
int interval_;
String txaCmdStr = "CCTXA";
private Logger log = Logger.getLogger(this.getClass());
。。。。。。
public TXASender(MySerialPort sp, int destCardNum, int txType, int transType, String data, int interval)
{
sp_ = sp;
destCardNum_ = destCardNum;
txType_ = txType;
transType_ = transType;
data_ = data;
interval_ = interval;
}
public void run()
{
int counter = 0;
String sendData = "";
while(true)
{
counter++;
sendData = (data_+"_No"+counter);
String header = "$";
String preMsg = txaCmdStr + "," + destCardNum_ + "," + txType_ + "," + transType_ + "," + ((2==transType_)?"A4":"") + strTo16(sendData);
//log.debug("preMsg=["+preMsg+"]");
//log.debug("getXORCheck=["+getXORCheck(preMsg.getBytes())+"]");
String msg = header + preMsg + "*" + getXORCheck(preMsg.getBytes()) +"\r\n";
log.debug("destCardNum_=["+destCardNum_+"]");
log.debug("txType_=["+txType_+"]");
log.debug("transType_=["+transType_+"]");
log.debug("data=["+sendData+"]");
try
{
sp_.send(msg.getBytes());
log.debug("msg=["+msg+"]");
log.debug("msg=["+strTo16(msg)+"]");
System.out.println((new StringBuilder(String.valueOf((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date())))).append("").toString());
System.out.println("CCTXA(通讯申请),counter="+counter);
System.out.println("用户地址(ID 号)="+destCardNum_);
System.out.println("通信类别="+txType_);
switch(txType_)
{
case 1:
System.out.println("\t\t普通通信");
break;
case 0:
System.out.println("\t\t特快通信");
break;
}
System.out.println("传输方式="+transType_);
switch(transType_)
{
case 0:
System.out.println("\t\t汉字");
break;
case 1:
System.out.println("\t\t代码");
break;
case 2:
System.out.println("\t\t混合传输");
break;
}
System.out.println("通信电文内容=["+sendData+"]");
System.out.println("\n");
Thread.sleep(1000L*interval_);
}
catch(Exception e)
{
e.printStackTrace();
break;
}
}
}
}
(2)接收BDFKI【反馈信息】和BDTXR【通讯信息】
public class TXReceiver implements Runnable
{
MySerialPort sp_;
int BDFKI_counter=0; //反馈信息BDFKI计数器
int BDTXR_counter=0; //通讯信息BDTXR计数器
private Logger log = Logger.getLogger(this.getClass());
public TXReceiver(MySerialPort sp)
{
sp_ = sp;
}
。。。。。。
public void run()
{
byte recvDataBytes[];
int recvDataLen = 0;
while(true)
{
recvDataBytes = new byte[1024];
recvDataLen = 0;
try
{
//while(sp_.is.available() > 0)
if(sp_.is.available() > 0)
{
recvDataLen = sp_.read(recvDataBytes); //从串口接收数据
recvDataBytes[recvDataLen]=0;
//输出接收到的所有信息
log.debug("SerialPort recvDataLen=["+recvDataLen+"]");
log.debug("SerialPort recvDataBytes=\n"+new String(recvDataBytes, 0, recvDataLen));
}
//未接收到串口数据
if(0 == recvDataLen){
Thread.sleep(1000L);
continue;
}
boolean flag = false;
int charIndex = 0;
//拷贝数据,分析用
byte recvDataTmp[] = new byte[recvDataLen];
System.arraycopy(recvDataBytes, 0, recvDataTmp, 0, recvDataLen);
if(recvDataLen > 5)
{
for(; charIndex < recvDataLen - 6; charIndex++)
{
//临时存储5个字符,用于判断各种指令
byte tmp[] = new byte[5];
System.arraycopy(recvDataTmp, charIndex, tmp, 0, 5);
//BDFKI指令(反馈信息)
if(tmp[0] == 0x42 && tmp[1] == 0x44 && tmp[2] == 0x46 && tmp[3] == 0x4b && tmp[4] == 0x49)
{
BDFKI_counter++;
//获取指令后的数据
String leftDataStr = new String(recvDataTmp, charIndex, recvDataTmp.length-charIndex);
//log.debug("tmpStr1 ="+leftDataStr);
//log.debug("tmpStr1 ="+strTo16(leftDataStr));
//查找\r\n子串
int indexCRNL = leftDataStr.indexOf("\r\n");
if(indexCRNL>=0){
//找到了\r\n子串,进行剔除
leftDataStr = leftDataStr.substring(0, indexCRNL);
}
//log.debug("tmpStr2 ="+leftDataStr);
//log.debug("tmpStr2 ="+strTo16(leftDataStr));
//以,进行拆分子串
String[] strArr = leftDataStr.split(",");
//System.out.println(strArr.length);
for (int i = 0; i < strArr.length; ++i){
//System.out.println(strArr[i]);
}
//打印输出BDFKI的信息
if(true)
{
System.out.println((new StringBuilder(String.valueOf((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date())))).append("").toString());
System.out.println("BDFKI(反馈信息),counter="+BDFKI_counter);
System.out.println("\t"+leftDataStr);
System.out.println("\t指令名称="+strArr[1]);
System.out.println("\t指令执行情况="+strArr[2]);
System.out.println("\t指令执行情况="+((strArr[2].equals("Y"))?"指令执行成功":"指令执行失败"));
System.out.println("\t频度设置指示="+strArr[3]);
System.out.println("\t频度设置指示="+((strArr[3].equals("Y"))?"频度设置正确":"频度设置错误"));
System.out.println("\t发射抑制指示="+strArr[4]);
System.out.print("\t发射抑制指示=");
switch(Integer.valueOf(strArr[4]))
{
case 0:
System.out.println("发射抑制解除");
break;
case 1:
System.out.println("接收到系统的抑制指令,发射被抑制");
break;
case 2:
System.out.println("电量不足,发射被抑制");
break;
case 3:
System.out.println("设置为无线电静默,发射被抑制");
break;
}
int index_ = strArr[5].indexOf("*");
if(index_>=0)
strArr[5] = strArr[5].substring(0, index_);
System.out.println("\t等待时间="+Integer.valueOf(strArr[5])+"秒");
System.out.println("\n");
}
}
//BDTXR指令(通信信息)
else if(tmp[0] == 0x42 && tmp[1] == 0x44 && tmp[2] == 0x54 && tmp[3] == 0x58 && tmp[4] == 0x52)
{
BDTXR_counter++;
String leftDataStr = new String(recvDataTmp, charIndex, recvDataTmp.length-charIndex);
//log.debug("tmpStr1 ="+leftDataStr);
//log.debug("tmpStr1 ="+strTo16(leftDataStr));
int indexCRNL = leftDataStr.indexOf("\r\n");
if(indexCRNL>=0)
leftDataStr = leftDataStr.substring(0, indexCRNL);
log.debug("tmpStr2 ="+leftDataStr);
log.debug("tmpStr2 ="+strTo16(leftDataStr));
String[] strArr = leftDataStr.split(",");
//System.out.println(strArr.length);
for (int i = 0; i < strArr.length; ++i){
//System.out.println(strArr[i]);
}
//打印输出BDTXR的信息
if(true)
{
System.out.println((new StringBuilder(String.valueOf((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date())))).append("").toString());
System.out.println("BDTXR(通讯信息),counter="+BDTXR_counter);
System.out.println("\t"+leftDataStr);
System.out.println("\t信息类别="+strArr[1]);
switch(Integer.valueOf(strArr[1]))
{
case 1:
System.out.println("\t\t普通通信");
break;
case 2:
System.out.println("\t\t特快通信");
break;
case 3:
System.out.println("\t\t通播通信(可以判断是下属用户)");
break;
case 4:
System.out.println("\t\t按最新存入电文查询获得的通信");
break;
case 5:
System.out.println("\t\t按发信方地址查询获得的通信");
break;
}
System.out.println("\t发信方地址="+strArr[2]);
System.out.println("\t电文形式="+strArr[3]);
switch(Integer.valueOf(strArr[3]))
{
case 0:
System.out.println("\t\t汉字");
break;
case 1:
System.out.println("\t\t代码");
break;
case 2:
System.out.println("\t\t混合传输");
break;
}
System.out.println("\t发信时间="+strArr[4]);
int index_ = strArr[5].indexOf("*");
if(index_>=0)
strArr[5] = strArr[5].substring(0, index_);
System.out.println("\t通信电文内容1="+strArr[5]);
if(2==Integer.valueOf(strArr[3]))
strArr[5] = strArr[5].substring(2,strArr[5].length());
System.out.println("\t通信电文内容2="+strArr[5]);
System.out.println("\t通信电文内容3="+hexStringToString(strArr[5]));
System.out.println("\n");
}
}
else{
continue;
}
}
}
}
catch(IOException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
break;
}
}
}
}