×

java串口通信

java串口通信(北斗双向通信什么意思)

admin admin 发表于2023-03-11 06:36:19 浏览39 评论0

抢沙发发表评论

本文目录

北斗双向通信什么意思

北斗双向通信是指北斗的短报文通讯功能。北斗的短报文通讯可以实现用户之间,用户与控制中心之间的双向短报文通信,而老美的GPS则是单向通讯。这也是我国北斗的巨大优势。

基于北斗的短报文双向通讯功能,在海 洋、沙漠等野外没有或通信网络很弱的地方,只要有北斗终端的用户,除了可以可以定位自己的位置,还可以对外发布信息。这个功能将支持我国北斗应用的尽快发展。

关注引石老王,为您解读安全与高科技,提高信息安全意识,保障自身隐私安全。

jsp,java串口通信的问题


可以,使用comm,jar
class SerialExample {
public static void main(String args) {
//TO DO: Add your JAVA codes here
long curTime = System.currentTimeMillis();
long serialtime = 8000;
boolean state = true;
SerialBean SB = new SerialBean(2);//设置端口号2
String Msg = “AD 01 0D“;//发送命令
SB.Initialize(9600);//设置波率
SB.WritePort(Msg);//发送命令
/* for (int i = 5; i 《 10; i++) {
System.out.println( SB.ReadPort(3));//设置读取个数
}
*/
String readdata = SB.ReadPort(“0D“,4000);//读取以OD结束的数据,4000ms没有数据就返回空
if (readdata.length() 》 0) { //System.out.println(readdata.length());//如果有读到数据
System.out.println(readdata);//如果有读到数据
}
while (readdata.length() 《 1 && state) {//如果没有读到数据
readdata = SB.ReadPort(“0D“,4000);
System.out.println(readdata);
if (System.currentTimeMillis() - curTime 》 serialtime) {
state = false;//设置读取错误超时
}
System.out.println(“readdaa:“ + state);
System.out.println(System.currentTimeMillis() - curTime);
}
if (!state) {
System.out.println(“数据读取超时“);
}
SB.ClosePort();//关闭连接
}
}
public class SerialBuffer {
Convents cv = new Convents();
private String Content = ““;
private String CurrentMsg, TempContent;
private boolean available = false;
private int LengthNeeded = 1;
String str = ““;
byte b;
/**
*
* This function returns a string with a certain length from the incoming
* messages.
*
* @param Length The length of the string to be returned.
*
*/
public synchronized String GetMsg(int Length) {
LengthNeeded = Length;
long timeout=2000;
long curtime=System.currentTimeMillis();
notifyAll();
if (LengthNeeded 》 Content.length()) {
available = false;
while (available == false) {
try {
if(System.currentTimeMillis()-curtime《timeout) wait();
} catch (InterruptedException e) {
}
}
}
CurrentMsg = Content.substring(0, LengthNeeded);
TempContent = Content.substring(LengthNeeded);
Content = TempContent;
LengthNeeded = 1;
notifyAll();
return CurrentMsg;
}
public synchronized String GetMsg(String endstring,long timeout) {
LengthNeeded =Content.indexOf(endstring);
notifyAll();
if (LengthNeeded 《 0) {
available = false;
while (available == false) {
try {
wait(timeout);
available=true;
} catch (InterruptedException e) {
}
}
return ““;
}
if (LengthNeeded 》 0) {
CurrentMsg = Content.substring(0, LengthNeeded+endstring.length());
TempContent = Content.substring(LengthNeeded+endstring.length());
Content = TempContent;
}

LengthNeeded = -1;
notifyAll();
return CurrentMsg;
}
public synchronized void PutChar(int c) {
Content = Content.concat(cv.byteToHexString(c));
if (LengthNeeded 《 Content.length() && Content.length() 》 0) {
available = true;
}
notifyAll();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package common.serial;
/**
*
* @author Jason
*/
import java.io.*;
import java.util.*;
import javax.comm.*;
import common.code.Convents;
public class SerialBean {
Convents cv=new Convents();
String PortName = ““;
CommPortIdentifier portId = null;
SerialPort serialPort = null;
OutputStream out;
InputStream in;
SerialBuffer SB;
ReadSerial RT;
int rate=9600;
String endstring =““;
long timeout=2000;
public SerialBean(int PortID) {
PortName = “COM“ + PortID;
}
public int Initialize(int rate) {

int InitSuccess = 1;
int InitFail = -1;
try {
portId = CommPortIdentifier.getPortIdentifier(PortName);
try {
serialPort = (SerialPort) portId.open(“Serial_Communication“, 2000);
} catch (PortInUseException e) {
return InitFail;
}
//Use InputStream in to read from the serial port, and OutputStream
//out to write to the serial port.
try {
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
} catch (IOException e) {
return InitFail;
}
//Initialize the communication parameters to 9600, 8, 1, none.
try {
serialPort.setSerialPortParams(rate,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
return InitFail;
}
} catch (NoSuchPortException e) {
return InitFail;
}
SB = new SerialBuffer();
RT = new ReadSerial(SB, in);
RT.start();
return InitSuccess;
}
public String ReadPort(int Length) {
String Msg;
Msg = SB.GetMsg(Length);
return Msg;
}
public String ReadPort(String endstring,long timeout) {
String Msg;
Msg = SB.GetMsg(endstring,timeout);
return Msg;
}
public void WritePort(String Msg) {
try {
out.write(cv.hexStringToByte(Msg));
} catch (IOException e) {
}
}
public void ClosePort() {
serialPort.close();
}
}
package common.serial;
import java.io.*;
public class ReadSerial extends Thread {
private SerialBuffer ComBuffer;
private InputStream ComPort;
char ch;
public ReadSerial(SerialBuffer SB, InputStream Port) {
ComBuffer = SB;
ComPort = Port;
}
@Override
public void run() {
int c;
try {
while (true) {
c=ComPort.read();
ComBuffer.PutChar(c);
}
} catch (IOException e) {
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package common.serial;
/**
*
* @author Administrator
*/
public class PortOpreate {
private String sendtxt=““;
private String recivetxt=““;
private int comid = 1;
private int comrate = 9600;
private int timeout = 4000;
private long waittime = 13000;
private String endtxt = “0D“;
private boolean pstate=false;
private String massage=““;
public void PortOpreate(boolean hasreturn) {
long curTime = System.currentTimeMillis();
long serialtime = getWaittime();
boolean state = true;
int t=0;
SerialBean SB = new SerialBean(getComid());//设置端口号2
t=SB.Initialize(getComrate());//设置波率
if(t》0){
SB.WritePort(getSendtxt());//发送命令
if (hasreturn) {
String readdata = SB.ReadPort(getEndtxt(), getTimeout());//读取以OD结束的数据,4000ms没有数据就返回空
if (readdata.length() 》 0) { //System.out.println(readdata.length());//如果有读到数据
System.out.println(readdata);//如果有读到数据
}
while (readdata.length() 《 1 && state) {//如果没有读到数据
readdata = SB.ReadPort(getEndtxt(), getTimeout());
System.out.println(readdata);
if (System.currentTimeMillis() - curTime 》 serialtime) {
state = false;//设置读取错误超时
}
System.out.println(“readdaa:“ + state);
System.out.println(System.currentTimeMillis() - curTime);
}
if (!state) {
System.out.println(“数据读取超时“);
setMassage(“数据读取超时“);
}
setRecivetxt(readdata);
setPstate(state);
}
SB.ClosePort();//关闭连接
}else{
System.out.println(“端口号出现错误“);
setMassage(“端口号出现错误“);
}
}
/**
* @return the sendtxt
*/
public String getSendtxt() {
return sendtxt;
}
/**
* @param sendtxt the sendtxt to set
*/
public void setSendtxt(String sendtxt) {
this.sendtxt = sendtxt;
}
/**
* @return the recivetxt
*/
public String getRecivetxt() {
return recivetxt;
}
/**
* @param recivetxt the recivetxt to set
*/
public void setRecivetxt(String recivetxt) {
this.recivetxt = recivetxt;
}
/**
* @return the comid
*/
public int getComid() {
return comid;
}
public void setComid(int comid) {
this.comid = comid;
}
public int getComrate() {
return comrate;
}
public void setComrate(int comrate) {
this.comrate = comrate;
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public long getWaittime() {
return waittime;
}
public void setWaittime(long waittime) {
this.waittime = waittime;
}
public String getEndtxt() {
return endtxt;
}
public void setEndtxt(String endtxt) {
this.endtxt = endtxt;
}
public boolean isPstate() {
return pstate;
}
public void setPstate(boolean pstate) {
this.pstate = pstate;
}
public String getMassage() {
return massage;
}
public void setMassage(String massage) {
this.massage = massage;
}
}
package common.serial;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class PortOperatorServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“text/html;charset=UTF-8“);
PrintWriter out = response.getWriter();
try {
long curTime = System.currentTimeMillis();
long serialtime = 8000;
boolean state = true;
String Msg = “AD 01 0D“;//发送命令
SerialBean SB = new SerialBean(10);//设置端口号2
SB.Initialize(9600);//设置波率
SB.WritePort(Msg);//发送命令
/* for (int i = 5; i 《 10; i++) {
System.out.println( SB.ReadPort(3));//设置读取个数
}
*/
String readdata = SB.ReadPort(“0D“,4000);//读取以OD结束的数据
if (readdata.length() 》 0) { //System.out.println(readdata.length());//如果有读到数据
System.out.println(readdata);//如果有读到数据
}
while (readdata.length() 《 1 && state) {//如果没有读到数据
readdata = SB.ReadPort(“0D“,4000);
System.out.println(readdata);
out.println(readdata);
if (System.currentTimeMillis() - curTime 》 serialtime) {
state = false;//设置读取错误超时
}
System.out.println(“readdaa:“ + state);
System.out.println(System.currentTimeMillis() - curTime);
}
if (!state) {
System.out.println(“数据读取超时“);
out.println(“数据读取超时“);
}
SB.ClosePort();//关闭连接
} finally {
out.close();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
public String getServletInfo() {
return “Short description“;
}
}
package common.code;
public final class Convents {
public final static char BToA = “0123456789abcdef“.toCharArray();
/**
* 把16进制字符串转换成字节数组A1 01 0D
* @param hex
* @return
*/
public byte hexStringToByte(String hex) {
String str = hex.split(“ “);
int len = str.length;
byte result = new byte[len];
char achar = hex.toCharArray();
for (int i = 0; i 《 len; i++) {
result[i] = (byte) (toByte(str[i].charAt(0)) * 16 + toByte(str[i].charAt(1)));
}
return result;
}
private static byte toByte(char c) {
byte b = (byte) (“0123456789ABCDEF“.indexOf(c));
return b;
}
/**
* 把字节数组转换成16进制字符串
* @param bArray
* @return
*/
public String byteToHexString(int b){
String st=““;
st=Integer.toHexString(b);
if (st.length() 《 2) {
st=“0“+Integer.toHexString(b).toUpperCase()+“ “;
} else {
st=Integer.toHexString(b).toUpperCase()+“ “;
}
return st;
}
public String bytesToHexString(byte bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i 《 bArray.length; i++) {
sTemp = Integer.toHexString(bArray[i]).toUpperCase();
}
return sb.toString();
}
}

Java串口通信发送十六进制的问题


对于输入输出来说,就只有二进制,没有十六进制的说法。
你还是说说,这个程序到底是要几个二进制位。
---------------
你看看这样行不行。
试试下面这两种方法。
1.输出4个字节
int value = Integer.parseInt(“41“, 16);
out.write(value);
2.输出一个字节
byte value = Byte.parseByte(“41“, 16);
out.write(value);

java串口通信中怎样以十六进制数发送


做串口通讯的时候基本都是通过io流读取、输出。那么在java开发中发送数据的时候使用OutputStream,而其write()的参数是字节数组、int整形。如果使用字节数组发送的时候,通常可以直接写成out.write(“1234“.getBytes())。这样一来单片机读到的数据则是31 32 33 34。但是在串口发送的时候先把16进制字符串转化为byte数组在发送出来,则是发送什么读取到的就是什么。使用:out.write(HexString2Bytes(“1234“));那么读取到的还是1234。16进制字符串转化为byte数组的方法为:
public static byte HexString2Bytes(String src) {
if (null == src || 0 == src.length()) {
return null;
}
byte ret = new byte[src.length() / 2];
byte tmp = src.getBytes();
for (int i = 0; i 《 (tmp.length / 2); i++) {
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
}
return ret;
}

在用java做串口开发的时候建议使用开源的Rxtx做。效率、使用方法都要优。使用sun开源的comm个人觉得不是很便利。rxtx网上有开源实例。可以根据自己个需求进行加以修饰利用。

java串口通信数据丢失,怎么解决


解决办法:
1、将两台PC间波特率设置为一样的大小。
2、采取进距离传输,随着距离的增加,信息衰减率也便增加。
3、尽量采用低波特率传输,这样误码率会大大减少。
4、加入数据校验功能,在接收数据一方实现“奇偶校验法”等方法验证数据传输的完整性。