|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
public class ofSerial
|
|
|
|
|
|
{
|
|
|
|
|
|
public ofSerial()
|
|
|
|
|
|
{
|
|
|
|
|
|
id = createNative();
|
|
|
|
|
|
}
|
|
|
|
|
|
~ofSerial()
|
|
|
|
|
|
{
|
|
|
|
|
|
releaseNative(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
public bool setup()
|
|
|
|
|
|
{
|
|
|
|
|
|
return setupNative(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
public bool setup(string portName, int baudrate)
|
|
|
|
|
|
{
|
|
|
|
|
|
return setupWithPortNameNative(id, portName, baudrate);
|
|
|
|
|
|
}
|
|
|
|
|
|
public bool setup(int deviceNumber, int baudrate)
|
|
|
|
|
|
{
|
|
|
|
|
|
return setupWithDeviceNumberNative(id, deviceNumber, baudrate);
|
|
|
|
|
|
}
|
|
|
|
|
|
public bool isInitialized()
|
|
|
|
|
|
{
|
|
|
|
|
|
return isInitializedNative(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
public void close()
|
|
|
|
|
|
{
|
|
|
|
|
|
closeNative(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
public int available()
|
|
|
|
|
|
{
|
|
|
|
|
|
return availableNative(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
public int readBytes(byte[] buffer)
|
|
|
|
|
|
{
|
|
|
|
|
|
return readBytes(buffer, 0, buffer.Length);
|
|
|
|
|
|
}
|
|
|
|
|
|
public unsafe int readBytes(byte[] buffer, int offset, int length)
|
|
|
|
|
|
{
|
|
|
|
|
|
fixed (byte* p = buffer)
|
|
|
|
|
|
{
|
|
|
|
|
|
IntPtr ptr = (IntPtr)(p+ offset);
|
|
|
|
|
|
// do you stuff here
|
|
|
|
|
|
return readBytesNative(id, ptr, length);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public int readByte()
|
|
|
|
|
|
{
|
|
|
|
|
|
return readByteNative(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
public int writeBytes(byte[] buffer)
|
|
|
|
|
|
{
|
|
|
|
|
|
return writeBytes(buffer, 0, buffer.Length);
|
|
|
|
|
|
}
|
|
|
|
|
|
public unsafe int writeBytes(byte[] buffer,int offset, int length)
|
|
|
|
|
|
{
|
|
|
|
|
|
fixed (byte* p = buffer)
|
|
|
|
|
|
{
|
|
|
|
|
|
IntPtr ptr = (IntPtr)(p+ offset);
|
|
|
|
|
|
// do you stuff here
|
|
|
|
|
|
return writeBytesNative(id, ptr, length);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public bool writeByte(byte singleByte)
|
|
|
|
|
|
{
|
|
|
|
|
|
return writeByteNative(id, singleByte);
|
|
|
|
|
|
}
|
|
|
|
|
|
public void flush(bool flushIn = true, bool flushOut = true)
|
|
|
|
|
|
{
|
|
|
|
|
|
flushNative(id, flushIn, flushOut);
|
|
|
|
|
|
}
|
|
|
|
|
|
public void drain()
|
|
|
|
|
|
{
|
|
|
|
|
|
drainNative(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
#region ofSerial.dll
|
|
|
|
|
|
[DllImport("ofSerial")]
|
|
|
|
|
|
public static extern uint createNative();
|
|
|
|
|
|
|
|
|
|
|
|
[DllImport("ofSerial")]
|
|
|
|
|
|
public static extern void releaseNative(uint id);
|
|
|
|
|
|
//void listDevicesNative(uint id);
|
|
|
|
|
|
//vector<ofSerialDeviceInfo> getDeviceList();
|
|
|
|
|
|
|
|
|
|
|
|
[DllImport("ofSerial")]
|
|
|
|
|
|
public static extern bool setupNative(uint id);
|
|
|
|
|
|
[DllImport("ofSerial")]
|
|
|
|
|
|
public static extern bool setupWithPortNameNative(uint id, [MarshalAs(UnmanagedType.LPStr)]string portName, int baudrate);
|
|
|
|
|
|
[DllImport("ofSerial")]
|
|
|
|
|
|
public static extern bool setupWithDeviceNumberNative(uint id, int deviceNumber, int baudrate);
|
|
|
|
|
|
[DllImport("ofSerial")]
|
|
|
|
|
|
public static extern bool isInitializedNative(uint id);
|
|
|
|
|
|
[DllImport("ofSerial")]
|
|
|
|
|
|
public static extern void closeNative(uint id);
|
|
|
|
|
|
[DllImport("ofSerial")]
|
|
|
|
|
|
public static extern int availableNative(uint id);
|
|
|
|
|
|
[DllImport("ofSerial")]
|
|
|
|
|
|
public static extern int readBytesNative(uint id, IntPtr buffer, int length);
|
|
|
|
|
|
[DllImport("ofSerial")]
|
|
|
|
|
|
public static extern int readByteNative(uint id);
|
|
|
|
|
|
[DllImport("ofSerial")]
|
|
|
|
|
|
public static extern int writeBytesNative(uint id, IntPtr buffer, int length);
|
|
|
|
|
|
[DllImport("ofSerial")]
|
|
|
|
|
|
public static extern bool writeByteNative(uint id, byte singleByte);
|
|
|
|
|
|
[DllImport("ofSerial")]
|
|
|
|
|
|
public static extern void flushNative(uint id, bool flushIn = true, bool flushOut = true);
|
|
|
|
|
|
[DllImport("ofSerial")]
|
|
|
|
|
|
public static extern void drainNative(uint id);
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
private uint id = 0;
|
|
|
|
|
|
|
|
|
|
|
|
#if false
|
|
|
|
|
|
ofSerial();
|
|
|
|
|
|
virtual ~ofSerial();
|
|
|
|
|
|
void listDevices();
|
|
|
|
|
|
vector<ofSerialDeviceInfo> getDeviceList();
|
|
|
|
|
|
bool setup();
|
|
|
|
|
|
bool setup(string portName, int baudrate);
|
|
|
|
|
|
bool setup(int deviceNumber, int baudrate);
|
|
|
|
|
|
bool isInitialized() const;
|
|
|
|
|
|
void close();
|
|
|
|
|
|
int available();
|
|
|
|
|
|
int readBytes(unsigned char* buffer, int length);
|
|
|
|
|
|
int readByte();
|
|
|
|
|
|
int writeBytes(unsigned char* buffer, int length);
|
|
|
|
|
|
bool writeByte(unsigned char singleByte);
|
|
|
|
|
|
void flush(bool flushIn = true, bool flushOut = true);
|
|
|
|
|
|
void drain();
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|