Java 在linux&;中以字节表示的响应始终为空;树莓皮喘息旁边的windows相同的代码工作得很好 为什么字节数组响应在LINUX系统中为空,但在WINDOWS中工作? 字节数组变量值均为0
我在串行通信中遇到了一个问题。我正在使用GEARMO USB RS 485/232转换器的串行USB转换器。在窗口中,我的程序在com13端口上运行良好。同样的程序在linux/raspberry pi wheezy/ubuntu系统中不起作用!我尝试了谷歌多Linux系统的所有解决方案。我检查了多个readBytes()、readBytes(4)、UTF编码选项。我还尝试了多字节转换函数,但每次都没有响应。 此外,我认为linux中的驱动程序可能会因为我的问题而出现问题,但连接打开,发送数据,usb led灯闪烁始终有效,但在返回响应数据时,数据总是以字节形式获取空值Java 在linux&;中以字节表示的响应始终为空;树莓皮喘息旁边的windows相同的代码工作得很好 为什么字节数组响应在LINUX系统中为空,但在WINDOWS中工作? 字节数组变量值均为0,java,linux,serial-port,bytearray,usbserial,Java,Linux,Serial Port,Bytearray,Usbserial,我在串行通信中遇到了一个问题。我正在使用GEARMO USB RS 485/232转换器的串行USB转换器。在窗口中,我的程序在com13端口上运行良好。同样的程序在linux/raspberry pi wheezy/ubuntu系统中不起作用!我尝试了谷歌多Linux系统的所有解决方案。我检查了多个readBytes()、readBytes(4)、UTF编码选项。我还尝试了多字节转换函数,但每次都没有响应。 此外,我认为linux中的驱动程序可能会因为我的问题而出现问题,但连接打开,发送数据,
import jssc.SerialPort;
import jssc.SerialPortList;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
....
// serialPort = new SerialPort("COM13"); // = FOR WINDOWS (work great)
serialPort = new SerialPort("/dev/ttyUSB0"); = FOR LINUX/UBUNTU/WHEEZY (Byte data is null)
serialPort.openPort();//Open port
serialPort.setParams(19200, 8, 1, 2);//Set params
serialPort.addEventListener(new SerialPortReader());//Add SerialPortEventListener
public void serialEvent(SerialPortEvent event) {
...
byte buffer[] = serialPort.readBytes();
System.out.println("Read Buffer "+ buffer);`
for (int k = 0; k < array.length; k++) {
System.out.println("Response[" + k + "] = " + "0x" + byteToHex(array[k]));
...
循环输出的System.out.println:
有人见过同样的问题吗?可能是权限问题。尝试以root用户身份运行,或将用户添加到拥有/dev/ttyUSB0的组,或更改该文件夹的权限(将在重新启动后消失)。@christopheD i只需将用户添加到/dev/ttyUSB0,然后重新启动,但不起作用。我也通过root进行了测试,但输出仍然相同。还有其他建议吗?尝试使用
strace
查看open()。他建议我对两条线做一些改动,如半双工或全双工。即使我试着用所有可能的方法更换电线,但仍然没有收到任何回应。我还要求提供SERIAL2USB linux驱动程序,但技术部门说linux不需要驱动程序。
Read Buffer [B@58c80c
Response[0] = 0x00
Response[1] = 0x00
Response[2] = 0x00
Response[3] = 0x00
Response[4] = 0x00
Response[5] = 0x00
Response[6] = 0x00
Response[7] = 0x00
Response[8] = 0x00
static public String byteToHex(byte b) {
// Returns hex String representation of byte b
char hexDigit[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] };
return new String(array);
}