共有者は.NETとArduinoライブラリの両方です。デスクトップアプリケーションは、シェアリアル通信を使用して、Arduinoの変数とリモートコール関数の読み取り/書き込み関数を使用できます。共有者は当初、Ballcuberプロジェクト(https://ballcuber.github.io)のために開発されましたが、現在はスタンドアロンライブラリです;)。
// C#
var connection = new SharerConnection ( "COM3" , 115200 ) ;
connection . Connect ( ) ;注:MicroやLeonardoなどの一部のボードでは、RTSとDTR信号をRtsEnableおよびDtrEnableプロパティで設定する必要があります。
// C# - My Arduino code has a function : int Sum(int a, byte b);
var result = connection . Call ( "Sum" , 10 , 12 ) ;
// result.Status : OK
// result.Type : int
// result.Value : 22 // C# - Read all digital pins : digitalRead(0) to digitalRead(13)
for ( int i = 0 ; i <= 13 ; i ++ ) {
var digitalValue = connection . Call ( "digitalRead" , i ) ;
// digitalValue.Status : OK
// digitalValue.Type : bool
// digitalValue.Value : true or false
} // C# - Definition of all functions
var functions = connection . Functions ;
// functions[0].Name : function name
// functions[0].ReturnType : enum void, int, long, ...
// functions[0].Arguments[0].Name // Name of first argument
// functions[0].Arguments[0].Type // enum void, int, long, ... // C#
connection . WriteVariable ( "myVar" , 15 ) ; // returns true if writting OK // C# - Write simultaneously several variables by passing a List<WriteVariable>()
connection . WriteVariables ( listOfVariablesToWrite ) ; // C# - Definition of all variables
var variables = connection . Variables ;
// variables[0].Name : variable name
// variables[0].Type : enum int, long, ... // C#
var value = connection . ReadVariable ( "myVar" ) ;
// value.Status : OK
// value.Value : 12 // C# - Read simultaneously several variables
var values = connection . ReadVariables ( new string [ ] { "myVar" , "anotherVar" , "yetAnother" } ) ; // C#
var info = connection . GetInfos ( ) ;
// info.Board : Arduino UNO
// info.CPUFrequency : 16000000 (Hz)
// a lot more : info.CPlusPlusVersion, info.FunctionMaxCount, info.VariableMaxCount, ...WriteUserData関数を使用して、シリアルポートでクラシックメッセージを送信および受信できます。また、Arduinoによってデータが送信されると、 UserDataReceivedイベントが発生します。
警告: UserDataReceivedイベントハンドラーの変数を読み取りまたは書き込み、関数を呼び出すことはできません。
// C#
connection . WriteUserData ( "Hello!" ) ;
connection . WriteUserData ( 12.2 ) ;
connection . WriteUserData ( new byte [ ] { 0x12 , 0x25 , 0xFF } ) ;
// Event raised when new user data sent by Arduino
connection . UserDataReceived += UserDataReceived ; // C++ Arduino
# include < Sharer.h >
// A simple function that sums an integer and a byte and return an integer
int Sum ( int a, byte b) {
return a + b;
}
// a simple integer
int myVar = 25 ;
void setup () {
Sharer. init ( 115200 ); // Init Serial communication with 115200 bauds
// Expose this function to Sharer : int Sum(int a, byte b)
Sharer_ShareFunction ( int , Sum, int , a, byte, b);
// Share system functions
Sharer_ShareFunction ( bool , digitalRead, uint8_t , pin);
Sharer_ShareVoid (pinMode, uint8_t , pin);
// Expose this variable to Sharer so that the desktop application can read/write it
Sharer_ShareVariable ( int , myVar);
}
// Run Sharer engine in the main Loop
void loop () {
Sharer. run ();
}共有者は2つのリポジトリに分かれています。1つはArduinoソース用、もう1つは.NETソース用です。
Sharerは、メニューTools/Manage Libraries...で利用可能なライブラリマネージャーで入手できます。共有者を探して、最新バージョンをインストールしてください。
Sharer Library Archive:https://github.com/rufus31415/sharer/releases/latest/download/sharer.zipをダウンロードしてください
共有者は、Arduino Uno、Nano、Mega、およびDueでテストされています。他のボードで動作する場合があります。抽出して、Arduinoの「ライブラリ」ディレクトリに共有ディレクトリを取得するようにしてください: C:Program Files (x86)ArduinolibrariesSharer
その後、Arduino IDEを再起動してメニューFile / examples / Sharerに移動することで、例を楽しむことができます。
sharer.netはnugetで入手できます:https://www.nuget.org/packages/sharer/
このコマンドラインを使用して、パッケージマネージャーと一緒にインストールしてください。
Install-Package Sharer
sharer.dllアセンブリは、https://github.com/rufus31415/sharer.net/releases/latest/download/sharerasssemblies.zip.zipからダウンロードできます
このアーカイブには、NugetパッケージSHARER.NUPKGとSHARER.DLLが含まれています。
Windowsフォームの例では、.NETフレームワーク3.5が必要です。ここからダウンロードできます:https://github.com/rufus31415/sharer.net/releases/latest/download/sharerwindowsformsexample.zip
コンソールの例は、.NET Core 3.0で実行されます。ただし、実行するのに実行時間は必要ありません。スタンドアロンコンソールの例はこちらから入手できます。
©Rufus31415詳細については、ライセンスファイルを参照してください。
ヘッダーファイル<sharer.h>を含める必要があります。
function setup()では、共有者がSharer.init(...)を機能させるためにボーレートを渡すことにより、共有者が初期化されます。同じボードレートでSerial.init()内部的に呼び出します。
function loop()では、 Sharer.run()を呼び出す必要があります。受信したコマンドをデコードする共有者の内部カーネルを実行します。
# include < Sharer.h >
void setup () {
Sharer. init ( 115200 );
}
void loop () {
Sharer. run ();
}Serial2を使用してデスクトップアプリケーションと通信する場合、ArduinoのSerial2など、別のストリームで共有者を初期化することもできます。
void setup () {
// Initialize with another Serial interface
Serial2. begin ( 9600 );
Sharer. init (Serial2);
}共有変数リストに変数を追加するには、Macro Sharer_ShareVariableを呼び出す必要があります。その最初の引数は変数のタイプであり、2つ目はその名前です。このマクロにより、共有者は変数、その名前、そのタイプ、およびメモリサイズへのポインターを持つことができます。
byte myByteVar;
long myLongVar;
int myIntArray[ 2 ];
void setup () {
Sharer. init ( 115200 );
Sharer_ShareVariable (byte, myByteVar);
Sharer_ShareVariable ( long , myLongVar);
Sharer_ShareVariable ( int , myIntArray[ 0 ]);
Sharer_ShareVariable ( int , myIntArray[ 1 ]);
}共有関数リストに変数を追加するには、Macro Sharer_ShareFunctionを呼び出す必要があります。その最初の引数は返されたタイプで、2つ目はその名前です。マクロの引数に従って、そのタイプとその名前による共有関数の引数を説明しています。共有できる引数の数に制限はありませんが、すべての引数を共有する必要があります。
クラスのメソッド共有はサポートされておらず、共有関数は自由関数(非会員の静的関数)でなければなりません。
独自の機能を共有できますが、すべてのシステムはAnalogread、DigitalRead、DigitalWrite、Millisなどのような機能を使用できます。
int Sum ( int a, byte b) {
return a + b;
}
void setup () {
Sharer. init ( 115200 );
// Share your function to Sharer : int Sum(int a, byte b)
Sharer_ShareFunction ( int , Sum, int , a, byte, b);
// Sharer system functions
Sharer_ShareFunction ( int , analogRead, uint8_t , pin);
Sharer_ShareFunction ( bool , digitalRead, uint8_t , pin);
}void関数は、返されたタイプのない関数です。 void関数を共有するには、Macro Sharer_ShareVoidを呼び出す必要があります。最初の議論はその名前で、その後に各引数のタイプと名前が続きます。
void TurnLEDOn ( void ) {
pinMode ( 13 , OUTPUT);
digitalWrite ( 13 , true );
}
void SetLEDState ( bool state) {
pinMode ( 13 , OUTPUT);
digitalWrite ( 13 , state);
}
void setup () {
Sharer. init ( 115200 );
// Share your void fuctions
Sharer_ShareVoid (TurnLEDOn);
Sharer_ShareVoid (SetLEDState, bool , state);
// Sharer system void functions
Sharer_ShareVoid (digitalWrite, uint8_t , pin);
}共有クラスはストリームから継承するため、次の機能を使用できます。共有関数内で、sharer.print()、sharer.println()またはsharer.write()を使用しないでください。
void loop () {
Sharer. run ();
// gets the number of bytes available in the stream
int available = Sharer. available ();
// reads last reveided byte (-1 if no data to read)
int lastByte = Sharer. read ();
// Empty the stream
Sharer. flush ();
// reads data from the serial buffer until the target is found
// returns, true if data is found
bool found = Sharer. find ( " string to find " );
// Returns the next byte of incoming serial data without removing it from the internal serial buffer
// Successive calls to peek() will return the same character
int nextByte = Sharer. peek ();
// reads characters from the serial buffer into a String
String str = Sharer. readString ();
// Looks for the next valid integer in the incoming serial
int lastInt = Sharer. parseInt ();
lastInt = Sharer. parseInt (SKIP_ALL); // all characters other than digits or a minus sign are ignored when scanning the stream for an integer. This is the default mode
lastInt = Sharer. parseInt (SKIP_NONE); // nothing is skipped, and the stream is not touched unless the first waiting character is valid.
lastInt = Sharer. parseInt (SKIP_WHITESPACE); // only tabs, spaces, line feeds, and carriage returns are skipped.
// returns the first valid floating point number from the Serial buffer
float lastFloat = Sharer. parseFloat ();
lastFloat = Sharer. parseFloat (SKIP_ALL); // all characters other than a minus sign, decimal point, or digits are ignored when scanning the stream for a floating point number. This is the default mode.
lastFloat = Sharer. parseFloat (SKIP_NONE); // Nothing is skipped, and the stream is not touched unless the first waiting character is valid.
lastFloat = Sharer. parseFloat (SKIP_WHITESPACE); // only tabs, spaces, line feeds, and carriage returns are skipped.
// Prints ASCII encoded string
Sharer. print ( 85 ); // sends the string "85"
Sharer. print ( 1.23456 ); // sends the string "1.23"
Sharer. print ( ' N ' ); // sends the string "N"
Sharer. print ( " Hello world. " ); // sends the string "Hello world."
Sharer. print ( 78 , BIN); // sends the string "1001110"
Sharer. print ( 78 , OCT); // sends the string "116"
Sharer. print ( 78 , DEC); // sends the string "78"
Sharer. print ( 78 , HEX); // sends the string "4E"
Sharer. print ( 1.23456 , 0 ); // sends the string "1"
Sharer. print ( 1.23456 , 2 ); // sends the string "1.23"
Sharer. print ( 1.23456 , 4 ); // sends the string "1.2345"
Sharer. println ( " Hello ;) " ); // send the string and a new line "Hello ;)n"
Sharer. println (); // just sends a new line
// Write a single byte
Sharer. write ( 0x12 );
}ファイルC:Program Files (x86)ArduinolibrariesSharersrcSharerConfig.hの定数を編集することにより、共有者の制限を変更できます。
// maximum number of shared functions
# define _SHARER_MAX_FUNCTION_COUNT 16
// maximum number of shared variables
# define _SHARER_MAX_VARIABLE_COUNT 32
// circle buffer size for user serial message
# define _SHARER_USER_RECEIVE_BUFFER_SIZE 64
// maximum number of call to Serial.read() each time Sharer.Run() is called
# define _SHARER_MAX_BYTE_READ_PER_RUN 1000 ここには、sharer.net:/sharer.net/sharer.net.documentation.mdの完全なドキュメントを見つけることができます。
コマンドは、115200ボーでのArduino UNO(コマンド +応答)で呼び出されるのに10ミリ秒未満かかります。プロトコルは、解読する文字列がないように最適化されています。バイトごとにバイトを解釈するバイナリストリームだけです。メモリフットプリントは、f()マクロとprogmemを使用して、Flashの変数、関数、および引数の名前を保存することにより最適化されています。
共有者は、共有プロトコルと呼ばれるユニークなプロトコルを使用しています。 Arduinoが受け取ったすべてのシリアルコマンドはインタープレーティングされています。
続けるために、私は約束します...
次のような共有機能を拡張するためのアイデアがいくつかあります。
あなたが共有者の開発を手伝ってくれることに興味があるなら、私は喜んで機能のリクエストを受け取ります。フォークも大歓迎です;)