📄 serialconnection.java
字号:
try {
this.m_RecBuffer = null;
this.m_RecBuffer = new byte[CollectorDefine.MAX_BYTE];
newData = m_InputStream.read(this.m_RecBuffer);
if (newData == -1) {
CollectorDefine.SystemPrintln("rec no byte ");
this.m_RecNum = 0;
this.m_RecBuffer = null;
break;
}
if ('\r' == (char) newData) {
CollectorDefine.SystemPrintln("rec r");
// inputBuffer.append('\n');
}
else {
CollectorDefine.SystemPrintln("rec byte ");
String m_str = CFunction.translateBytesToString(1,
this.m_RecBuffer, newData);
//CollectorDefine.SystemPrintln ("str : " + m_str + "len :" + newData);
this.m_RecNum = newData;
}
}
catch (IOException ex) {
System.err.println(ex);
return;
}
}
// Append received data to messageAreaIn.
/* messageAreaIn.append(new String(inputBuffer));
**/
break;
// If break event append BREAK RECEIVED message.
case SerialPortEvent.BI:
/*messageAreaIn.append("\n--- BREAK RECEIVED ---\n");
*/
break;
}
return;
}
/**
* Handles ownership events. If a PORT_OWNERSHIP_REQUESTED event m_InputStream
* received a dialog box m_InputStream created asking the user if they are
* willing to give up the port. No action m_InputStream taken on other types
* of ownership events.
*/
public void ownershipChange(int type) {
if (type == CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED) {
// PortRequestedDialog prd = new PortRequestedDialog(parent);
;
}
return;
}
/**
* A class to handle <code>KeyEvent</code>s generated by the messageAreaOut.
* When a <code>KeyEvent</code> occurs the <code>char</code> that m_InputStream
* generated by the event m_InputStream read, converted to an <code>int</code> and
* writen to the <code>OutputStream</code> for the port.
*/
/* class KeyHandler extends KeyAdapter {
OutputStream m_OutputStream;
public KeyHandler(OutputStream m_OutputStream) {
super();
this.m_OutputStream = m_OutputStream;
}
public void keyTyped(KeyEvent evt) {
char newCharacter = evt.getKeyChar();
try {
m_OutputStream.write((int)newCharacter);
} catch (IOException e) {
System.err.println("OutputStream write error: " + e);
}
}
}
*/
public int sendBuffer(byte[] m_SendBuffer, int m_Length) {
try {
this.m_OutputStream.write(m_SendBuffer, 0, m_Length);
}
catch (IOException m_IoException) {
CFunction.writeLog("sendBuffer In SerialConnection Error #1 " +
"port no:" + m_SerialParameters.getPortName(),
m_IoException);
return -1;
// m_IoException.printStackTrace();
}
return 1;
}
public int sendBuffer(String m_String) {
try {
OutputStream outputStream = null;
if (this.m_OutputStream == null) {
//CollectorDefine.SystemPrintln (" ############################ this.m_OutputStream == null ");
return -1;
}
outputStream = new ConvertedOutputStream(this.m_OutputStream);
outputStream.write(m_String.getBytes());
outputStream = null;
}
catch (IOException m_IoException) {
CFunction.writeLog("sendBuffer(string) In SerialConnection Error #1 " +
"port no:" + m_SerialParameters.getPortName(),
m_IoException);
return -1;
// m_IoException.printStackTrace();
}
return 1;
}
public int sendString(String m_String) {
try {
this.m_OutputStream.write(m_String.getBytes());
}
catch (IOException m_IoException) {
CFunction.writeLog("sendString(string) In SerialConnection Error #1 " +
"port no:" + m_SerialParameters.getPortName(),
m_IoException);
return -1;
// m_IoException.printStackTrace();
}
return 1;
}
public String recStringBuffer(byte[] RecBuffer, boolean m_Lowercase,
int m_TimeCounter) {
/* int m_Count =0;
int m_SuccessFlag = 1;
String m_receiveString = "";
while (m_SuccessFlag ==1)
{
if (this.m_RecNum >0)
{
String m_String = new String(this.m_RecBuffer,0,this.m_RecNum);
if (m_Lowercase == true)
m_receiveString = m_String.toLowerCase();
else
m_receiveString = m_String;
this.m_RecBuffer = null;
this.m_RecNum = 0 ;
}
try
{
Thread.sleep(1000);
}
catch(InterruptedException interruptedexception)
{
CFunction.putHintString(1,"recStringBuffer in SerialConnection Error #1 " + "端口号:" + this.m_PortNo);
return "";
}
m_Count ++;
if (m_Count > m_TimeCounter)
m_SuccessFlag = -1;
}
return m_receiveString;
*/
int m_Count = 0;
int m_SuccessFlag = 1;
String m_receiveString = "";
try {
int m_RecAvailableNum = 0;
int bytes = 0;
while (m_SuccessFlag == 1) {
m_RecAvailableNum = m_InputStream.available();
if (m_RecAvailableNum > 0 &&
m_RecAvailableNum <= CollectorDefine.MAX_BYTE) {
//CollectorDefine.SystemPrintln (" rec recStringBuffer");
// byte [] m_TempBuffer = new byte[CollectorDefine.MAX_BYTE];
bytes = m_InputStream.read(RecBuffer);
String m_String = new String(RecBuffer, 0, bytes);
if (m_Lowercase == true) {
m_receiveString = m_String.toLowerCase();
}
else {
m_receiveString = m_String;
}
break;
}
try {
Thread.sleep(CollectorDefine.COMM_SLEEP_TIME);
}
catch (InterruptedException interruptedexception) {
CFunction.writeLog("recStringBuffer in SerialConnection Error #1 " +
"port no:" + m_SerialParameters.getPortName(),
interruptedexception);
return "";
}
m_Count++;
if (m_Count > m_TimeCounter) {
m_SuccessFlag = -1;
}
}
}
catch (IOException m_IoException) {
CFunction.writeLog("recStringBuffer in SerialConnection Error #2 " +
"port no:" + m_SerialParameters.getPortName(),
m_IoException);
return "";
}
return m_receiveString;
}
public int recBuffer(byte[] RecBuffer, int m_TimeCounter) {
/*
int m_Num = 0 ;
int m_Count =0;
while (true)
{
if (this.m_RecNum >0)
{
System.arraycopy(this.m_RecBuffer,0,RecBuffer , 0 ,this.m_RecNum);
this.m_RecBuffer = null;
m_Num = this.m_RecNum;
this.m_RecNum = 0 ;
}
try
{
Thread.sleep(1000);
}
catch(InterruptedException m_InterruptedException)
{
CFunction.putHintString(1,"recBuffer1 in SerialConnection Error #1 " + "端口号:" + this.m_PortNo);
return -1;
}
m_Count ++;
if (m_Count > m_TimeCounter)
break;
}
return m_Num;
*/
int m_Count = 0;
int m_RecNum = -1;
try {
int m_RecAvailableNum = 0;
int bytes = 0;
while (true) {
m_RecAvailableNum = m_InputStream.available();
if (m_RecAvailableNum > 0 &&
m_RecAvailableNum <= CollectorDefine.MAX_BYTE) {
bytes = m_InputStream.read(RecBuffer);
m_RecNum = bytes;
break;
}
try {
Thread.sleep(CollectorDefine.COMM_SLEEP_TIME);
}
catch (InterruptedException m_InterruptedException) {
CFunction.writeLog("recBuffer1 in SerialConnection Error #1 " +
"port no:" + m_SerialParameters.getPortName(),
m_InterruptedException);
return -1;
}
m_Count++;
if (m_Count > m_TimeCounter) {
break;
}
}
}
catch (IOException m_IoException) {
CFunction.writeLog("recBuffer1 in SerialConnection Error #2 " +
"port no:" + m_SerialParameters.getPortName(),
m_IoException);
return -1;
}
return m_RecNum;
}
public int recBuffer(byte[] m_RecBuffer) {
int m_Count = 0;
int m_RecNum = -1;
int m_TimeCounter = 1;
try {
int m_RecAvailableNum = 0;
int bytes = 0;
while (true) {
m_RecAvailableNum = m_InputStream.available();
if (m_RecAvailableNum > 0 &&
m_RecAvailableNum <= CollectorDefine.MAX_BYTE) {
bytes = m_InputStream.read(m_RecBuffer);
m_RecNum = bytes;
break;
}
try {
Thread.sleep(CollectorDefine.COMM_SLEEP_TIME);
}
catch (InterruptedException interruptedexception) {
CFunction.writeLog("recBuffer2 in SerialConnection Error #1 " +
"port no:" + m_SerialParameters.getPortName(),
interruptedexception);
return -1;
}
m_Count++;
if (m_Count > m_TimeCounter) {
break;
}
}
}
catch (IOException m_IoException) {
CFunction.writeLog("recBuffer2 in SerialConnection Error #2 " +
"port no:" + m_SerialParameters.getPortName(),
m_IoException);
return -1;
}
return m_RecNum;
}
public int flushOutputStream() {
try {
if (m_Open == true) {
this.m_OutputStream.flush();
}
else {
CFunction.writeLog("flushOutputStream in SerialConnection Error #1 " +
"port no:" + m_SerialParameters.getPortName(), null);
}
}
catch (IOException m_IoException) {
CFunction.writeLog("flushOutputStream in SerialConnection Error #2 " +
"port no:" + m_SerialParameters.getPortName(),
m_IoException);
return -1;
}
return 1;
}
public int initConnection(long t) {
return 1;
}
public boolean isAlive() {
return true;
}
public int resetInputStream() {
try {
if (m_Open == true) {
this.m_InputStream.reset();
}
else {
CFunction.writeLog("resetInputStream in SerialConnection Error #1 " +
"port no:" + m_SerialParameters.getPortName(), null);
}
}
catch (IOException m_IoException) {
CFunction.writeLog("resetInputStream in SerialConnection Error #2 " +
"port no:" + m_SerialParameters.getPortName(),
m_IoException);
return -1;
}
return 1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -