Android Question How to read and write data from multiple UART_USB (CH340 two ports)

Diego Marcelo Croatto

Member
Licensed User
Hi to all.... I'm trying to read and write data from two USB Uarts (CH340) I'm using felUSBSerial(v1.12)

I have been able to connect of each port but when i receive data I cant identify from what device is incomming the data.
serial_DataAvailable:
Private Sub serial_DataAvailable (Buffer() As Byte)
This function receive the data from the two ports at the same time.... How I can do the identification of the data incomming from each port and prevent the colition of this data.

Thank's a Lot!!!!
 

DonManfred

Expert
Licensed User
Longtime User
Use two Serial Instances and listen to one port each?
 
Upvote 0

Diego Marcelo Croatto

Member
Licensed User
Use two instances. Give every instance a new EventPrefix (serial1, serial2)
Use two Event Subs
B4X:
Private Sub serial1_DataAvailable (Buffer() As Byte)
Private Sub serial2_DataAvailable (Buffer() As Byte)


I create this code for open each port with:

device_0 and device_1 as each UsbDevice

usbserial_0 and usbserial_1 as each UsbSerial

but I dont understand what is the event of each port

Globals:
    Private manager As UsbManager
    
    Private device_0 As UsbDevice
    Private device_1 As UsbDevice
    
    Private usbserial_0 As felUsbSerial
    Private usbserial_1 As felUsbSerial

    Private felAST_0 As felAsyncStreamsText
    Private felAST_1 As felAsyncStreamsText


OpenPorts:
    '******************************************************************
    ' Initialize the USB Ports
    '******************************************************************

    felAST_0.Initialize(Me,"felAST")
    felAST_1.Initialize(Me,"felAST")
    
    Dim Tmp,Tmp1 As Int
        
    If manager.GetDevices.Length = 0 Then
        Log("No connected usb devices.")
    Else

        device_0= manager.GetDevices(CH340_Port_0) 'first Channel
        device_1= manager.GetDevices(CH340_Port_1) 'second Channel
            
        If manager.HasPermission(device_0) = False Then
            ToastMessageShow("Please allow connection and click again.", True)
            manager.RequestPermission(device_0)
        Else
            usbserial_0.Initialize("serial", device_0, -1)
            usbserial_0.BaudRate = 9600
            usbserial_0.DataBits = usbserial_0.DATA_BITS_8
            usbserial_0.StartReading
        End If

        If manager.HasPermission(device_1) = False Then
            ToastMessageShow("Please allow connection and click again.", True)
            manager.RequestPermission(device_1)
        Else
            usbserial_1.Initialize("serial", device_1, -1)
            usbserial_1.BaudRate = 9600
            usbserial_1.DataBits = usbserial_1.DATA_BITS_8
            usbserial_1.StartReading
        End If
        
    End If
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
For THIS prefix you need to use similar event subs
B4X:
Private Sub FelAST1_DataAvailable (Buffer() As Byte)
Private Sub FelAST2_DataAvailable (Buffer() As Byte)

PD: I strongly suggest to read the Documentation, watch some Videos to learn the basics.
 
Upvote 0

Diego Marcelo Croatto

Member
Licensed User
Don .... Thank's a lot... I have discovered the event!!!!

felUsbSerial:
usbserial_0.Initialize("serial_0", device_0, -1)    'Port 0'
usbserial_1.Initialize("serial_1", device_1, -1)    'Port 1'
"serial_0" and "serial_1 is the event! ...☝
 
Upvote 0
Top