B4J Question Get Data From COM Port

Hello
I want to connect Digital weight indicator to pc and should get data from port COM (RS232).
Is there any library or any code that I can do this with it and get the weight indicator information?
 
Last edited:

emexes

Expert
Licensed User
Try this for a start. Modify the COM port and baud rate to suit your setup, then post us a sample of the logged serial data sent by the weight indicator.
B4X:
Sub Process_Globals
    Private sp As Serial
    Private astream As AsyncStreams
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    
    sp.Initialize("sp")
    
    Log(sp.ListPorts)
    
    sp.Open("COM11")    '*** COM PORT ***
    sp.SetParams(115200, 8, 1, 0)    '*** BAUD RATE ***
    
    astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
    
End Sub

Sub AStream_NewData (Buffer() As Byte)
    
    Dim LogHeader As String = DateTime.Now & " " & Buffer.Length & " :"
    
    Dim LogBytes As String = ""
    For I = 0 To Buffer.Length - 1
        LogBytes = LogBytes & " " & Buffer(I))
    Next
    Log(LogHeader & LogBytes)
    
    Dim LogString As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log(LogHeader & " [" LogString & "]")
    
End Sub
 
Upvote 0

emexes

Expert
Licensed User
There is a faint chance that your weight indicator might not send data until it's requested. In which case: what's the manufacturer and model of the weight indicator, and do you have any documentation on how to talk to it?

Ideally it would just send the weight automatically, eg say 5 times every second, and it'd be in readable ASCII eg 2718 CR for 2.718 kgf.
 
Upvote 0
Try this for a start. Modify the COM port and baud rate to suit your setup, then post us a sample of the logged serial data sent by the weight indicator.
B4X:
Sub Process_Globals
    Private sp As Serial
    Private astream As AsyncStreams
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   
    sp.Initialize("sp")
   
    Log(sp.ListPorts)
   
    sp.Open("COM11")    '*** COM PORT ***
    sp.SetParams(115200, 8, 1, 0)    '*** BAUD RATE ***
   
    astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
   
End Sub

Sub AStream_NewData (Buffer() As Byte)
   
    Dim LogHeader As String = DateTime.Now & " " & Buffer.Length & " :"
   
    Dim LogBytes As String = ""
    For I = 0 To Buffer.Length - 1
        LogBytes = LogBytes & " " & Buffer(I))
    Next
    Log(LogHeader & LogBytes)
   
    Dim LogString As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log(LogHeader & " [" LogString & "]")
   
End Sub

Thanks so much it worked.
 
Upvote 0

emexes

Expert
Licensed User
Thanks so much it worked.
Super! Just out of interest, and to help anyone else here looking to talk with similar devices over a serial port, could you post the make and model of the device, and a sample of the serial data received when a weight is applied... eg, whatever you would have found useful to know before rather than after. šŸ»
 
Upvote 0
Top