⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serialtester.java

📁 控制移到机器人的例子程序
💻 JAVA
字号:
/*
	A basic extension of the java.awt.Frame class
 */

import java.awt.*;

public class SerialTester extends Frame
{
    serialPort myPort;
	public SerialTester()
	{
		// This code is automatically generated by Visual Cafe when you add
		// components to the visual environment. It instantiates and initializes
		// the components. To modify the code, only use code syntax that matches
		// what Visual Cafe can generate, or Visual Cafe may be unable to back
		// parse your Java file into its visual environment.
		//{{INIT_CONTROLS
		setLayout(null);
		setVisible(false);
		setSize(insets().left + insets().right + 444,insets().top + insets().bottom + 278);
		button1 = new java.awt.Button();
		button1.setActionCommand("button");
		button1.setLabel("openSerial");
		button1.setBounds(insets().left + 12,insets().top + 60,100,32);
		button1.setBackground(new Color(12632256));
		add(button1);
		label1 = new java.awt.Label("Portnum, Baudnum");
		label1.setBounds(insets().left + 12,insets().top + 36,120,17);
		add(label1);
		textField1 = new java.awt.TextField();
		textField1.setText("1");
		textField1.setBounds(insets().left + 24,insets().top + 12,29,18);
		add(textField1);
		textField2 = new java.awt.TextField();
		textField2.setText("1");
		textField2.setBounds(insets().left + 84,insets().top + 12,29,19);
		add(textField2);
		button2 = new java.awt.Button();
		button2.setActionCommand("button");
		button2.setLabel("closeSerial");
		button2.setBounds(insets().left + 144,insets().top + 60,90,31);
		button2.setBackground(new Color(12632256));
		add(button2);
		label2 = new java.awt.Label("returnval");
		label2.setBounds(insets().left + 12,insets().top + 96,63,18);
		add(label2);
		label3 = new java.awt.Label("returnval");
		label3.setBounds(insets().left + 144,insets().top + 96,63,18);
		add(label3);
		textField3 = new java.awt.TextField();
		textField3.setBounds(insets().left + 72,insets().top + 96,36,20);
		add(textField3);
		textField4 = new java.awt.TextField();
		textField4.setBounds(insets().left + 204,insets().top + 96,36,18);
		add(textField4);
		button3 = new java.awt.Button();
		button3.setActionCommand("button");
		button3.setLabel("sendByte");
		button3.setBounds(insets().left + 12,insets().top + 168,72,24);
		button3.setBackground(new Color(12632256));
		add(button3);
		textField5 = new java.awt.TextField();
		textField5.setBounds(insets().left + 12,insets().top + 144,48,24);
		add(textField5);
		button4 = new java.awt.Button();
		button4.setActionCommand("button");
		button4.setLabel("setTimeout");
		button4.setBounds(insets().left + 132,insets().top + 168,72,24);
		button4.setBackground(new Color(12632256));
		add(button4);
		textField6 = new java.awt.TextField();
		textField6.setBounds(insets().left + 132,insets().top + 144,55,18);
		add(textField6);
		button5 = new java.awt.Button();
		button5.setActionCommand("button");
		button5.setLabel("getByte");
		button5.setBounds(insets().left + 252,insets().top + 168,69,24);
		button5.setBackground(new Color(12632256));
		add(button5);
		textField8 = new java.awt.TextField();
		textField8.setBounds(insets().left + 60,insets().top + 192,36,18);
		add(textField8);
		textField9 = new java.awt.TextField();
		textField9.setBounds(insets().left + 180,insets().top + 192,36,18);
		add(textField9);
		textField10 = new java.awt.TextField();
		textField10.setBounds(insets().left + 276,insets().top + 192,46,24);
		add(textField10);
		setTitle("Serial Test Window");
		//}}

		//{{INIT_MENUS
		//}}

		//{{REGISTER_LISTENERS
		SymWindow aSymWindow = new SymWindow();
		this.addWindowListener(aSymWindow);
		SymMouse aSymMouse = new SymMouse();
		button1.addMouseListener(aSymMouse);
		button2.addMouseListener(aSymMouse);
		button3.addMouseListener(aSymMouse);
		button4.addMouseListener(aSymMouse);
		button5.addMouseListener(aSymMouse);
		//}}
		this.setVisible(true);
		myPort = new serialPort();
	}

