共享者既是.NET和Arduino庫。它允許桌面應用程序使用Sharer Prodocole累積串行通信,在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個存儲庫,一個用於Arduino來源,另一個用於.NET來源
在菜單Tools/Manage Libraries...只需尋找共享者並安裝最新版本即可。
請下載共享者庫存檔: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 。
nuget上可用:https://www.nuget.org/packages/sharer/
使用此命令行將其與您的軟件包管理器一起安裝:
Install-Package Sharer
可以在此處下載Sharer.dll組件:https://github.com/rufus31415/sharer.net/ReleaSes/latest/download/download/sharerasssemblies.zip
該檔案包含Nuget軟件包Sharer.nupkg和Sharer.dll在AnyCPU版本中彙編的以下目標:
Windows表單示例需要.NET Framework 3.5。它可以在此處下載:https://github.com/rufus31415/sharer.net/releases/latest/download/sharerwindowsformsexample.zip
控制台示例以.NET Core 3.0運行。但是您不需要任何運行時執行它。獨立的控制台示例可在此處找到:
©RUFUS31415有關詳細信息,請參見許可證文件。
標題文件<sharer.h>應包括在內。
在函數setup()中,通過傳遞baudrate來初始化共享者來函數Sharer.init(...) 。它在內部稱為Serial.init()與同一baudrate。
在函數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);
}要在共享變量列表中添加變量,您應該調用宏Sharer_ShareVariable 。它的第一個論點是變量的類型,第二個是其名稱。該宏使共享者可以將指針指向變量,其名稱,類型和內存大小。
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 ]);
}要在共享功能列表中添加變量,您應該調用宏Sharer_ShareFunction 。它的第一個論點是返回的類型,第二個是其名稱。按照宏的參數來描述共享函數的類型和名稱的參數。您可以共享的參數數量沒有限制,但是所有參數都應共享。
不支持類方法共享,共享功能必須是免費功能(非成員靜態功能)。
您可以共享自己的功能,但是所有系統功能都可以使用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函數是沒有返回類型的函數。您應該致電Macro Sharer_ShareVoid共享一個void函數。第一個參數是其名稱,其次是每個參數的類型和名稱。
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波特(Bauds)上的Arduino Uno(命令 +響應)上需要少於10毫秒。優化了原核,以免字符串解碼,只是一個字節來解釋的二進制流。通過使用f()宏和progmem將可變,功能和參數的名稱存儲在flash中的名稱中,已通過使用f()宏和progmem來優化內存足跡。
共享者使用一個獨特的原始型原核,稱為Sharer Protocole。解釋了Arduino收到的每個序列命令。
繼續,我保證...
我有一些想法可以擴展共享功能,例如:
如果您有興趣幫助我進行共享者開發,我很樂意收到功能請求。也歡迎叉子;)