私は降伏する。ブロックされているものを見つけるために数週間試してみましたが、 シリアルデータが私のコードのグラフィック部分によって更新されるのを受けました。 初めてのJavaプログラミング。約15年間のプログラミング経験があります マイクロと私は自分自身の問題を解決するために使用されていますが、これは、そのタクティクスが生産的な のポイントを超えています。 私のアプリケーションは2つのファイルで構成されています。グラフィックの更新
1つのファイルがRXTXプロジェクトに由来し、毎秒二回、いくつかのパケット に送信されたシリアルデータをキャッチ。 これはチャームのように動作します(時間がかかりました)。キャプチャされたデータが正しいことがわかります と安定しています。
他のファイルは、グラフィックで、エンドユーザが 読み取りと時々の値を書き込むことができる約80のメニューから構成されています。ナビゲートは、これまでの ボタンとスクロールバーのマウスイベントで行われます。 この部分も正常に動作します。値は読み込み、変更、保存することができます。
私が立ち往生している部分は、シリアルファイルから更新された値がグラフィック画面に更新されません。 このサイトから数多くのサンプルとチュートリアル(このサイトの多くの )を実行しようとしました。
オブジェクト関連の言語の概念は私には新しく、まだかなり混乱しています。 私の問題には継承とクラスが関係していることは間違いありません。スレッドは別の候補です... を実行し、私の問題を提示し、誰かが何が間違って見ることができる願って、最小サイズにコードをカットしました。
package components;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.SwingUtilities;
public class SerialComm extends ScreenBuilder implements java.util.EventListener {
InputStream in;
public SerialComm() {
super();
}
public interface SerialPortEventListener
extends java.util.EventListener {
}
void connect(String portName) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM1");
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Error: Port is currently in use");
} else {
CommPortIdentifier.getPortIdentifier("COM1");
System.out.println("" + portName);
CommPort commPort = portIdentifier.open("COM1", 2000);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_2, SerialPort.PARITY_NONE);
InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();
serialPort.addEventListener(new SerialComm.SerialReader(in));
serialPort.notifyOnDataAvailable(true);
(new Thread(new SerialComm.SerialReader(in))).start();
// TX functionality commented for now
// (new Thread(new SerialWriter(out))).start();
} else {
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
public class SerialReader extends SerialComm implements Runnable,
gnu.io.SerialPortEventListener {
public SerialReader(InputStream in) {
this.in = in;
}
@Override
public void run() {
count=11; // just for test. run is normally empty
count2=count; // and real code runs within serialEvent()
System.out.println("SerialReader " + count);
dspUpdate(); // do some desperate stuff in graphics file
System.out.println("Post Update " + count);
}
@Override
public void serialEvent(SerialPortEvent event) {
System.out.println("SerialEvent");
switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
try {
synchronized (in) {
while (in.available() < 0) {
in.wait(1, 800000);
} //in real code RX data is captured here twice a sec
} //and stored into buffers defined in ScreenBuilder
//dspUpdate() is called from here to make ScreenBuilder update its screen
//That never happens despite all my attempts
} catch (IOException e) {
System.out.println("IO Exception");
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
}
}
}
/* "main" connect PC serial port and start graphic part of application
* To demonstrate problem with no serial data stream present
* order of init between serial port and graphics are switched
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ScreenBuilder screen = new ScreenBuilder();
screen.createAndShowGUI();
System.out.println("Created GUI");
}
});
try {
(new SerialComm()).connect("COM1");
} catch (Exception e) {
System.out.println("Error");
e.printStackTrace();
}
}
}
とグラフィックあなたはGUIクラスにすべてを入れているに実行している最大の問題
package components;
import java.awt.*;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.*;
public class ScreenBuilder extends JPanel implements ActionListener {
public Font smallFont = new Font("Dialog", Font.PLAIN, 12);
Color screenColor;
Color lineColor;
short btn=0;
short count;
short count2;
Button helpButton;
public static void createAndShowGUI() {
System.out.println("Created GUI on EDT? "
+ SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame("JUST A TEST");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new ScreenBuilder());
f.pack();
f.setVisible(true);
}
public void dspButton() {
setLayout(null);//
helpButton = new Button("?");
helpButton.setLocation(217, 8); // set X, Y
helpButton.setSize(16, 14); //Set Size X, Y //
helpButton.addActionListener(this);
add(helpButton);
setBackground(Color.black);
helpButton.setBackground(Color.black);
screenColor = Color.black;
helpButton.setForeground(Color.white);
lineColor = Color.white;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == helpButton) {
count2++;
System.out.println("Pressed Button ");
repaint();
}
}
public ScreenBuilder() {
setBorder(BorderFactory.createLineBorder(Color.black));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(240, 180);
}
public void dspUpdate() {
/*
* This function is called from SerialComm
* Should be called when serial packets have arrived (twice a second)
* and update screen with values from serial stream
* For now just a test var to validate that values from SerialComm
* get to here (they do)
*/
count++;
System.out.println("Update Count " + count);
System.out.println("Update Count2 " + count2);
// revalidate(); // another futile attempt to update screen
// repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(lineColor);
g.setFont(smallFont);
count++;
g.drawString("" + count, 130, 20);
g.drawString("" + count2, 150, 20);
if (btn == 0) {
dspButton();
btn = 1;
}
}
}
はアップ手はありません、私はスイングに精通しているようではないんだけど、あなたはメソッド呼び出しの間の関係を説明することができ – mKorbel
を放棄しませんか?私が得られないことを説明するのはちょっと難しいです:まず、 'SerialComm'は' dspUpdate() 'を呼び出します。このメソッドは 'repaint'(良いと思います)を呼び出すでしょう、再描画は' dspUpdate'を呼び出す 'paintComponent'を呼び出しますか? – phineas
@phineas S /彼はスイング中に並行性の問題があり、GUIにそこにすべての更新がハイテク、内部PaintComponentsからdspUpdate()()の呼び出しが私の側から必死で別のテストだった – mKorbel