共享者既是.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收到的每个序列命令。
继续,我保证...
我有一些想法可以扩展共享功能,例如:
如果您有兴趣帮助我进行共享者开发,我很乐意收到功能请求。也欢迎叉子;)