做串口发送和接受数据是遇到了问题,希望各位前辈帮忙
代码片断
public void serialEvent(SerialPortEvent event)//实现串口监听中的方法
{
switch(event.getEventType())
{
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer=new byte[1];
System.out.println("为什么只执行一遍?");
try
{
while(inputStream.available()!=-1)
{
int numBytes=inputStream.read(readBuffer);
area.append(new String(readBuffer));
}
System.out.println("为什么不执行这一句呢?");
}
catch(Exception e){}
break;
}
}
代码片断
public void serialEvent(SerialPortEvent event)//实现串口监听中的方法
{
switch(event.getEventType())
{
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer=new byte[1];
System.out.println("为什么只执行一遍?");
try
{
while(inputStream.available()!=-1)
{
int numBytes=inputStream.read(readBuffer);
area.append(new String(readBuffer));
}
System.out.println("为什么不执行这一句呢?");
}
catch(Exception e){}
break;
}
}
解决方案 »
- 问一个有序集合的问题
- 在服务器端有个文件正在下载,现在想把他删除,可是报错文件正在使用.应该怎删除
- 搞不懂JAVA的版本,明白的帮我解答一下吧(新手提问)
- 迷你DVD,要求使用对象数组做,怎么用对象数组做啊 ???
- 为什么我安装的JDK显现中文的地方都是方块(在WIN2K)
- 大X,给个SERVLET的数据POOL的例子。急呀
- JAVA如何复制文件啊?
- 如何用java或是其他工具做成图象文件(.gif 或是 .jpg)
- 请问大家,什么地方有java的免费下载的地方,一定要能下载呀
- 哪里有最新的JDK 下载?送分20!!
- 急问。o(∩_∩)o..关于JAVA的一些学习。大家来说说。
- Erroe:no 'server' JVM at 'F:\jre1.5.0\bin\server\jvm.dll
while(inputStream.available()!=-1)//开始不停的接受设备上发过来的数据
while循环就是完了它也不执行后面的代码
public void readData()
{
readBuffer = new byte[50];
try
{
while (in.available() > 0)
{
in.read(readBuffer); }
if (!enter)
{
deleteControlChar(readBuffer);
enter = false;
}
}
catch (IOException e)
{
e.printStackTrace();
}
} /**
* @param readBuffer
* @return void
*/
private void deleteControlChar(byte[] readBuffer)
{
String buff = new String(readBuffer);
//boolean tab = false;
Calendar cal = Calendar.getInstance();
String time = cal.get(Calendar.HOUR_OF_DAY) + ":"
+ cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND); for (int i = 0; i < buff.length(); i++)
{
/*System.out.println("letter: " + buff.charAt(i) + " -> "
+ (int) buff.charAt(i));
System.out.print("tab: ");
System.out.println(tab == true);
System.out.print("enter: ");
System.out.println(enter == true);*/
if ((int) buff.charAt(i) == 9 && !enter)
{
tab = true;
} if (tab && (int) buff.charAt(i) != 0 && (int) buff.charAt(i) != 9)
{
if ((int) buff.charAt(i) == 13)
{
tab = false;
enter = true;
rev = strBuff.toString();
String revCS = owner.getCheckSum(rev.substring(0, rev
.length() - 1));
if (!rev.equals("000X123456789"))
{
if (revCS.equals(rev))
{
received++;
//System.out.println("received=" + received);
System.out.println(time + " received: " + rev); setReceived();
checkType(rev);
}
else
{
System.out.println(time + " received: " + rev);
System.out.println("but received should be: "
+ revCS);
//send also
/*received++;
setReceived();
checkType(rev);*/
}
}
else
{
System.out.println(time + " received: " + rev);
} strBuff.delete(0, strBuff.length());
break;
}
strBuff.append(buff.charAt(i));
}
}
}