简体   繁体   English

发送一个Java Map <Integer,String> 使用XBee

[英]Send a Java Map<Integer,String> With XBee

I would send a Java Map with XBee, but I see in the Git repository how they send byte[]. 我将使用XBee发送Java Map,但是我在Git存储库中看到了它们如何发送byte []。 How can I send my Map? 如何发送地图? I thinked use Python for that, but I prefer use Java for all and not use Java for all unless for send the message. 我以为使用Python,但是我更喜欢全部使用Java,除非发送消息,否则不要全部使用Java。

This is my code: 这是我的代码:

public class Comunicacion {
    /* Constants */
    private static final String PORT = "ttyUSB0";
    private static final int BAUD_RATE = 9600;

    private static Map<Integer, String> dataToSend = new HashMap<Integer, String>();

public static void main(String[] args) {
    XBeeDevice myDevice = new XBeeDevice(PORT, BAUD_RATE);
    byte[] dataInByte = dataToSend.getBytes();

    try {
        myDevice.open();

        System.out.format("Sending broadcast data: '%s'", new String(dataInByte));

        myDevice.sendBroadcastData(dataInByte);

        System.out.println(" >> Success");

    } catch (XBeeException e) {
        System.out.println(" >> Error");
        e.printStackTrace();
        System.exit(1);
    } finally {
        myDevice.close();
    }
}

Just an example of a conversion for just about anything to a binary string... However I am unsure of what your sending this info too and the required headers. 只是将任何内容转换为二进制字符串的示例...但是,我不确定您也发送此信息以及所需的标头。 None the less, this may get you what you want, a converted binary value. 但是,这可能会为您带来所需的转换后的二进制值。

public class JavaApplication10 {
    static HashMap<String, Boolean> map = new HashMap<>(); 


    public static void main(String[] args){

        byte[] dataInByte = map.toString().getBytes();
        String sender ="";
        for (byte b : dataInByte) {
            sender = sender + (Integer.toBinaryString(b));
        }
        System.out.println(sender);   
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM