Skip to main content
Home
for DLMS smart meters

Main navigation

  • Home
  • Products
  • About us
  • Open Source
  • Community
  • Forum
  • Downloads
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Gurux Serial Android (java) Not Connect Using Standard Com Port (CP210x UART Bridge)

Gurux Serial Android (java) not connect using Standard Com Port (CP210x UART Bridge)

By Pavel Saburov, 8 March, 2024
Forums
Gurux.Serial

Hi. I have RF reader, based on CP210x UART Bridge with two USB interfaces:

I create project in Android Studio, kotlin, add Java Modul Serial.

val port = serial.getPorts()
get two ports, that have same names:
0 = {GXPort@17520} CP210x UART Bridge
1 = {GXPort@17521} CP210x UART Bridge
gxport = port[0] or gxport = port[1] have same result

serial.port = gxport
serial.baudRate = BaudRate.BAUD_RATE_9600
serial.dataBits = 8
serial.parity = Parity.EVEN
serial.stopBits = StopBits.ONE
serial.writeTimeout = 1000
serial.readTimeout = 2000
serial.dtrEnable = false
serial.rtsEnable = false
serial.trace = TraceLevel.VERBOSE
serial.addListener(this)
serial.open()

In Class GXserial method open() have code:
...
mConnection = manager.openDevice(it.getValue());
UsbInterface usbIf = it.getValue().getInterface(0);