	public SerialTester(String title)
	{
		this();
		setTitle(title);
	}

	public synchronized void show()
	{
		move(50, 50);
		super.show();
	}

	public void addNotify()
	{
	    // Record the size of the window prior to calling parents addNotify.
	    Dimension d = getSize();
	    
		super.addNotify();

		if (fComponentsAdjusted)
			return;

		// Adjust components according to the insets
		setSize(insets().left + insets().right + d.width, insets().top + insets().bottom + d.height);
		Component components[] = getComponents();
		for (int i = 0; i < components.length; i++)
		{
			Point p = components[i].getLocation();
			p.translate(insets().left, insets().top);
			components[i].setLocation(p);
		}
		fComponentsAdjusted = true;
	}

    // Used for addNotify check.
	boolean fComponentsAdjusted = false;

	//{{DECLARE_CONTROLS
	java.awt.Button button1;
	java.awt.Label label1;
	java.awt.TextField textField1;
	java.awt.TextField textField2;
	java.awt.Button button2;
	java.awt.Label label2;
	java.awt.Label label3;
	java.awt.TextField textField3;
	java.awt.TextField textField4;
	java.awt.Button button3;
	java.awt.TextField textField5;
	java.awt.Button button4;
	java.awt.TextField textField6;
	java.awt.Button button5;
	java.awt.TextField textField8;
	java.awt.TextField textField9;
	java.awt.TextField textField10;
	//}}

	//{{DECLARE_MENUS
	//}}

	class SymWindow extends java.awt.event.WindowAdapter
	{
		public void windowClosing(java.awt.event.WindowEvent event)
		{
			Object object = event.getSource();
			if (object == SerialTester.this)
				Frame1_WindowClosing(event);
		}
	}
	
	void Frame1_WindowClosing(java.awt.event.WindowEvent event)
	{
		hide();		 // hide the Frame
	}

	class SymMouse extends java.awt.event.MouseAdapter
	{
		public void mouseClicked(java.awt.event.MouseEvent event)
		{
			Object object = event.getSource();
			if (object == button1)
				button1_MouseClick(event);
			else if (object == button2)
				button2_MouseClick(event);
			else if (object == button3)
				button3_MouseClick(event);
			else if (object == button4)
				button4_MouseClick(event);
			else if (object == button5)
				button5_MouseClick(event);
		}
	}

	void button1_MouseClick(java.awt.event.MouseEvent event)
	{
		// the openSerial button
		int portNum = Integer.parseInt(textField1.getText());
		int baudSpec = Integer.parseInt(textField2.getText());
		
		int retVal;
		
		retVal = myPort.openSerial(portNum, baudSpec);
		
		textField3.setText(Integer.toString(retVal));
	}

	void button2_MouseClick(java.awt.event.MouseEvent event)
	{
		// to do: code goes here.
		
		int retVal;
		
		retVal = myPort.closeSerial();
		textField4.setText(Integer.toString(retVal));
	}

	void button3_MouseClick(java.awt.event.MouseEvent event)
	{
		// send a byte
		
        int retVal;
        int byteToSend = Integer.parseInt(textField5.getText());
        System.out.println("Sending byte " + byteToSend);
        retVal = myPort.sendByte(byteToSend);
        textField8.setText(Integer.toString(retVal));
	}
	

	void button4_MouseClick(java.awt.event.MouseEvent event)
	{
		// set the timeout
        int retVal;
        retVal = myPort.setReadTimeout(Integer.parseInt(textField6.getText()));
        textField9.setText(Integer.toString(retVal));
	}
	

	void button5_MouseClick(java.awt.event.MouseEvent event)
	{
		// getByte
        int retVal;
        retVal = myPort.readByte();
        textField10.setText(Integer.toString(retVal));		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -