B4J Question I2c problem

HARRY

Active Member
Licensed User
Longtime User
Hi,
I am trying to use a RTC DS1340 module on my Raspberry. As a starting point I use the code Erel supplied with the jPi4J library in post 3.

I checked using Terminal commands that the device is working correctly. The hwclock -r command gives the correct date and time.

Up till now the program looks as follows:
B4X:
'Non-UI application (console / server application)
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Dim bus1 As JavaObject = GetBus(1)
    Dim device1 As JavaObject = GetDevice(bus1, 68)
    Dim binair As Byte
    Dim bcd As Byte
    Dim data() As Byte
    data=Array As Byte(0x31,0x32,0x33,0x34,0x35,0x36,0x37)
    Dim startpoint As Int
    Dim number As Int
End Sub

Sub AppStart (Args() As String)
    Log("Program started")
    startpoint=0
    number=7
    Write(device1, 0)  'used to set the register to read from
    Read2(device1,data,startpoint,number)
    StartMessageLoop       
End Sub
'Bus methods
Sub GetBus(BusNumber As Int) As JavaObject
       Dim factory As JavaObject
      Return  factory.InitializeStatic("com.pi4j.io.i2c.I2CFactory").RunMethodJO("getInstance", Array As Object(BusNumber))
End Sub

Sub GetDevice(bus As JavaObject, Address As Int) As JavaObject
       Return bus1.RunMethodJO("getDevice", Array As Object(Address))
End Sub

Sub CloseBus (bus As JavaObject)
   bus1.RunMethod("close", Null)
End Sub

'Device methods
Sub Read(device As JavaObject) As Int
   Return device1.RunMethod("read", Null)
End Sub

Sub Read2 (device As JavaObject, Buffer() As Byte, Offset As Int, Size As Int) As Int
   Return device1.RunMethod("read", Array As Object(Buffer, Offset, Size))
End Sub

Sub Write(device As JavaObject, b As Byte)
   device1.RunMethod("write", Array As Object(b))
End Sub

Sub Write2(device As JavaObject, Buffer() As Byte, Offset As Int, Size As Int)
   device1.RunMethod("write", Array As Object(Buffer, Offset, Size))
End Sub
Sub bin2bcd(val As Byte)
    bcd=val+6*(val/10)
End Sub
Sub bcd2bin(val As Byte)
    binair=val-6*(Bit.UnsignedShiftRight(val,4))
End Sub

The libraries used are:
JavaObject
jCore
jPi4J
jSerial

When I run the progtram I get the following log:

Program started.
Program started
Error occurred on line: 51 (Main)
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:130)
at b4j.example.main._write(main.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:591)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:224)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:158)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMetho
dAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.main(main.java:29)
Caused by: java.io.IOException: Error writing to /dev/i2c-1 at address 0x44. Got -20001.
at com.pi4j.io.i2c.impl.I2CDeviceImpl.write(I2CDeviceImpl.java:79)
... 21 more

Some questions:

1) Does somebody know what I am doing wrong?
2) How can I check whether GetBus and GetDevice are successfull?
3) Were can I find what the various methods really do?

I know that there are other means to configure this device. But this device is only used for testing purposes to understand i2c, as the final i2c device, a voltage/ current meter, has not yet arrived.

Harry
 

Roycefer

Well-Known Member
Licensed User
Longtime User
The Exception is not being thrown at GetBus() or GetDevice() (though they might not be working as expected). The Exception is being thrown in your Write() method.
 
Upvote 0

HARRY

Active Member
Licensed User
Longtime User
Hi Roycefer,

I understand that the exeption is thrown in Write() method, but why?

Is it posssible that the OS blocks accesss because it is using the device itself?

Harry
 
Upvote 0

HARRY

Active Member
Licensed User
Longtime User
1) B4J is started as sudo java -jar etc
2) = GetBus(1)Dim device1 AsJavaObject and = GetDevice(bus1, 68) moved to AppStart
The beginning of the program looks like:
B4X:
Sub Process_Globals
    Dim bus1 As JavaObject
    Dim device1 As JavaObject
    Dim binair As Byte
    Dim bcd As Byte
    Dim data() As Byte
    data=Array As Byte(0x31,0x32,0x33,0x34,0x35,0x36,0x37)
    Dim startpoint As Int
    Dim number As Int
End Sub

Sub AppStart (Args() As String)
    Log("Program started")
    bus1=GetBus(1)
    device1=GetDevice(bus1,104)
    startpoint=0
    number=7
    Write(device1, 0)
    Read2(device1,data,startpoint,number)
    StartMessageLoop       
End Sub

The device number is changed from 68 to 104; 68 was a mistake of my( hex vs int)

3) I tried bus 0: other errors already on the GetBus sub. In the declaration via the Terminal I used also 1 and it works. Bus 1 should be used on a Raspberry Pi 2, which I have. This I learned on internet.
 
Upvote 0

HARRY

Active Member
Licensed User
Longtime User
The problem is solved. After rebooting the Raspberry and not starting the RTC module in the Raspberry, access of the RTC module via B4JV works fine. It seems that the OS indeed blocks further access when the device is used by the RTC software in the Raspberry. May be that this is a layman's explanation, but for me it is satisfying.

Harry
 
Upvote 0

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi

I have a raspberry pi2b with debian/jessie. Why i just see a i2c-1 in my PI /dev ? I try i2cdetect in it. i see three device 0x57,UU(0x18),UU(0x6f). 0x57 connect with i2c-0. 0x18,0x6f connect with i2c-1. Why i have i2c-1 no i2c-0 in /dev ?
 
Upvote 0
Top