62,636
社区成员




package send;
public class SerialBuffer
{
public String Content = "";
private String CurrentMsg;
private boolean available = false;
private int LengthNeeded = 2;
public boolean isTimeOut=false;
public synchronized String getMsg(long timeOut)//获得所读取的前两个字符
{
while (Content.equals("")||Content==null)
{
try
{
wait(timeOut);
}catch(InterruptedException e){
e.printStackTrace();
}
if(Content.equals("")||Content==null)
{
isTimeOut=true;
Content="aa";
}
else
{
break;
}
}
CurrentMsg = Content.substring(0, LengthNeeded);
Content = Content.substring(LengthNeeded, Content.length());
notifyAll();
return CurrentMsg;
}
public synchronized void putChar(int c)//得到串口读过来的数据存到字符变量中
{
String t="";
t=Integer.toHexString(c);
if(t.length()<2)
{
t="0"+t;
}
Content = Content.concat(t);
if (LengthNeeded < Content.length())
{
available = true;
}
notifyAll();
}
public synchronized void cleanBuff()
{
Content="";
notifyAll();
}public synchronized boolean getIsTimeOut()
{
notifyAll();
return isTimeOut;
}
}
package send;
import java.io.*;
import javax.comm.*;
public class SerialBean
{
private String mess="";//从串口读过来的数据
private static String PortName;
private CommPortIdentifier portId;
private SerialPort serialPort;
private static OutputStream out;
private static InputStream in;
private String Content = "";
private String CurrentMsg;
private boolean available = false;
private int LengthNeeded = 2;
private ReadSerial rs;
private SerialBuffer sb;
public SerialBean(int PortID,int BaudRate)
{
Initialize(PortID,BaudRate);
}
public int Initialize(int PortID,int BaudRate)
{
PortName = "COM" + PortID;
int InitSuccess = 1;
int InitFail = -1;
try
{
portId = CommPortIdentifier.getPortIdentifier(PortName);
try
{
serialPort = (SerialPort)
portId.open("Serial_Communication", 2000);
} catch (PortInUseException e)
{
return InitFail;
}
try
{
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
} catch (IOException e)
{
return InitFail;
}
try
{
serialPort.setSerialPortParams(BaudRate,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e)
{
return InitFail;
}
} catch (NoSuchPortException e){
return InitFail;
}
sb=new SerialBuffer();
rs=new ReadSerial(sb,in);
rs.start() ;
return InitSuccess;
}
public void wPort(String str)
{
try
{
out.write(StrToHex(str));
}catch(IOException e){e.printStackTrace();}
}
public String rPort(long timeOut)
{
mess=sb.getMsg(timeOut);
return mess;
}
public void clPort()
{
sb.cleanBuff();
}
public boolean isTimeOut()
{
return sb.getIsTimeOut();
}
public static byte[] StrToHex(String hexstr) //十六进制字符串转化为字节数组
{
byte[] b = new byte[hexstr.length() / 2];
int j = 0;
for (int i = 0; i < b.length; i++) {
char c0 = hexstr.charAt(j++);
char c1 = hexstr.charAt(j++);
b[i] = (byte) ((parse(c0) << 4) | parse(c1));
}
return b;
}
private static int parse(char c)
{
if (c >= 'a')
return (c - 'a' + 10) & 0x0f;
if (c >= 'A')
return (c - 'A' + 10) & 0x0f;
return (c - '0') & 0x0f;
}
}
package send;
import java.io.*;
public class ReadSerial extends Thread//读取串口数据的线程
{
private SerialBuffer sb;
private InputStream in;
public ReadSerial(SerialBuffer sb, InputStream in)
{
this.sb = sb;
this.in =in;
}
public void run()
{
int c;
try
{
while(true)
{
c = in.read();
sb.putChar(c);
}
} catch (IOException e) {System.out.println("------------block----------");}
}
}