[SOLVED] Question about developing serial port operation plugins under Eclipse

xiaoxiao2021-03-06  14

Ask, I use Eclipse to develop a plugin that can read the serial port information, which can be run separately as an application. But click the button when running in the plugin, click the button will appear.

Unhandled Event Loop Exceptionreason: Javax / Comm / NosuchportException is unusual. I don't know what is wrong?

Plugin window program:

package com.plugin.Hello; import org.eclipse.core.runtime *;. import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; import Org.eclipse.swt.events. *; import org.eclipse.swt.Layout. *; import org.eclipse.swt.widgets. *; import com.leon.serial. *;

/ ** * This class demonstrates JFace's ErrorDialog class * / public class DialogSample extends ApplicationWindow {/ ** * ShowError constructor * / public DialogSample () {super (null);} / ** * Runs the application * / public void run ( ) {// don't return from open () until window closs setblockonopen (true);

// Open the main window open ();

// dispose the display.getcurrent (). Dispose ();

/ ** * configures the shell * * @Param shell the shell * / protected void configureshell (shell shell) {super.configureShell (shell);

// set the title bar text and the size shell.settext ("read com"); shell.setsize (400, 400);}

/ ** * Creates the main window's contents * * @param parent the main window * @return Control * / protected Control createContents (Composite parent) {Composite composite = new Composite (parent, SWT.NONE); composite.setLayout (new GridLayout (1, false));

// Create A Big Text Box To Accept Error Text Final TEXT TEXT = New Text (Composite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL); Text.setLayOutdata (New Griddata (GridData.Fill_both);

// Create the button to launch the error dialog Button show = new Button (composite, SWT.PUSH); show.setText ( "open serial port"); show.addSelectionListener (new SelectionAdapter () {public void widgetSelected (SelectionEvent event) {Serialbean sb = new serialbean (2); string msg = ""; sb.initialize (); msg = sb.readport (10); sb.closeport (); text.settext (msg);}}); Return Composite } / ** * The application entry point * * @Param args the command line arguments * // * public static void main (string [] args) {new dialogsample (). Run ();} * /}

The plugin is changed according to HelloWorld

Package com.plugin.hello;

Import org.eclipse.ui.plugin. *; import org.osgi.framework.bundlecontext; import java.util. *;

. / ** * The main plugin class to be used in the desktop * / public class HelloPlugin extends AbstractUIPlugin {// The shared instance private static HelloPlugin plugin;.. // Resource bundle private ResourceBundle resourceBundle; / ** * The constructor. * / public HelloPlugin () {super (); plugin = this; try {resourceBundle = ResourceBundle.getBundle ( "com.plugin.Hello.HelloPluginResources");} catch (MissingResourceException x) {resourceBundle = null;}}

/ ** * this method is caled upon plug-in activation * / public void start (bundleContext context) throws exception {super.start (context);}

/ ** * this method is called when the plug-in is stopped * / public void stop (bundleContext context) throws exception {super.stop (context);

/ ** * returns the shared instance. * / Public static helloplugin getDefault () {Return plugin;

/ ** * Returns the string from the plugin's resource bundle, * or 'key' if not found * / public static String getResourceString (String key) {ResourceBundle bundle = HelloPlugin.getDefault () getResourceBundle ();.. Try {return ( !? bundle = null) bundle.getString (key): key;} catch (MissingResourceException e) {return key;}} / ** * Returns the plugin's resource bundle, * / public ResourceBundle getResourceBundle () {return resourceBundle;}}

/ ****************************************************************************************************************** *************************** /

/ * * Created on 2005-3-3 * * Todo to change the Template for this generated file go to * window - preferences - java - code style - code templates * / package com.leon.serial;

/ ** * @Author leon.guo * * Todo to change the template for this generated Type Comment Go to * WINDOW - Preferences - Java - Code Style - Code Templates * / Import Java.io. *;

/ ** * * This class reads message from the specific serial port and save * the message to the serial buffer * * / public class ReadSerial extends Thread {private SerialBuffer ComBuffer;. Private InputStream ComPort; / ** * * Constructor * * @ param SB The buffer to save the incoming messages * @param Port The InputStream from the specific serial port * * / public ReadSerial (SerialBuffer SB, InputStream Port) {ComBuffer = SB; ComPort = Port;}.. public void run () { INT C; TRY {while (true) {c = comport.read (); combffer.putchar (c);}} catch (ooException e) {}}}}

/ ************************************************** ***************************************************************************** // * * CREATED ON 2005-3-3 * * Todo to change the Template for this generated file go to * window - preferences - java - code style - code templates * / package com.leon.serial;

/ ** * @author leon.guo * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates * / import java.io.IOException; import java.io.InputStream ; import java.io.OutputStream; import javax.comm *;.. / ** * * This bean provides some basic functions to implement full dulplex * information exchange through the srial port * * / public class SerialBean {static String PortName; CommPortIdentifier portId; serialPort serialPort; static OutputStream out; static InputStream in; SerialBuffer SB; ReadSerial RT; / ** * * Constructor * * @param PortID the ID of the serial to be used 1 for COM1, * 2 for COM2, etc.. * / Public serialbean (int portid) {portname = "com" portid;} / ** * * this function initialize the serial port for Communication. It Startss A * Thread Which Consistently Monitors T . He serial port Any signal capturred * from the serial port is stored into a buffer area * * / public int Initialize () {int InitSuccess = 1;. Int InitFail = -1; try {portId = CommPortIdentifier.getPortIdentifier (PortName); Try {serialport = (serialport) Portid.open ("Serial_Communication", 2000);} catch (portinuseexcection e) {return initfail;

} // Use InputStream in to read from the serial port, and OutputStream // out to write to the serial port try {in = serialPort.getInputStream ();. Out = serialPort.getOutputStream ();} catch (IOException e) { return InitFail;} // Initialize the communication parameters to 9600, 8, 1, none try {serialPort.setSerialPortParams (9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);.} catch (UnsupportedCommOperationException e) {return InitFail; }}}}} Caturn (NosuchPortexception E) {Return Initfail;} // When SuccessFully Open The Serial Port, Create a New Serial Buffer, // THEN CREATE A Thread That Constently Accepts Incoming Signals from ////////// Return Success Information Return INT.START (); // Return Success Information Return INITSUCCESS; .. ** * * This function returns a string with a certain length from the incomin * messages * * @param length The length of the string to be returned * * / public String ReadPort (int length) {String Msg; Msg = SB .Getmsg (length); Return MSG;

} / ** * * this function sends a message through the serial port. * * @Param msg the string to be slow. * * / Public void writeport (string msg) {INT C; try {for (int i = 0; I

/ * * Created on 2005-3-3 * * Todo to change the Template for this generated file go to * window - preferences - java - code style - code templates * / package com.leon.serial;

转载请注明原文地址:https://www.9cbs.com/read-49776.html

New Post(0)