2012-10-10 27 views
6

अद्यतन करना मैं आत्मसमर्पण करता हूं। धारावाहिक डेटा को मेरे कोड के ग्राफ़िक भाग द्वारा अपडेट होने से प्राप्त करने के लिए अब कुछ हफ्तों की कोशिश कर रहा है। जावा में पहली बार प्रोग्रामिंग। लगभग 15 वर्षों का अनुभव प्रोग्रामिंग माइक्रोसॉफ्ट है और मुझे अपनी समस्याओं को हल करने के लिए उपयोग किया जाता है लेकिन यह उस बिंदु से परे है जहां है कि रणनीति उत्पादक है। मेरे आवेदन में दो फाइलें हैं।ग्राफिक्स

एक फ़ाइल 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(); 
    } 
    } 
} 

और ग्राफिक्स फाइल

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; 
    } 
    } 
} 
+4

कभी हाथ नहीं लेते, कभी भी – mKorbel

+0

आत्मसमर्पण नहीं करते हैं, मैं स्विंग से परिचित नहीं हूं, लेकिन क्या आप विधि कॉल के बीच संबंधों को समझा सकते हैं? यह समझाने में थोड़ा मुश्किल है कि मुझे क्या नहीं मिलता है: पहली जगह 'सीरियलकॉम' कॉल 'dspUpdate() '। यह विधि 'repaint' (अच्छा मुझे लगता है),' पेंटकंपोनेंट 'कॉल को दोबारा कॉल करेगी जो 'dspUpdate' कहलाती है? – phineas

+0

@ फीनस/उसे स्विंग में कॉन्सुरेंसी के साथ समस्या है, जीयूआई के सभी अपडेट ईडीटी, – mKorbel

उत्तर

2

सबसे बड़ी समस्या आप में जीयूआई वर्गों में सब कुछ डाल रहा है चला रहे हैं। अपने सामने वाले अंत (सुंदर जीयूआई सामान) से अपने मॉडल (बैकएंड सीरियल संचार सामग्री) को अलग करने का प्रयास करें, और आप अपने आप को बहुत सी सिरदर्द बचाएंगे। उदाहरण में मैंने आपके लिए ऐसा करने की कोशिश की है - यह एक फ़ाइल में है, लेकिन आपको इसे 3 में अलग करना चाहिए: मॉडल, व्यू, और कंट्रोल (नियंत्रण मॉडल और दृश्य के बीच संचार करता है)।

यदि आप नमूना धागे के बजाय मॉडल में अपना सीरियल संचार कोड (जिसे आपने कहा था) जोड़ते हैं, तो आप दृश्य और मॉडल के बीच बहुत अधिक परेशानी के बिना संवाद करने में सक्षम होना चाहिए। मैंने जितना संभव हो उतना कोड सुरक्षित रखने की कोशिश की।

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class TranslucentWindow { 

    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        View screen = new View(); 
        System.out.println("Created GUI"); 
        Model model = new Model(); 

        Control c = new Control(screen, model); 
       } catch (Exception e) { 
        System.out.println("Error"); 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    //Only cares about the backend. Simplified because you said all the backend code was working right. 
    public static class Model{ 

     //Data that was updated - you can change this to whatever you want. 
     public String count; 
     //Listener that notifies anyone interested that data changed 
     public ActionListener refreshListener; 

     public void run() { 
      //As a sample, we're updating info every 1/2 sec. But you'd have your Serial Listener stuff here 
      Thread t = new Thread(new Runnable(){ 
       @Override 
       public void run() { 
        int i = 0; 
        while(true){ 
         dspUpdate(i++); 
         try { 
          Thread.sleep(500); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
        } 
       }}); 
      t.start(); 
     } 

     //Update data and notify your listeners 
     public void dspUpdate(int input) { 
      count = String.valueOf(input); 
      System.out.println("Update Count " + count); 
      refreshListener.actionPerformed(new ActionEvent(this, input, "Update")); 
     } 

    } 


    //Only cares about the display of the screen 
    public static class View extends JPanel { 

     public Font smallFont = new Font("Dialog", Font.PLAIN, 12); 
     Color screenColor; 
     Color lineColor; 
     short btn=0; 
     String modelRefreshInfo; 
     int buttonPressCount; 
     Button helpButton; 

     public View(){ 
      //Build Panel 
      dspButton(); 

      //Create and show window 
      System.out.println("Created GUI on EDT? "+ SwingUtilities.isEventDispatchThread()); 
      JFrame f = new JFrame("JUST A TEST"); 
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      f.add(this); 
      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 // 
      add(helpButton); 
      setBackground(Color.black); 
      helpButton.setBackground(Color.black); 
      screenColor = Color.black; 
      helpButton.setForeground(Color.white); 
      lineColor = Color.white; 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(240, 180); 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.setColor(lineColor); 
      g.setFont(smallFont); 
      g.drawString("ModelUpdates: " + modelRefreshInfo, 10, 20); 
      g.drawString("RefreshCount: " + buttonPressCount, 10, 40); 
      if (btn == 0) { 
       dspButton(); 
       btn = 1; 
      } 
     } 
    } 

    //Links up the view and the model 
    public static class Control{ 
     View screen; 
     Model model; 

     public Control(View screen, Model model){ 
      this.screen = screen; 
      //Tells the screen what to do when the button is pressed 
      this.screen.helpButton.addActionListener(new ActionListener(){ 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        //Update the screen with the model's info 
        Control.this.screen.buttonPressCount++; 
        System.out.println("Pressed Button "); 
        Control.this.screen.repaint(); 
       } 
      }); 

      this.model = model; 
      //Hands new data in the model to the screen 
      this.model.refreshListener = new ActionListener(){ 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        //Update the screen with the model's info 
        Control.this.screen.modelRefreshInfo = Control.this.model.count; 
        System.out.println("Model Refreshed"); 
        Control.this.screen.repaint(); 
       } 
      }; 

      //Starts up the model 
      this.model.run(); 
     }  
    } 
} 
+0

+1 मैं 'स्विंगवर्कर' का उपयोग करता हूं, लेकिन सीरियल आईओ के लिए एक अलग थ्रेड आवश्यक है; यह भी देखें [उत्तर] (http://stackoverflow.com/a/12731752/230513)। – trashgod

+0

@ निक रिपपे धन्यवाद दस लाख! "वास्तविक" धारावाहिक कोड में ग्राफ़िक भाग द्वारा साझा किए गए बफर में ~ 200 आने वाली बाइट्स को सहेजना शामिल है। यह हर 500ms होता है ताकि घटना मेरा "टाइमर" हो। बफर में "छिपा" मान भी रीफ्रेश करने की आवश्यकता है जो बाद में मेनू के माध्यम से स्क्रॉल करते समय प्रदर्शित किया जा सकता है। क्या यह आपके द्वारा प्रदान किए गए कोड में होगा? मेरा मूल कोड ~ 9 000 लाइन था इसलिए मुझे कुछ सुझावों को आपके सुझावों के अनुसार वापस रखने में कुछ समय लगेगा। शायद कुछ सवाल के साथ कल वापस आ जाओ। अब बिस्तर पर जाने की जरूरत है ... /रिचर्ड – user1735586

+0

आपको उन अन्य मूल्यों को उसी श्रोता में क्रैक करने में सक्षम होना चाहिए - बस मॉडल की दृश्य से जानकारी पास करने वाले इस तरह की अधिक पंक्तियां जोड़ें। 'Control.this.screen.modelRefreshInfo = Control.this.model.count;' –