- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码给我带来了很多麻烦,我想我盯着它看的时间太长了,希望有新的眼睛-
调用方法-(在RobotInterface类中)
try
{
serialConnection.put(candidate.getCandidate().getGenome());
TwoWaySerialCommTest.inputAvailable.await();
}
catch ( Exception e )
{
TwoWaySerialCommTest.listPorts();
e.printStackTrace();
}
TwoWaySerialCommTest 类 - 改编自 rxtx 网站示例。完整代码供引用,重要部分是put()方法和serialwriter。
package org.dnsdojo.ryanhost.GA.MuPlusOne;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.locks.*;
/**
* This version of the TwoWaySerialComm example makes use of the
* SerialPortEventListener to avoid polling.
*
*/
public class TwoWaySerialCommTest
{
static Lock lock = new ReentrantLock();
static Condition outputAvailable = lock.newCondition();
static Condition inputAvailable = lock.newCondition();
public volatile byte[] inputBuffer = new byte[1024];
public volatile byte[] outputBuffer = new byte[1024];
public TwoWaySerialCommTest()
{
super();
}
public void put(byte[] buffer)
{
try
{
lock.lock();
int i = 0;
for(; i < buffer.length; i++ )
{
outputBuffer[i] = buffer[i];
}
System.out.println((byte) '\n');
outputBuffer[i] = (byte) '\n';
i++;
for(; i < outputBuffer.length; i++ )
{
outputBuffer[i] = 0;
}
System.out.println("Output buffer after put");
for(int j = 0; j < outputBuffer.length; j++)
{
System.out.print(outputBuffer[j]);
}
}
catch(Exception e)
{
System.out.println("Exception for lock in put");
e.printStackTrace();
}
finally
{
lock.unlock();
outputAvailable.signal();
}
}
void connect ( String portName ) throws Exception
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if ( portIdentifier.isCurrentlyOwned() )
{
System.out.println("Error: Port is currently in use");
}
else
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
if ( commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();
serialPort.addEventListener(new SerialReader(in , inputBuffer));
serialPort.notifyOnDataAvailable(true);
(new Thread(new SerialWriter(out , outputBuffer))).start();
}
else
{
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
static void listPorts()
{
java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
while(portEnum.hasMoreElements())
{
CommPortIdentifier portIdentifier = portEnum.nextElement();
if(portIdentifier == null)
{
System.out.println("No ports");
}
System.out.println("Available - " + portIdentifier.getName());
}
}
/**
* Handles the input coming from the serial port. A new line character
* is treated as the end of a block in this example.
*/
public static class SerialReader implements SerialPortEventListener
{
private InputStream in;
byte[] buffer;
public SerialReader ( InputStream in )
{
this.in = in;
}
public SerialReader (InputStream in, byte[] buffer)
{
this.in = in;
this.buffer = buffer;
}
public void serialEvent(SerialPortEvent arg0) {
lock.lock();
int data;
try
{
int len = 0;
while ( ( data = in.read()) > -1 )
{
if ( data == '\n' )
{
break;
}
buffer[len++] = (byte) data;
}
//inputAvailable.signal();
//outputAvailable.signal();
}
catch ( IOException e )
{
e.printStackTrace();
System.exit(-1);
}
finally
{
lock.unlock();
}
}
}
/** */
public static class SerialWriter implements Runnable
{
OutputStream out;
volatile byte[] buffer;
public SerialWriter ( OutputStream out )
{
this.out = out;
}
public SerialWriter ( OutputStream out, byte[] buffer)
{
this.out = out;
this.buffer = buffer;
}
public void run ()
{
while(true)
{
lock.lock();
try
{
outputAvailable.await();
System.out.println("Waking up");
int i = 0;
if (this.buffer != null)
{
System.out.println("Buffer isn't empty");
for (int j = 0; j < buffer.length; j++)
{
System.out.print(buffer[i]);
}
while(buffer[i] != ((byte)'\n') && i < buffer.length -1 )
{
this.out.write(buffer[i++]);
System.out.print(buffer[i-1]);
buffer[i-1] = 0;
}
}
else
{
System.out.println("Buffer is null");
System.out.println(this.buffer.toString());
}
}
catch ( IOException e )
{
e.printStackTrace();
System.exit(-1);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
lock.unlock();
}
}
}
}
/* public static void main ( String[] args )
{
try
{
(new TwoWaySerialCommTest()).connect("/dev/ttyS82");
}
catch ( Exception e )
{
TwoWaySerialCommTest.listPorts();
e.printStackTrace();
}
}
*/
}
输出如下所示。我试过四处移动锁文件,从串行编写器中取出锁,在调用方法和其他一些技巧中加锁。
期望的结果是该方法将调用serialConnection(已连接的TwoWaySerialCommTest的实例),并等待serialConnection写入串口并收到响应,然后再继续执行。
几个小时以来我一直在努力解决这个问题,请帮忙:)
Output buffer after put
10101010000001101111010110101111010000100110101000000011101011100011000101111111010011101111001100111110001010011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000RXTX fhs_lock() Error: creating lock file: /var/lock/LCK..ttyS82: File exists
java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.signal(AbstractQueuedSynchronizer.java:1941)
at org.dnsdojo.ryanhost.GA.MuPlusOne.TwoWaySerialCommTest.put(TwoWaySerialCommTest.java:64)
at org.dnsdojo.ryanhost.GA.MuPlusOne.RobotInterface.evaluate(RobotInterface.java:35)
at org.dnsdojo.ryanhost.GA.MuPlusOne.RobotInterface.run(RobotInterface.java:58)
at java.lang.Thread.run(Thread.java:722)
如果我按照建议将通知移动到 try block 内,则输出为
Output buffer after put
11100111000111010010111111000110100010010111000000110001010011001001110001010111110001010110010001010001010110101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000Waking up
Buffer isn't empty
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111RXTX fhs_lock() Error: creating lock file: /var/lock/LCK..ttyS82: File exists
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110011100011101001011111100011010001001011100000011000101001100100111000101011111000101011001000101000101011010java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:155)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1260)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.fullyRelease(AbstractQueuedSynchronizer.java:1723)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2040)
at org.dnsdojo.ryanhost.GA.MuPlusOne.RobotInterface.evaluate(RobotInterface.java:36)
at org.dnsdojo.ryanhost.GA.MuPlusOne.RobotInterface.run(RobotInterface.java:58)
at java.lang.Thread.run(Thread.java:722)
RobotInterface 类 -
package org.dnsdojo.ryanhost.GA.MuPlusOne;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class RobotInterface implements Runnable
{
// create a serial connection
// transmit a string and check for response
// wait for evaluation
// take evaluation
private CandidateTest candidate;
private TwoWaySerialCommTest serialConnection;
//private Random rng = new Random();
protected static Logger logger = Logger.getLogger("Thread" + Thread.currentThread().getName());
public RobotInterface(CandidateTest test, TwoWaySerialCommTest serialConnection)
{
this.candidate = test;
this.serialConnection = serialConnection;
PropertyConfigurator.configure("log4j.properties");
}
public void evaluate (Genome genome)
{
//send to robot and return fitness
//genome.setFitness(rng.nextDouble());
logger.debug("fitness is " + genome.getFitness());
try
{
try
{
serialConnection.put(candidate.getCandidate().getGenome());
TwoWaySerialCommTest.inputAvailable.await();
}
catch ( Exception e )
{
TwoWaySerialCommTest.listPorts();
e.printStackTrace();
}
}
catch(Exception E)
{
}
}
public void run()
{
logger.debug("entering run of Robot Interface");
logger.debug("Send Genome via serial and wait for a response");
Genome testSubject = candidate.getCandidate();
evaluate(testSubject);
candidate.finished();
}
最佳答案
你在释放锁后调用signal
而you should call it WHILE holding the lock :
lock.unlock();
outputAvailable.signal();
应该是相反的:
outputAvailable.signal();
lock.unlock();
尽管在 try
block 的末尾调用 signal
会更好。
关于java - 重入锁 - 尝试使用 rxtx 将字节 [] 写入串行端口时出现非法监控状态异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15769220/
我想知道两者都可以 UnicastRemoteObject.exportObject(Remote,portNo) & LocateRegistry.createRegistry(portNo); p
我有一个运行 tomcat 8.0.23 和 apache httpd 服务器的 vps。在 tomcat 中我有 3 个项目让我们用下面的名字来调用它们: /firstpro /secondpro
我试图将非 SSL 端口 8080 上的流量重定向到 SSL 端口 8443(在 Jboss 4.2.3.GA 版本上),但它不起作用。当我在此端口上访问我的 web 应用程序时,它会停留在该端口上并
跟进: Possible to query the native inbox of a mobile from J2ME? 我怎么知道Kannel将 SMS 发送到 native 收件箱端口(我想是端
我想用 python 开发一个代码,它将在本地主机中打开一个端口并将日志发送到该端口。日志只是 python 文件的命令输出。 喜欢: hello.py i = 0 while True:
我的 tomcat 在我的 linux 机器上独立运行在端口 7778 上。我已经将 apache 配置为在端口 443 上的 ssl 上运行。 我的 httpd.conf 如下: Liste
我正在使用 React Native 作为我想要部署到 iOS 和 Android 的头像生成器。我正在为我的服务器使用 Express,它使用 localhost:3000,react native
我正在使用主板(MSI Gaming主板)和两个支持NIC卡的e1000e驱动程序构建自定义操作系统。我想使用NIC卡中的端口而不是板载端口。 为了禁用板载端口,我尝试使用 70-persistanc
我目前使用的是xampp 1.7.0,我的php版本是5.2.8 我将我的 php.ini 文件更改为: [mail function] ; For Win32 only. SMTP = smtp.g
我有以下代码来配置 Jetty 服务器: @Configuration public class RedirectHttpToHttpsOnJetty2Config { @Bean p
我使用 Ubuntu 使用 Certbot 生成了一个 SSL。这已经自动更新了我的 Nginx 配置文件并添加了一个额外的监听端口。我担心我是否只需要监听一个端口(80 或 443)而不是两个端口,
我被困在一个点,我必须调用 pentaho API 来验证来自 reactJS 应用程序的用户。两者都在我的本地机器上。到目前为止我尝试过的事情: a) 在 reactJS 配置文件 - packag
我的 native 项目在 android 模拟器上运行 但是每当我尝试将我的项目与我的 android 设备连接时,就会出现上述错误。 注意:我的手机和笔记本电脑使用相同的 wifi 连接。 请帮我
我正在运行 Elasticsearch 服务器。除了 9200/9300 端口外,Elasticsearch 还开放了很多端口,如下所示。 elasticsearch-service-x64.exe
<portType> 元素是最重要的 WSDL 元素 <portType>可描述一个 web service、可被执行的操作,以及相关的消息 我们可以把 <portT
Stack Overflow 的其他地方有一个关于让 Icecast 出现在端口 80 上的问题,我已经阅读了该问题,但仍然无法让我的服务器在端口 80 上工作。 我的 icecast.xml 有这些
如果这是一个简单的问题,我很抱歉,我对这种事情不熟悉。 我正在尝试确定我的代理服务器 ip 和端口号,以便使用 google 日历同步程序。我使用谷歌浏览器下载了 proxy.pac 文件。最后一行是
我想知道 cassnadra 是否对所有 JMX 连接/节点间通信使用 7199 端口?与早期版本一样,7199 仅用于初始握手,但后来它使用随机选择 1024-65355 端口之间的任何端口。 谢谢
在docker hub中,有一个容器,该容器在启动时会默认打开9000端口。可以通过传递环境变量server__port来覆盖该端口。 我正在尝试在dockerfile中传递Heroku $ PORT
我已经在互联网上公开的虚拟机中安装了 docker。我已经在 VM 的 docker 容器中安装了 mongodb。Mongodb 正在监听 27017港口。 我已经使用以下步骤安装 docker
我是一名优秀的程序员,十分优秀!