Liz 0 Posted October 9 (edited) Hello, I am working with Rad Studio and I need to make an application in Delphi for the phone that connects via USB to an Esp32 and sends and receives data. I didn't find anything available for delphi, so I chose to download a java library that handles it(https://github.com/mik3y/usb-serial-for-android), downloaded the .jar and converted it to .pas with java2op, but now I can't find a way to use the methods correctly and have the devices detected. by usb. Could you see this system, tell me how I could call the classes and methods in the interface or do you have some direct form of Delphi that allows me to make the connections with the USBs or at least see the connected devices usb-serial-for-android-3.8.0.jar JavaInterfaces.pas Edited October 10 by Liz Share this post Link to post
Remy Lebeau 1394 Posted October 10 Can you fix the font formatting of your message? Your text is cut off. Can't read what the actual problem is that you are having with the converted code. Share this post Link to post
Liz 0 Posted October 10 7 minutes ago, Remy Lebeau said: Can you fix the font formatting of your message? Your text is cut off. Can't read what the actual problem is that you are having with the converted code. Yes sorry, did it already Share this post Link to post
Zoran Bonuš 12 Posted October 10 Have you considered https://winsoft.sk/acpusb.htm (commercial)? Share this post Link to post
Liz 0 Posted October 10 10 hours ago, Zoran Bonuš said: Have you considered https://winsoft.sk/acpusb.htm (commercial)? Yes, I think I had already checked it, but for the project I need to do, which is for the school, the solution must be free. That's why I'm looking for even java solutions to use in rad studio delphi because I need it to be free. But that solution would be perfect but i cant afford that Share this post Link to post
Remy Lebeau 1394 Posted October 10 (edited) 20 hours ago, Liz said: I didn't find anything available for delphi, so I chose to download a java library that handles it(https://github.com/mik3y/usb-serial-for-android), downloaded the .jar and converted it to .pas with java2op, but now I can't find a way to use the methods correctly and have the devices detected. by usb. That website provides examples in Java. Did you try matching them up to the classes in the Delphi unit which you generated? What exactly have you tried so far and are having trouble with? For instance, the 1st example to open a device might look something like this: uses ..., Androidapi.JNI.JavaTypes, Androidapi.JNI.GraphicsContentViewText, Androidapi.Helpers, JavaInterfaces; var manager: JUsbManager; availableDrivers: JList; driver: JUsbSerialDriver; connection: JUsbDeviceConnection; port: JUsbSerialPort; begin // Find all available drivers from attached devices. manager := TJUsbManager.Wrap(TAndroidHelper.Context.getSystemService(TJContext.JavaClass.USB_SERVICE)); availableDrivers := TJUsbSerialProber.JavaClass.getDefaultProber.findAllDrivers(manager); if availableDrivers.isEmpty then begin Exit; end; // Open a connection to the first available driver. driver := TJUsbSerialDriver.Wrap(availableDrivers.get(0)); connection := manager.openDevice(driver.getDevice); if connection = nil then begin // add UsbManager.requestPermission(driver.getDevice(), ..) handling here Exit; end; port := TJUsbSerialPort.Wrap(driver.getPorts.get(0)); // Most devices have just one port (port 0) port.open(connection); port.setParameters(115200, 8, TJUsbSerialPort.JavaClass.STOPBITS_1, TJUsbSerialPort.JavaClass.PARITY_NONE); ... end; Edited October 10 by Remy Lebeau Share this post Link to post
Liz 0 Posted October 10 (edited) 14 minutes ago, Remy Lebeau said: That website provides examples in Java. Did you try matching them up to the classes in the Delphi unit which you generated? What exactly have you tried so far and are having trouble with? For instance, the 1st example to open a device might look something like this: uses ..., Androidapi.JNI.JavaTypes, JavaInterfaces; var manager: JUsbManager; availableDrivers: JList; driver: JUsbSerialDriver; connection: JUsbDeviceConnection; port: JUsbSerialPort; begin // Find all available drivers from attached devices. manager := TJUsbManager.Wrap(TAndroidHelper.Context.getSystemService(TJContext.JavaClass.USB_SERVICE)); availableDrivers := TJUsbSerialProber.JavaClass.getDefaultProber.findAllDrivers(manager); if availableDrivers.isEmpty then begin Exit; end; // Open a connection to the first available driver. port := TJUsbSerialDriver.Wrap(availableDrivers.get(0)); connection := manager.openDevice(driver.getDevice); if connection = nil then begin // add UsbManager.requestPermission(driver.getDevice(), ..) handling here Exit; end; port := TJUsbSerialPort.Wrap(driver.getPorts.get(0)); // Most devices have just one port (port 0) port.open(connection); port.setParameters(115200, 8, TJUsbSerialPort.JavaClass.STOPBITS_1, TJUsbSerialPort.JavaClass.PARITY_NONE); ... end; The problem is next: JUsbManagerClass = interface(JObjectClass) ['{1048A6E9-E1B5-4DA5-A168-ED91E8DE5284}'] {class} function _GetACTION_USB_ACCESSORY_ATTACHED: JString; cdecl; {class} function _GetACTION_USB_ACCESSORY_DETACHED: JString; cdecl; {class} function _GetACTION_USB_DEVICE_ATTACHED: JString; cdecl; {class} function _GetACTION_USB_DEVICE_DETACHED: JString; cdecl; {class} function _GetEXTRA_ACCESSORY: JString; cdecl; {class} function _GetEXTRA_DEVICE: JString; cdecl; {class} function _GetEXTRA_PERMISSION_GRANTED: JString; cdecl; {class} function getAccessoryList: TJavaObjectArray<JUsbAccessory>; cdecl; {class} function getDeviceList: JHashMap; cdecl; {class} function openDevice(device: JUsbDevice): JUsbDeviceConnection; cdecl;//Deprecated {class} procedure requestPermission(device: JUsbDevice; pi: JPendingIntent); cdecl; overload;//Deprecated {class} procedure requestPermission(accessory: JUsbAccessory; pi: JPendingIntent); cdecl; overload;//Deprecated {class} property ACTION_USB_ACCESSORY_ATTACHED: JString read _GetACTION_USB_ACCESSORY_ATTACHED; {class} property ACTION_USB_ACCESSORY_DETACHED: JString read _GetACTION_USB_ACCESSORY_DETACHED; {class} property ACTION_USB_DEVICE_ATTACHED: JString read _GetACTION_USB_DEVICE_ATTACHED; {class} property ACTION_USB_DEVICE_DETACHED: JString read _GetACTION_USB_DEVICE_DETACHED; {class} property EXTRA_ACCESSORY: JString read _GetEXTRA_ACCESSORY; {class} property EXTRA_DEVICE: JString read _GetEXTRA_DEVICE; {class} property EXTRA_PERMISSION_GRANTED: JString read _GetEXTRA_PERMISSION_GRANTED; end; [JavaSignature('android/hardware/usb/UsbManager')] JUsbManager = interface(JObject) ['{6F603A25-E816-4012-9B23-054B428A4A75}'] function hasPermission(device: JUsbDevice): Boolean; cdecl; overload;//Deprecated function hasPermission(accessory: JUsbAccessory): Boolean; cdecl; overload;//Deprecated function openAccessory(accessory: JUsbAccessory): JParcelFileDescriptor; cdecl;//Deprecated end; TJUsbManager = class(TJavaGenericImport<JUsbManagerClass, JUsbManager>) end; Here, for example, it doesn't let me use the openDevice that is in JUsbManagerClass. In other words, I can't find a way to access those functions of that class, however, those of JUsbManager if possible. I don't know how to wrap that. In your example I get two errors here: port := TJUsbSerialDriver.Wrap(availableDrivers.get(0)); connection := manager.openDevice(driver.getDevice); It tells me: [DCC Error] usb.pas(48): E2010 Incompatible types: 'JUsbSerialPort' and 'JUsbSerialDriver' [DCC Error] usb.pas(49): E2003 Undeclared identifier: 'openDevice' Edited October 10 by Liz Share this post Link to post
Remy Lebeau 1394 Posted October 10 (edited) 1 hour ago, Liz said: port := TJUsbSerialDriver.Wrap(availableDrivers.get(0)); connection := manager.openDevice(driver.getDevice); It tells me: [DCC Error] usb.pas(48): E2010 Incompatible types: 'JUsbSerialPort' and 'JUsbSerialDriver' Sorry, that was a copy/paste typo on my part. 'port' should be 'driver'. I have corrected it. 1 hour ago, Liz said: [DCC Error] usb.pas(49): E2003 Undeclared identifier: 'openDevice' Then the Java2Op translation is wrong. According to the official Android documentation, not of the UsbManager methods are static, so none of them should be declared in JUsbManagerClass, they should all be in JUsbManager instead. Which version of RADStudio/Java2Op are you using? This was a known problem in earlier Delphi versions (not sure if it's fixed in the latest version, I'll have to check), for instance: RSP-15473: Java2Op emits erroneous code RSP-24029: Regression in Java2OP so that instance methods are treated as class methods RSP-40400: Java2OP say often that method as Static (class) when they are not Edited October 10 by Remy Lebeau Share this post Link to post
Liz 0 Posted October 10 4 minutes ago, Remy Lebeau said: Sorry, that was a copy/paste typo on my part. 'port' should be 'driver'. I have corrected it. Then the Java2Op translation is wrong. openDevice() is not a static method, so it should be in JUsbManager instead of JUsbManagerClass. Yes, no problem, I realized later, I already put the ´driver´ and it didn't cause any problems. What do you recommend? Should I use java2op again to transfer the .jar to .pas or look for another alternative? Share this post Link to post
Remy Lebeau 1394 Posted October 10 (edited) 52 minutes ago, Liz said: What do you recommend? Should I use java2op again to transfer the .jar to .pas or look for another alternative? I'll see if the problem exists in the latest Delphi. If the problem continues, please file a bug report with Embarcadero about Java2Op still being broken. You might need to just fix the generated code manually. Or look around to see if there is a 3rd party importer that works better than Java2Op. Edited October 10 by Remy Lebeau Share this post Link to post
Liz 0 Posted October 10 43 minutes ago, Remy Lebeau said: If you are not using the latest Delphi, then I suggest you upgrade. If the problem continues, complain to Embarcadero about Java2Op still being broken. And/or see if you can find a 3rd party importer that works better than Java2Op. I'm using Rad Studio 12, I know it's not the latest. I'll try to install the latest one and see. Could you try converting the .jar with java2op and see if it converts the methods correctly and send it to me? Share this post Link to post
Liz 0 Posted October 11 19 hours ago, Remy Lebeau said: I'll see if the problem exists in the latest Delphi. If the problem continues, please file a bug report with Embarcadero about Java2Op still being broken. You might need to just fix the generated code manually. Or look around to see if there is a 3rd party importer that works better than Java2Op. I probe it today with esp32 and it works. Thank you. The problem with static functions and all that, i just copy from JUsbManagerClass to JUsbManager and it works. I just need now how work withe send data and receive in pascal. these are the instructions of the library in java but how i convert to delphi: then use direct read/write port.write(request, WRITE_WAIT_MILLIS); len = port.read(response, READ_WAIT_MILLIS); or direct write + event driven read: usbIoManager = new SerialInputOutputManager(usbSerialPort, this); usbIoManager.start(); ... port.write("hello".getBytes(), WRITE_WAIT_MILLIS); @Override public void onNewData(byte[] data) { runOnUiThread(() -> { textView.append(new String(data)); }); } Share this post Link to post
Remy Lebeau 1394 Posted October 11 On 10/10/2024 at 11:19 AM, Remy Lebeau said: Or look around to see if there is a 3rd party importer that works better than Java2Op. This one was just brought to my attention: https://github.com/DelphiWorlds/JID "JID was created primarily as an alternative to Java2OP that ships with Delphi, in order to address some shortcomings in Java2OP." Share this post Link to post