RXTX字符串编码,只有'?'印刷的

时间:2018-04-18 09:36:45

标签: java serial-port rxtx

我尝试使用java RXTX从rs232串口读取数据,但是当我打印输出时,它只打印'?'

这是我的代码;

public void read()
{
    int max = 32;
    int total = 0, read;
    byte[] buffer = new byte[max];
    try
    {
        while (total < max && (read = in.read(buffer, total, max - total)) >= 0) 
        {
            total += read;
        }

        System.out.println(new String(buffer, StandardCharsets.UTF_8));
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我尝试使用python程序并收到好的数据,所以我确定问题来自算法。 这里我发送hello时的输出是: ???? ? ?? ?????

你有什么想法吗?

1 个答案:

答案 0 :(得分:1)

每次我在互联网上提问时,我都会在一秒钟之后自己找到答案......我确实复制了连接protocole,我忘了更改借记。我刚换了// Set the date we're counting down to var countDownDate = new Date("2018-05-25 12:00:00").getTime(); // Update the count down every 1 second var x = setInterval(function() { // Get todays date and time var now = new Date().getTime(); // Find the distance between now an the count down date var distance = countDownDate - now; // Time calculations for days, hours, minutes and seconds var days = Math.floor(distance / (1000 * 60 * 60 * 24)); var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((distance % (1000 * 60)) / 1000); if (days.toString().length < 2) { days = "0" + days; } if (hours.toString().length < 2) { hours = "0" + hours; } if (minutes.toString().length < 2) { minutes = "0" + minutes; } if (seconds.toString().length < 2) { seconds = "0" + seconds; } // Display the result in the element with id="countdown" document.getElementById("countdown").innerHTML = days + " : " + hours + " : " + minutes + " : " + seconds; // If the count down is finished, write some text if (distance < 0) { clearInterval(x); document.getElementById("countdown").innerHTML = "<a href='/register'>Countdown Expired</a>"; } }, 1000);<span id="countdown"></span> 它运作良好!