Sharer เป็นทั้ง . NET และ Arduino Library ช่วยให้แอปพลิเคชันเดสก์ท็อป อ่าน/เขียนตัวแปร และ ฟังก์ชั่นการโทรจากระยะไกลบน Arduino โดยใช้ Sharer Protocole เพิ่มการสื่อสารแบบอนุกรม ผู้แบ่งปันได้รับการพัฒนาในขั้นต้นสำหรับโครงการ 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 นอกจากนี้เหตุการณ์ UserDataReceived จะเพิ่มขึ้นเมื่อข้อมูลถูกส่งโดย Arduino
คำเตือน: มันเป็นไปไม่ได้ที่จะอ่านหรือเขียนตัวแปรและฟังก์ชั่นการโทรในตัวจัดการเหตุการณ์ 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
Sharer มีอยู่ใน Library Manager ที่มีอยู่ใน Tools/Manage Libraries... เพียงแค่มองหา Sharer และติดตั้งเวอร์ชันล่าสุด
กรุณาดาวน์โหลด Sharer Library Archive: https://github.com/rufus31415/sharer/releases/latest/download/sharer.zip
ผู้แบ่งปันได้รับการทดสอบกับ Arduino Uno, Nano, Mega และถึงกำหนด มันอาจใช้งานได้กับบอร์ดอื่น ๆ แยกออกเพื่อให้คุณได้รับไดเรกทอรี Sharer ในไดเรกทอรี arduino "ไลบรารี" ของคุณ: C:Program Files (x86)ArduinolibrariesSharer
จากนั้นคุณสามารถเพลิดเพลินกับตัวอย่างด้วยการรีสตาร์ท Arduino IDE ของคุณและไปที่ File / examples / Sharer
sharer.net มีอยู่ใน NUGET: https://www.nuget.org/packages/sharer/
ใช้บรรทัดคำสั่งนี้เพื่อติดตั้งกับตัวจัดการแพ็คเกจของคุณ:
Install-Package Sharer
สามารถดาวน์โหลดชุดแอสเซมบลี dharer.dll ได้ที่นี่: https://github.com/rufus31415/sharer.net/Releases/latest/download/sharerasssemblies.zip
คลังเก็บนี้มี NUGET Package 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);
} ในการเพิ่มตัวแปรในรายการตัวแปรที่ใช้ร่วมกันคุณควรโทรหา Macro 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 ]);
} ในการเพิ่มตัวแปรในรายการฟังก์ชั่นที่ใช้ร่วมกันคุณควรโทรหา Macro 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);
} ฟังก์ชั่นโมฆะเป็นฟังก์ชั่นที่ไม่มีประเภทที่ส่งคืน คุณควรโทรหา 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 สืบทอดมาจากสตรีมดังนั้นคุณสามารถใช้ฟังก์ชั่นต่อไปนี้ โปรดอย่าใช้ sharer.print (), sharer.println () หรือ sharer.write () ภายในฟังก์ชั่นที่ใช้ร่วมกันเขียน woud จะถูกละเว้น
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 );
} คุณสามารถเปลี่ยนขีด จำกัด ของ Sharer ได้โดยการแก้ไขค่าคงที่ของไฟล์ 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
คำสั่งใช้เวลาน้อยกว่า 10ms ในการเรียกใช้ arduino uno (คำสั่ง + การตอบกลับ) ที่ 115200 bauds โปรโตโคลได้รับการปรับให้เหมาะสมเพื่อไม่ให้มีการถอดรหัสเพียงแค่สตรีมไบนารีเพื่อตีความไบต์โดยไบต์ รอยเท้าหน่วยความจำได้รับการปรับให้เหมาะสมโดยใช้มาโคร f () และ progmem เพื่อจัดเก็บชื่อของตัวแปรฟังก์ชั่นและอาร์กิวเมนต์ในแฟลช
Sharer ใช้โปรโตโคลที่ไม่ซ้ำกันที่เรียกว่า Sharer Protocole คำสั่งอนุกรมทุกคำสั่งที่ได้รับจาก Arduino จะถูกระหว่างกัน
ที่จะดำเนินการต่อฉันสัญญา ...
ฉันมีแนวคิดบางอย่างที่จะขยายคุณสมบัติของผู้แบ่งปันเช่น:
หากคุณสนใจที่จะช่วยเหลือฉันในการพัฒนาผู้แบ่งปันฉันยินดีที่จะได้รับคำขอคุณสมบัติ ยินดีต้อนรับส้อม;)