mConnection have mDevice that have two mIntefaces:
0 = {UsbInterface@.....} UsbInterface[mId=0,mAlternateSetting=0,mName=Enhanced Com Port,mClass=255,mSubclass=0,mProtocol=0,mEndpoints=[
UsbEndpoint[mAddress=129,mAttributes=2,mMaxPacketSize=64,mInterval=0]
UsbEndpoint[mAddress=1,mAttributes=2,mMaxPacketSize=64,mInterval=0]];
1 = {UsbInterface@.....} UsbInterface[mId=1,mAlternateSetting=0,mName=Standard Com Port,mClass=255,mSubclass=0,mProtocol=0,mEndpoints=[
UsbEndpoint[mAddress=130,mAttributes=2,mMaxPacketSize=32,mInterval=0]
UsbEndpoint[mAddress=2,mAttributes=2,mMaxPacketSize=32,mInterval=0]]

I need send my data using mInterfaces = 1 (Standard Com Port)
forced use this interface: UsbInterface usbIf = it.getValue().getInterface(1);

BUT, program always using mInterface = 0 (Enhanced Com Port).
Why?

How send data by mInterfaces = 1 (Standard Com Port)

Pavel Saburov

1 year 4 months ago

//implementation("org.gurux…

//implementation("org.gurux:gurux.serial.android:2.0.2") and UsbInterface usbIf = it.getValue().getInterface(1); work :-)))

but another problem with sending any data.

when UsbInterface usbIf = it.getValue().getInterface(0); data send and receive normal

when UsbInterface usbIf = it.getValue().getInterface(1); always (ret = mConnection.bulkTransfer(mOut, buff, pos, dataSize, mWriteTimeout);) ret=-1 and execute throw new IllegalArgumentException("Data send failed.");

in this method:
public final void send(final Object data) throws Exception {
if (mOut == null) {
throw new RuntimeException("Serial port is not open.");
}
if (mTrace == TraceLevel.VERBOSE) {
notifyTrace(new TraceEventArgs(TraceTypes.SENT, data));
}
// Reset last position if end of packet is used.
mSyncBase.resetLastPosition();
byte[] buff = GXSynchronousMediaBase.getAsByteArray(data);
if (buff == null) {
throw new IllegalArgumentException("Data send failed. Invalid data.");
}
int ret, pos = 0, dataSize = mOut.getMaxPacketSize();
while (pos != buff.length) {
if (buff.length - pos < dataSize) {
dataSize = buff.length - pos;
}
ret = mConnection.bulkTransfer(mOut, buff, pos, dataSize, mWriteTimeout);
if (ret != dataSize) {
throw new IllegalArgumentException("Data send failed.");
}
pos += ret;
}
this.mBytesSend += buff.length;
}

Why?

Profile picture for user Kurumi

Kurumi

1 year 4 months ago

Hi, Have you tried this? /…

Hi,

Have you tried this?

//Add serial ports.
serial = new GXSerial(this);
serial.addListener(this);
serial.setPort(serial.getPorts().at(1));
serial.open();

BR,
Mikko

Pavel Saburov

1 year 4 months ago

Good evening. My test…

Good evening.

My test project:
class MainActivity : AppCompatActivity(), IGXMediaListener {
private lateinit var binding: ActivityMainBinding
var log = ""
var receivedString = ""

override fun onCreate(savedInstanceState: Bundle?) {
var serial : GXSerial

super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

serial = GXSerial(this)
serial.addListener(this)

binding.buttonConnect.setOnClickListener {
try {
serial.port = serial.ports[1]
serial.baudRate = BaudRate.BAUD_RATE_9600
serial.dataBits = 8
serial.parity = Parity.EVEN
serial.stopBits = StopBits.ONE
serial.open()
}
catch (ex: Exception) {
updateLog(ex.toString())
}
}

binding.buttonSend.setOnClickListener {
try {
if(serial.isOpen){
val cmd = "<pswd=tex addr=local cmd=set ch=0 crc=x>"
serial.send(cmd)
updateLog(cmd)
}
}
catch (ex: Exception) {
updateLog(ex.toString())
}
}
}

Android manifest have:
...
<uses-permission android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.USB_PERMISSION" />

settings.gradle:
...
rootProject.name = "Test"
include(":app")
include(":Serial")

build.gradle:
...
implementation(project(":Serial"))
implementation("org.gurux:Gurux.Common.Android:2.0.1")

if in GXSerial.java in function open():
UsbInterface usbIf = it.getValue().getInterface(0); - all works normal:
22:07:35: onPropertyChanged: PortName
22:07:35: onPropertyChanged: Parity
22:07:35: MediaStateChange: OPEN
22:07:39: <pswd=tex addr=local cmd=set ch=0 crc=x>
22:07:39: <r=0 addr=local ch=0>

but i need use another interface by usb port:
UsbInterface usbIf = it.getValue().getInterface(1); - and after send several messages throw exception:
22:12:38: onPropertyChanged: PortName
22:12:38: onPropertyChanged: Parity
22:12:38: MediaStateChange: OPEN
22:12:39: <pswd=tex addr=local cmd=set ch=0 crc=x>
22:12:41: <pswd=tex addr=local cmd=set ch=0 crc=x>
22:12:43: <pswd=tex addr=local cmd=set ch=0 crc=x>
22:12:50: java.lang.IllegalArgumentException: Data send failed.

What am I doing wrong? Maybe the USB has not been granted permissions? I've been trying to solve the problem for 4 days. This afternoon, we somehow managed to transmit and receive data, but without changing anything in the code, it stopped working.

Profile picture for user Kurumi

Kurumi

1 year 4 months ago

Hi Pavel, I did try this…

Hi Pavel,

I did try this yesterday with CP210x and it worked without problems. I don't believe that this is a permission problem if you were able to send data.

Have you tried with serial android example app?

https://github.com/Gurux/gurux.serial.android

BR,
Mikko

Pavel Saburov

1 year 4 months ago

Good afternoon. Yes, I…

Good afternoon.
Yes, I certainly tried with the serial android example app. When connecting the device, 2 ports with the same name are available: CP210x UART Bridge.
When selecting ANY of the ports, sending is ALWAYS carried out via the Enhanced COM PORT, which in USB is with the index 0. This is hardcoded in the line: UsbInterface usbIf = it.getValue().getInterface(0); (GXSerial.java, line 768).
When changing the code, the Standard COM PORT interface would be used: UsbInterface usbIf = it.getValue().getInterface(1); the serial android example app also has the error "Data send failed."

Image

Pavel Saburov

1 year 4 months ago

Вот изображение, которое…

Here is an image that shows that the null interface is always selected, regardless of the selected port.

Image
Profile picture for user Kurumi

Kurumi

1 year 2 months ago

Hi Pavel, We have tried to…

Hi Pavel,

We have tried to repeat this with multiple CP210x chipsets, but it works every time. I have a few questions.
1. What Android version you are using?
2. Do you have a web link for CP210x UART Bridge that you are using?

BR,
Mikko

Pavel Saburov

1 year 2 months ago

Hi. I use api 28-32…

Hi.
I use api 28-32.
Documentation by this Link:
https://drive.google.com/file/d/1oHbFcsjs7Mvby9MB13HDTy7N8wQD7-lH/view?…
When use interface 1 (standart com port) all work.

Profile picture for user Kurumi

Kurumi

1 year 2 months ago

Hi, I found one chipset from…

Hi,

I found one chipset from Silicon Labsm but it's using SiLabs CP2012 and it worked without problems.

I ordered a few USB dongles that use CP2105. Now we have to wait until they arrive and this can be tested.

BR,
Mikko

  • Log in or register to post comments
  • Create new account
  • Reset your password

Hire Us!

Latest Releases

  • Wed, 07/09/2025 - 16:41
    Gurux.Serial.Android 2.0.13
  • Wed, 07/09/2025 - 12:07
    gurux.dlms.c 9.0.2507.0901
  • Sat, 07/05/2025 - 15:04
    Gurux.DLMS.Python 1.0.188
  • Tue, 07/01/2025 - 10:09
    Gurux.DLMS.Python 1.0.187
  • Tue, 07/01/2025 - 09:54
    gurux.dlms.c 9.0.2507.0101

New forum topics

  • DLMS communication issue when routing through VPN on Android
  • ESP32-Based Smart Meter Interface via DLMS/COSEM
  • svr_postWrite: args->size Always Zero
  • DLMS over LoraWan in water metering
  • Update High password - any way to cipher new password
More
RSS feed
Privacy FAQ GXDN Issues Contact
Follow Gurux on Twitter Follow Gurux on Linkedin