I have an Android tablet with RFID that I'm trying to read data from proximity cards. The vendor provided a so
file for opening the serial port but I have issue reading it with C#.
If I use the following code I can successfully read from the serial port:
[DllImport("serial_port", EntryPoint = "Java_android_1serialport_1api_SerialPort_open")]
public static extern SafeFileHandle Open(IntPtr env, IntPtr thiz, IntPtr path, int baudrate, int flags);
var device = new Java.IO.File("/dev/ttyS9");
var absPath = new String(device.AbsolutePath);
var open = Open(JNIEnv.Handle, IntPtr.Zero, absPath.Handle, baudRate, 0);
var fd = Object.GetObject<FileDescriptor>(open, JniHandleOwnership.DoNotRegister);
fileInputStream = new FileInputStream(fd);
var result = new byte[64];
fileInputStream.Read(result, 0, result.Length);
But if I instead try to create a FileStream
object so that I can use it in the shared project I get an error:
var device = new Java.IO.File("/dev/ttyS9");
var absPath = new String(device.AbsolutePath);
var open = Open(JNIEnv.Handle, IntPtr.Zero, absPath.Handle, baudRate, 0);
var fileStream = new FileStream(open, FileAccess.Read);
var data = new byte[64];
var length = fileStream.Read(data, 0, data.Length);
The call to fileStream.Read
throws the following exception:
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.UnauthorizedAccessException: Access to the path is denied.
[mono-rt] ---> System.IO.IOException: Bad file descriptor
[mono-rt] --- End of inner exception stack trace ---
[mono-rt] at System.IO.Strategies.FileStreamHelpers.CheckFileCall(Int64 result, String path, Boolean ignoreNotSupported)
[mono-rt] at System.IO.RandomAccess.ReadAtOffset(SafeFileHandle handle, Span`1 buffer, Int64 fileOffset)
[mono-rt] at System.IO.Strategies.OSFileStreamStrategy.Read(Span`1 buffer)
[mono-rt] at System.IO.Strategies.OSFileStreamStrategy.Read(Byte[] buffer, Int32 offset, Int32 count)
[mono-rt] at System.IO.Strategies.BufferedFileStreamStrategy.ReadSpan(Span`1 destination, ArraySegment`1 arraySegment)
[mono-rt] at System.IO.Strategies.BufferedFileStreamStrategy.Read(Byte[] buffer, Int32 offset, Int32 count)
[mono-rt] at System.IO.FileStream.Read(Byte[] buffer, Int32 offset, Int32 count)
If I can read from the stream with Java classes, why can't I read it from BCL FileStream class? Is it a bug or am I doing anything wrong?