visual studio 2017 C#
bitalinoと言うBLE通信するデバイスと、BLE通信するwindowsアプリを作りたいと思っています。
ゆくゆくはwindowsフォームアプリでGUIアプリを作りたいと思っていますが、一先ず通信確認用にconsoleアプリをC#で作っていました。
BLE通信と言いますが、PC側は仮想COMドングルを使って、結局シリアル通信で、BLEの通信はAPIを使ってと言う事になります。
APIは下記サイトで公開されているのでこちらのvisual C#のやつを使っています。
http://bitalino.com/en/development/apis
BITalino_x86.dllと言う用意されているdllを参照して、下記コードを作成したのですが、
string ver = dev.version();
のところで3秒ほど固まって、デバイスが見つけられなかったと言う例外が発生します。
C#
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6 7namespace ConsoleApp1 8{ 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 try 14 { 15 // uncomment this block to search for Bluetooth devices 16 /* 17 Bitalino.DevInfo[] devs = Bitalino.find(); 18 foreach (Bitalino.DevInfo d in devs) 19 Console.WriteLine("{0} - {1}", d.macAddr, d.name); 20 return; 21 */ 22 23 Console.WriteLine("Connecting to device..."); 24 25 //Bitalino dev = new Bitalino("CC:78:AB:5E:9D:3D"); // device MAC address 98:D3:31:B2:11:6B 26 Bitalino dev = new Bitalino("COM3"); // Bluetooth virtual COM port or USB-UART COM port 27 28 Console.WriteLine("Connected to device. Press Enter to exit."); 29 30 string ver = dev.version(); // get device version string 31 Console.WriteLine("BITalino version: {0}", ver); 32 33 dev.battery(10); // set battery threshold (optional) 34 35 dev.start(1000, new int[] { 0, 1, 2, 3, 4, 5 }); // start acquisition of all channels at 1000 Hz 36 37 bool ledState = false; 38 39 Bitalino.Frame[] frames = new Bitalino.Frame[100]; 40 for (int i = 0; i < frames.Length; i++) 41 frames[i] = new Bitalino.Frame(); // must initialize all elements in the array 42 43 do 44 { 45 ledState = !ledState; // toggle LED state 46 dev.trigger(new bool[] { false, false, ledState, false }); 47 48 dev.read(frames); // get 100 frames from device 49 Bitalino.Frame f = frames[0]; // get a reference to the first frame of each 100 frames block 50 Console.WriteLine("{0} : {1} {2} {3} {4} ; {5} {6} {7} {8} {9} {10}", // dump the first frame 51 f.seq, 52 f.digital[0], f.digital[1], f.digital[2], f.digital[3], 53 f.analog[0], f.analog[1], f.analog[2], f.analog[3], f.analog[4], f.analog[5]); 54 55 } while (!Console.KeyAvailable); // until a key is pressed 56 57 dev.stop(); // stop acquisition 58 59 dev.Dispose(); // disconnect from device 60 } 61 62 catch (Bitalino.Exception e) 63 { 64 Console.WriteLine("BITalino exception: {0}", e.Message); 65 } 66 } 67 } 68 69} 70
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。