Kailash Chandra Dhaker
Lurker
Seeking for solution badly
====================
I am creating app for electric meter reading
When i am using below code for communication
On writing data with serialPort.write("S\r\n".getBytes());
It gives me serial number of the meter.
But as per our meter communication protocol i want to write data and read synchronously particular bytes from meter as per below code
but in readData method i always got -1 in result
Source code is also attached
====================
I am creating app for electric meter reading
When i am using below code for communication
Code:
UsbSerialDevice serialPort = UsbSerialDevice.createUsbSerialDevice(device, connection);
serialPort.open()
serialPort.setBaudRate(9600);
serialPort.setDataBits(UsbSerialInterface.DATA_BITS_8);
serialPort.setStopBits(UsbSerialInterface.STOP_BITS_1);
serialPort.setParity(UsbSerialInterface.PARITY_NONE);
serialPort.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF);
serialPort.read(mCallback);
On writing data with serialPort.write("S\r\n".getBytes());
It gives me serial number of the meter.
But as per our meter communication protocol i want to write data and read synchronously particular bytes from meter as per below code
Code:
Set connection
UsbDeviceConnection connection =
usbManager.openDevice(device);
if (connection != null &&
connection.claimInterface(usbInterfaceFound, true)) {
int result = connection.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset
Common.writeToLog("connection.controlTransfer(0x40, 0, 0, 0 "+String.valueOf(result));
result = connection.controlTransfer(0x40, 0, 1, 0, null, 0, 0);// clear Rx
Common.writeToLog("connection.controlTransfer(0x40, 0,1, 0 "+String.valueOf(result));
result = connection.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx
Common.writeToLog("connection.controlTransfer(0x40, 0, 2, 0 "+String.valueOf(result));
result = connection.controlTransfer(0x40, 0x02, 0x0002, 0, null, 0, 0); // flow control none
Common.writeToLog("connection.controlTransfer(0x40, 0x02, 0, 0 "+String.valueOf(result));
/*
* Calculate a Divisor at 48MHz 9600 : 0x4138 11400 : 0xc107 19200 : 0x809c
* 38400 : 0xc04e 57600 : 0x0034 115200 : 0x001a 230400 : 0x000d
*/
result = connection.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0);
Common.writeToLog("connection.controlTransfer(0x40, 0x03, 0, 0 "+String.valueOf(result));
result = connection.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0); // n/8/1
Common.writeToLog("connection.controlTransfer(0x40,0x04, 0x0008, 0 "+String.valueOf(result));
}
public void writeData(byte[] message)
{
message=new byte[1024];
message[0]=1;
message[1]=0;
message[2]=1;
message[3]=0;
message[4]=12;
message[5]=4;
message[6]=54;
message[7]=(byte)253;
Common.writeToLog("usbDeviceConnection "+String.valueOf(usbDeviceConnection));
synchronized (this) {
if (usbDeviceConnection != null) {
Common.writeToLog("data before write "+Common.bytesToHex(message));
int result= usbDeviceConnection.bulkTransfer(endpointOut,
message, message.length, 30000);
Common.writeToLog("bulkTransfer read result of writeData"+String.valueOf(result));
Common.writeToLog("data after write "+Common.bytesToHex(message));
}
}
}
public void readData(byte[] message, int offset, int length)
{
synchronized (this) {
if (usbDeviceConnection != null) {
Common.writeToLog("data before read "+Common.bytesToHex(message));
int result= usbDeviceConnection.bulkTransfer(endpointIn,
message,length, 30000);
Common.writeToLog("bulkTransfer read result of readData "+String.valueOf(result));
Common.writeToLog("data after read "+Common.bytesToHex(message));
}
}
}
but in readData method i always got -1 in result
Source code is also attached
Attachments
Last edited by a moderator:
