Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将数据传输到串行端口?_Java_Eclipse_Java Me_Serial Port - Fatal编程技术网

Java 如何将数据传输到串行端口?

Java 如何将数据传输到串行端口?,java,eclipse,java-me,serial-port,Java,Eclipse,Java Me,Serial Port,我知道在J2ME中,CommConnection是使用串行端口时要使用的连接。我知道有openInputStream和openOutputStream方法,但事实上我不知道如何将数据从MIDLet传输到COM端口(插入手机电缆的USB端口,手机是Alcatel OT-806D)。例如,我想发送文本“Hello world”。如何做到这一点 以下是代码: J2ME: import java.io.IOException; import java.io.OutputStream; import ja

我知道在J2ME
中,CommConnection
是使用
串行端口时要使用的连接。我知道有
openInputStream
openOutputStream
方法,但事实上我不知道如何将数据从MIDLet传输到COM端口(插入手机电缆的USB端口,手机是Alcatel OT-806D)。例如,我想发送文本“Hello world”。如何做到这一点

以下是代码:

J2ME:

import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.io.CommConnection;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.*;

public class SerialPortMidlet extends MIDlet implements CommandListener, Runnable {
    private Command upload = new Command("upload", Command.SCREEN, 0);
    private Command exit = new Command("exit", Command.SCREEN, 1);
    private Form f = new Form("test serial port");
    private Thread uploadThread;
    private CommConnection com;
    private OutputStream os;
    public SerialPortMidlet()
    {
        f.addCommand(upload);
        f.addCommand(exit);
        f.setCommandListener(this);
        uploadThread = new Thread(this);
    }
    public void startApp() {
        Display.getDisplay(this).setCurrent(f);
    }
    public void pauseApp() {
    }
    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }
    public void commandAction(Command c, Displayable d) {
        if (c == upload)
        {
            uploadThread.start();
            f.removeCommand(upload);
        }
        else if (c == exit)
        {
            if (uploadThread.isAlive())
            {
                uploadThread.interrupt();
                try {
                    uploadThread.join();
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
            destroyApp(true);
        }
    }
    public void run() {
        try
        {
            String s = new String("andrana mandefa lavaka");
            com = (CommConnection) Connector.open("comm:COM4");
            os = com.openOutputStream();
            os.write(s.getBytes());
            os.close();
        } 
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }
}
J2SE:(Eclipse)

我运行J2SE程序,将手机电缆插入计算机(USB插槽),在J2ME应用程序中单击
上传
命令,但eclipse的输出屏幕中没有任何内容

那有什么问题

我运行此J2SE代码来检测手机电缆所在的端口:

import gnu.io.*;
public class SerialPortLister {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        listPorts();
    }
    private static void listPorts()
    {
        @SuppressWarnings("unchecked")
        java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
        while ( portEnum.hasMoreElements() ) 
        {
            CommPortIdentifier portIdentifier = portEnum.nextElement();
            System.out.println(portIdentifier.getName()  +  " - " +  getPortTypeName(portIdentifier.getPortType()) );
        }        
    }
    private static String getPortTypeName ( int portType )
    {
        switch ( portType )
        {
            case CommPortIdentifier.PORT_I2C:
                return "I2C";
            case CommPortIdentifier.PORT_PARALLEL:
                return "Parallel";
            case CommPortIdentifier.PORT_RAW:
                return "Raw";
            case CommPortIdentifier.PORT_RS485:
                return "RS485";
            case CommPortIdentifier.PORT_SERIAL:
                return "Serial";
            default:
                return "unknown type";
        }
    }
}
导入gnu.io.*;
公共类SerialPortLister{
/**
*@param args
*/
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
listPorts();
}
私有静态void listPorts()
{
@抑制警告(“未选中”)
java.util.Enumeration portEnum=CommPortIdentifier.getPortIdentifiers();
while(portEnum.hasMoreElements())
{
CommPortIdentifier portIdentifier=portEnum.nextElement();
System.out.println(portIdentifier.getName()+“-”+getPortTypeName(portIdentifier.getPortType());
}        
}
私有静态字符串getPortTypeName(int-portType)
{
交换机(端口类型)
{
外壳CommPortIdentifier.PORT_I2C:
返回“I2C”;
外壳CommPortIdentifier.PORT_并行:
返回“平行”;
案例CommPortIdentifier.PORT_原始:
返回“原始”;
外壳CommPortIdentifier.PORT_RS485:
返回“RS485”;
外壳CommPortIdentifier.PORT_串行:
返回“序列号”;
违约:
返回“未知类型”;
}
}
}
它显示COM4,因为当我拆下电缆时,只显示COM1和LPT1


那么问题出在哪里呢?

当您的手机连接到虚拟COM端口4时,计算机似乎能很好地检测到您的手机。但是,我不清楚您是否应该在电话端使用COM端口协议与计算机通信。这是完全可能的,只是有一个缓冲区的电话,一旦填补将在usb端口交付

我不知道你的手机,但我已经为一个微控制器编程了。在那里,我从来没有使用COM端口协议,而是设法用虚拟COM端口驱动程序与计算机通信


为了更好地理解我的观点,您可能可以参考手机上的微控制器文档。

在手机中,您只能找到COM1或COM2,而这些端口在计算机系统中总是忙于其他用途。但我发现COM4是手机使用的端口!这是一个好迹象。只需在计算机系统中搜索文件即可。它一定在某个目录中。或者你可以在J2SE代码中给出一个物理路径。手机是阿尔卡特OT-806D。是一个
微控制器
一个虚拟com端口
驱动程序
?是你手机的主芯片,相当于手机的微处理器。问题是我正在开发的J2ME应用程序将作为一个项目出售:我在一家信息服务公司工作;它应该适用于任何类型的电话设备。那么,如何使传输始终建立?这可能是您粘贴的代码中的一个输入错误,但据我所知,手机上的串行端口是COM1或COM2或其他什么,而计算机上的端口是COM4。您不应该在j2se中使用COM4,在j2me中使用COM1吗?
import gnu.io.*;
public class SerialPortLister {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        listPorts();
    }
    private static void listPorts()
    {
        @SuppressWarnings("unchecked")
        java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
        while ( portEnum.hasMoreElements() ) 
        {
            CommPortIdentifier portIdentifier = portEnum.nextElement();
            System.out.println(portIdentifier.getName()  +  " - " +  getPortTypeName(portIdentifier.getPortType()) );
        }        
    }
    private static String getPortTypeName ( int portType )
    {
        switch ( portType )
        {
            case CommPortIdentifier.PORT_I2C:
                return "I2C";
            case CommPortIdentifier.PORT_PARALLEL:
                return "Parallel";
            case CommPortIdentifier.PORT_RAW:
                return "Raw";
            case CommPortIdentifier.PORT_RS485:
                return "RS485";
            case CommPortIdentifier.PORT_SERIAL:
                return "Serial";
            default:
                return "unknown type";
        }
    }
}