Java串口写/发送ASCII数据

时间:2013-11-22 21:01:02

标签: java matlab serial-port robot

我的问题是我需要通过蓝牙在Java中控制移动机器人E-puck,发送命令如“D,100,100”来设置速度,“E”来获取速度等等。我有一些代码:

String command = "D,100,100";
OutputStream mOutputToPort = serialPort.getOutputStream();
mOutputToPort.write(command.getBytes());

因此,使用此方法write我只能发送byte[]数据,但我的机器人不会理解这一点。 例如,之前我一直在Matlab上使用这个命令:

s = serial('COM45');
fopen(s);
fprintf(s,'D,100,100','async');

或仅限程序Putty类型:

D,100,100 `enter`

其他信息:

我也发现,Matlab有另一种解决办法。

s = serial('COM45');
fopen(s);
data=[typecast(int8('-D'),'int8') typecast(int16(500),'int8') typecast(int16(500),'int8')];

在这种情况下:

data = [  -68  -12    1  -12    1];
fwrite(s,data,'int8','async');

在Java中是不是一样:

  byte data[] = new byte[5];
  data[0] = -'D';
  data[1] = (byte)(500 & 0xFF);
  data[2] = (byte)(500 >> 8);
  data[3] = (byte)(500 & 0xFF);
  data[4] = (byte)(500>> 8);

然后:

OutputStream mOutputToPort = serialPort.getOutputStream();
mOutputToPort.write(data);
mOutputToPort.flush();

2 个答案:

答案 0 :(得分:4)

代码注释中的主要细节。现在,您可以通过在命令窗口D,1000,-500中键入并按Enter键来更改车轮速度。

public class serialRobot {

        public static void main(String[] s) {
                SerialPort serialPort = null;
                try {
                        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM71");
                        if (portIdentifier.isCurrentlyOwned()) {
                                System.out.println("Port in use!");
                        } else {
                                System.out.println(portIdentifier.getName());

                                serialPort = (SerialPort) portIdentifier.open(
                                                "ListPortClass", 300);
                                int b = serialPort.getBaudRate();
                                System.out.println(Integer.toString(b));
                                serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8,
                                                SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                                serialPort.setInputBufferSize(65536);
                                serialPort.setOutputBufferSize(4096);

                                System.out.println("Opened " + portIdentifier.getName());

                                OutputStream mOutputToPort = serialPort.getOutputStream();
                                InputStream mInputFromPort = serialPort.getInputStream();


                                PerpetualThread t = readAndPrint(mInputFromPort);

                                inputAndSend(mOutputToPort);


                                t.stopRunning();

                                mOutputToPort.close();
                                mInputFromPort.close();
                        }
                } catch (IOException ex) {
                        System.out.println("IOException : " + ex.getMessage());
                } catch (UnsupportedCommOperationException ex) {
                        System.out.println("UnsupportedCommOperationException : " + ex.getMessage());
                } catch (NoSuchPortException ex) {
                        System.out.println("NoSuchPortException : " + ex.getMessage());
                } catch (PortInUseException ex) {
                        System.out.println("PortInUseException : " + ex.getMessage());
                } finally {
                        if(serialPort != null) {
                                serialPort.close();
                        }
                }

        }

        private static PerpetualThread readAndPrint(InputStream in) {
                final BufferedInputStream b = new BufferedInputStream(in);
                PerpetualThread thread = new PerpetualThread() {

                        @Override
                        public void run() {
                                byte[] data = new byte[16];
                                int len = 0;
                                for(;isRunning();) {
                                        try {
                                                len = b.read(data);
                                        } catch (IOException e) {
                                                e.printStackTrace();
                                        }
                                        if(len > 0) {
                                                System.out.print(new String(data, 0, len));
                                        }
                                }
                        }

                };

                thread.start();

                return thread;
        }

        private static void inputAndSend(OutputStream out) {
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                int k = 0;
                for(;;) {

                        String komanda;
                        try {
                                komanda = in.readLine();
                        } catch (IOException e) {
                                e.printStackTrace();
                                return;
                        }
                        komanda = komanda.trim();

                        if(komanda.equalsIgnoreCase("end"))       return;

                        byte komandaSiust[] = proces(komanda); //Command we send after first 

//connection, it's byte array where 0 member is the letter that describes type of command, next two members 

// is about left wheel speed, and the last two - right wheel speed.



                        try {
                               if(k == 0){
                                String siunc = "P,0,0\n"; // This command must be sent first time, when robot is connected, otherwise other commands won't work
                                ByteBuffer bb = ByteBuffer.wrap(siunc.getBytes("UTF-8"));
                                bb.order(ByteOrder.LITTLE_ENDIAN);
                                out.write(bb.array());
                                out.flush();      
                               }else{
                               ByteBuffer bb = ByteBuffer.wrap(komandaSiust);
                               bb.order(ByteOrder.LITTLE_ENDIAN);
                               out.write(bb.array());
                               out.flush();
                               }
                               k++;

                        } catch (IOException e) {
                                e.printStackTrace();
                                return;
                        }
                }
        }

        private static byte[] proces(String tekstas){
            tekstas = tekstas.trim();
            char[] charArray = tekstas.toCharArray();
            byte kodas1[];



            int fComa = tekstas.indexOf(',', 1);
            int sComa = tekstas.indexOf(',', 2);
            int matavimas = charArray.length;
            int skir1 = sComa - fComa - 1;
            int skir2 = matavimas - sComa -1;

            char leftSpeed[] = new char[skir1];

            for(int i = 0; i < skir1; i++){
                  leftSpeed[i] = charArray[fComa + i + 1];
            }

            char rightSpeed[] = new char[skir2];

            for(int i = 0; i < skir2; i++){
                rightSpeed[i] = charArray[sComa + i + 1];
            }
            String right = String.valueOf(rightSpeed);
            String left = String.valueOf(leftSpeed);


            int val1 = Integer.parseInt(left);
            int val2 = Integer.parseInt(right);
            kodas1 = new byte[5];
            kodas1[0] = (byte)-charArray[0];
            kodas1[1] = (byte)(val1 & 0xFF);
            kodas1[2] = (byte)(val1 >> 8);
            kodas1[3] = (byte)(val2 & 0xFF);
            kodas1[4] = (byte)(val2 >> 8);

            return kodas1;


        }

        private static class PerpetualThread extends Thread {
                private boolean isRunning = true;

                public boolean isRunning() {    return isRunning;       }

                public void stopRunning()       {
                        isRunning = false;
                        this.interrupt();
                }
        }
}

答案 1 :(得分:2)

根据the documentation,您需要在串口上调用setSerialPortParams(int baudrate, int dataBits, int stopBits, int parity)