B4J Question What does this error mean - InterruptedException: sleep ...

Mark Read

Well-Known Member
Licensed User
Longtime User
My app is nearly finished. I am testing on a windows PC with a serial loopback plug (pins 2 & 3 shorted).

If I click connect, I see the connection but if I click connect (disconnect) again, I get this message. The app continues to work but I dont understand the error.

Connect button code:
B4X:
Sub btn_Connect_MouseClicked (EventData As MouseEvent)
    If ComPortOpen=False Then
        ComPortOpen=True
        myserial.Initialize("")
        uart_list = myserial.ListPorts
        myserial.Open(uart_list.Get(0))
        myserial.SetParams(9600,8,1,0)
        btn_Connect.Text="Disconnect"
        lbl_Connected.Style="-fx-background-color: green"
        astream.Initialize(myserial.GetInputStream,myserial.GetOutputStream,"astream")
        Log("Com Port Initialized:" & uart_list.Get(0))
        Log("I have sent: <STX>Q, 229, 002.74, M, 00, <ETX> 16")
        astream.Write("<STX>Q, 229, 002.74, M, 00, <ETX> 16".GetBytes("UTF8"))
    Else
        ComPortOpen=False
        myserial.Close
        btn_Connect.Text="Connect"
        lbl_Connected.Style="-fx-background-color: red"
        astream.Close
    End If
End Sub

Sub astream_NewData (Buffer() As Byte)
  rcvStr = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
  sndStr = "I have received " & rcvStr & Chr(13) & Chr(10)
  Log(sndStr)
  'astream.Write(sndStr.GetBytes("UTF8"))
End Sub

And the log info:

B4X:
Program started.
D:\BASIC4~2\MYAPPS~1\test\Objects\windsonic\
Com Port Initialized:COM1
I have sent: <STX>Q, 229, 002.74, M, 00, <ETX> 16
I have received <STX>Q, 229, 002.74, M, 00, <ETX> 16
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:122)
    at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:116)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.run(AsyncStreams.java:184)
    at java.lang.Thread.run(Thread.java:745)
Com Port Initialized:COM1
I have sent: <STX>Q, 229, 002.74, M, 00, <ETX> 16
I have received <STX>Q, 229, 002.74, M, 00, <ETX> 16
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:122)
    at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:116)
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.run(AsyncStreams.java:184)
    at java.lang.Thread.run(Thread.java:745)
 

Mark Read

Well-Known Member
Licensed User
Longtime User
Close AsyncStreams before you close the serial connection.

B4X:
Else
        ComPortOpen=False
        astream.Close
        myserial.Close
        btn_Connect.Text="Connect"
        lbl_Connected.Style="-fx-background-color: red"
        
    End If

Moved astream.close to before myserial.close but it makes no difference, error is still there. It cannot be critical as the app works?
 
Upvote 0
Top