B4J Library jSerial library

Discussion in 'B4J Libraries & Classes' started by Erel, Nov 21, 2013.

Thread Status:
Not open for further replies.
  1. Erel

    Erel Administrator Staff Member Licensed User

    The jSerial library allows you to open and communicate with other devices through the computer COM ports.

    It can also be used to communicate with Bluetooth devices over a virtual com port.
    (jBluetooth library is now available: http://www.b4x.com/android/forum/threads/jbluetooth-library.60184/)

    [​IMG]

    The attached examples implement an Android <-> PC chat app.

    In order to run the example you need to first pair the PC and the device and make sure that the Bluetooth is mapped to a virtual com port.

    jSerial works with AsyncStreams. Both standard mode and prefix mode are supported. Note that prefix mode can only work if both sides of the connection follow the protocol.
    Change AStream.InitializePrefix to AStream.Initialize to disable prefix mode.

    jSerial depends on an open source project named JSSC: http://code.google.com/p/java-simple-serial-connector/ (the jar file is included in the zip file).
     

    Attached Files:

    Last edited: Nov 9, 2015
    red30, percyverance, yontala and 5 others like this.
  2. KitCarlson

    KitCarlson Active Member Licensed User

    Excellent! Just what I need.
     
  3. ellpopeb4a

    ellpopeb4a Member Licensed User

    sorry where is it the serial lib in JShell.zip?
    :(
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Fixed...
     
  5. ellpopeb4a

    ellpopeb4a Member Licensed User

    Nice.. the CONNECTION works well
    Thanks
     
  6. Chris_Moulding

    Chris_Moulding Member Licensed User

    Hi,

    Does the jserial library work with USB ports? I've searched for documentation on the jserial library but I've not found it yet.

    Regards,

    Chris
     
  7. Chris_Moulding

    Chris_Moulding Member Licensed User

    WILLIAM S. BERALDO likes this.
  8. Erel

    Erel Administrator Staff Member Licensed User

    jSerial library works with COM ports. If your USB device can map itself as a virtual COM port then you can use it with this library.
     
  9. kris_audi

    kris_audi Member Licensed User

    Hi,
    How I can set param COM port (Even parity)?

    Private sp As Serial
    sp.Open("")
    sp.SetParams(9600,8,1,?)
     
  10. marco.canta

    marco.canta Active Member Licensed User

    How I can set param COM port (NONE parity) ?

    I use ... 2400, 8 ,none,1

    where can I find an example to get data?
    I need to receive data from Arduino


    Thanks
     
  11. agraham

    agraham Expert Licensed User

    PARITY_NONE = 0
    PARITY_ODD = 1
    PARITY_EVEN = 2
    PARITY_MARK = 3
    PARITY_SPACE = 4

    Use AsyncStreams to send/receive data. Examples are in the first post.
     
    DavideV likes this.
  12. kris_audi

    kris_audi Member Licensed User

    Hi.

    When I close the app I receive the mistake:

    Program started.
    jssc.SerialPortException: Port name - COM1; Method name - getInputBufferBytesCount(); Exception type - Port not opened.
    at jssc.SerialPort.checkPortOpened(SerialPort.java:862)
    at jssc.SerialPort.getInputBufferBytesCount(SerialPort.java:786)
    at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:110)
    at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:105)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.run(AsyncStreams.java:184)
    at java.lang.Thread.run(Thread.java:722)
    java.lang.RuntimeException: jssc.SerialPortException: Port name - COM1; Method name - getInputBufferBytesCount(); Exception type - Port not opened.
    at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:118)
    at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:105)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.run(AsyncStreams.java:184)
    at java.lang.Thread.run(Thread.java:722)
    Caused by: jssc.SerialPortException: Port name - COM1; Method name - getInputBufferBytesCount(); Exception type - Port not opened.
    at jssc.SerialPort.checkPortOpened(SerialPort.java:862)
    at jssc.SerialPort.getInputBufferBytesCount(SerialPort.java:786)
    at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:110)
    ... 3 more
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    You should close AsyncStreams in MainForm_Close event.
     
  14. kris_audi

    kris_audi Member Licensed User

    Hi.

    I use astream.Close in MainForm_Close event and I receive the mistake:

    Program started.
    java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:111)
    at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:105)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.run(AsyncStreams.java:184)
    at java.lang.Thread.run(Thread.java:722)
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    Can you upload your project (File - Export as zip)?
     
  16. kris_audi

    kris_audi Member Licensed User

    Yes.
     

    Attached Files:

  17. Erel

    Erel Administrator Staff Member Licensed User

    You should first close AsyncStream and then the serial port:
    Code:
    Sub btnClose_Action
       astream.Close
       sp.Close
       btnOpen.Enabled = 
    True
       btnClose.Enabled = 
    False
    End Sub
     
  18. kris_audi

    kris_audi Member Licensed User

    I changed astream.Close and sp.Close in
    btnClose_Action
    but I have still mistake.
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    If you only see the InterruptedException message in the logs then you can ignore it.
     
  20. kris_audi

    kris_audi Member Licensed User

    Thank you Erel.
     
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice