2017年9月21日 星期四

Raspberry]Bluetooth pi 無法與PC 正確識別對方


網路上找到的解法

Use app to connect to pi via bluetooth
https://www.raspberrypi.org/forums/viewtopic.php?p=947185#p947185

2017年9月8日 星期五

[Raspberry]Bluetooth


照著抄寫,有一堆編譯錯誤,好像找不到,h檔似的,可是我都有include...不知哪邊有誤

照著如下進去,看似有點進展:

但是會有如下錯誤訊息:

好像沒找到lib似的..加入如下檔案

竟成功了....YA!!





不錯的資料

樹莓派中 USB 藍牙卡的驅動與設置
http://ruten-proteus.blogspot.tw/2014/07/Bluetooth-Kit-tutorial-01.html

Raspberry Pi 安裝 USB Bluetooth Dongle
http://tekibrain.blogspot.tw/2013/05/raspberry-pi-usb-bluetooth-dongle.html

Bluetooth - Installing and Using Bluetooth on the Raspberry Pi
https://thepihut.com/blogs/raspberry-pi-tutorials/17841464-bluetooth-installing-and-using-bluetooth-on-the-raspberry-pi

Bluetooth Commands
http://www.raspberry-projects.com/pi/pi-operating-systems/raspbian/bluetooth/bluetooth-commands

Raspberry Pi 安裝 BlueZ 支援藍牙 BLE 功能http://www.arthurtoday.com/2014/12/raspberry-pi-install-bluez-5-with-ble-support.html


Chapter 4. Bluetooth programming in C with BlueZhttps://people.csail.mit.edu/albert/bluez-intro/c404.html


關於藍牙裝置找尋(inquiry, scan)兩三事http://ops9.blogspot.tw/2013/09/inquiry-scan.html

2017年9月1日 星期五

[C#]Bluetooth

照著網路文章打,竟通了....YA!!
















public partial class Form1 : Form
    {
        

        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            BluetoothClient bc =new BluetoothClient();


            BluetoothDeviceInfo[] arr = new BluetoothDeviceInfo[10];

            //第一參數表明搜索的最大設備數
            //第二參數表示是否搜尋已配對設備
            //第三參數表示是否搜尋濟助的設備
            //第四個表示是否搜尋未知設備
            //第五個表示搜索範圍內可被發現的設備

            arr =bc.DiscoverDevices(255, true, true, true,true);

            bc.Close();
        }




        BluetoothRadio radio = null;
        string sendFileName = null;
        BluetoothAddress sendAddress = null;
        ObexListener listener = null;
        string recDir = null;
        Thread listenThread, sendThread;

        private void button6_Click(object sender, EventArgs e)
        {
            radio = BluetoothRadio.PrimaryRadio;//獲取當前PC的藍牙適配器

            CheckForIllegalCrossThreadCalls = false;//不檢查跨執行緒

            if(radio==null)
            {
                MessageBox.Show("Bluetooth can't use !", "RROR");
            }

            recDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            richTextBox1.Text = recDir;

        }

        private void button7_Click(object sender, EventArgs e)
        {
            SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog();
            dialog.ShowRemembered = true;
            dialog.ShowAuthenticated = true;
            dialog.ShowUnknown = true;

            if(dialog.ShowDialog()==DialogResult.OK)
            {
                sendAddress = dialog.SelectedDevice.DeviceAddress;
                richTextBox1.Text = "Address:" + sendAddress.ToString() + "\r\n";
                richTextBox1.Text = richTextBox1.Text + "Device Name:" + dialog.SelectedDevice.DeviceName+"\r\n";
            }

        }

        private void button8_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            if(dialog.ShowDialog()==DialogResult.OK)
            {
                sendFileName = dialog.FileName;
                richTextBox1.Text = Path.GetFileName(sendFileName);

                sendThread = new Thread(sendFile);
                sendThread.Start();
            }
        }


        private void sendFile()
        {
            ObexWebRequest request = new ObexWebRequest(sendAddress, Path.GetFileName(sendFileName));   //創建網路需求

            WebResponse response = null;

            try
            {
                button8.Enabled = false;
                richTextBox1.Text = "";
                request.ReadFile(sendFileName);
                richTextBox1.Text = "start transfer"+"\r\n";
                response = request.GetResponse();
                richTextBox1.Text = richTextBox1.Text + "transfer End";
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("transfer Failed");
                richTextBox1.Text = richTextBox1.Text + "transfer failed";
            }
            finally
            {
                if(response!=null)
                {
                    response.Close();
                    button8.Enabled = true;
                }
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "選擇藍芽接收文件的存放路徑";
            if(dialog.ShowDialog()==DialogResult.OK)
            {
                recDir = dialog.SelectedPath;
                richTextBox1.Text = dialog.SelectedPath;
                richTextBox1.Text = recDir;

                if((listener==null) ||(!listener.IsListening))
                {
                    radio.Mode = RadioMode.Discoverable;//設置本地藍芽可被檢測
                    listener = new ObexListener(ObexTransport.Bluetooth);

                    listener.Start();

                    if(listener.IsListening)
                    {
                        richTextBox1.Text = "Start listener";

                        listenThread = new Thread(receiveFile);
                        listenThread.Start();
                    }
                }
                else
                {
                    listener.Stop();
                    richTextBox1.Text = "Stop Listener";
                }

            }
        }


        private void receiveFile()
        {
            ObexListenerContext context = null;

            ObexListenerRequest request = null;

            while(listener.IsListening)
            {
                context = listener.GetContext();    //獲取解析上下文
                if(context==null)
                {
                    break;
                }
                request = context.Request;//獲取請求
                string uriString = Uri.UnescapeDataString(request.RawUrl);//將 uri轉換成字符號
                string recFileName=recDir + uriString;

                request.WriteFile(recFileName);

                richTextBox1.Text = "收到文件" + uriString.TrimStart(new char[] { '/' });
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(sendThread!=null)
            {
                sendThread.Abort();
            }

            if (listener != null && listener.IsListening)
            {
                listener.Stop();
            }



            if (listenThread!=null)
            {
                listenThread.Abort();
            }




        }

        private void button5_Click(object sender, EventArgs e)
        {
            BluetoothRadio bluetoothRadio = BluetoothRadio.PrimaryRadio;
            if(bluetoothRadio==null)
            {
                MessageBox.Show("沒找到本機藍芽裝置");
            }
            else
            {
                richTextBox1.Text = "Class of Device:" + bluetoothRadio.ClassOfDevice + "\r\n";

                richTextBox1.Text = richTextBox1.Text + "Hardware Status:" + bluetoothRadio.HardwareStatus + "\r\n";

                richTextBox1.Text = richTextBox1.Text + "HCiRevision:" + bluetoothRadio.HciRevision+"\r\n";

                richTextBox1.Text = richTextBox1.Text + "HCiVersion:" + bluetoothRadio.HciVersion+"\r\n";

                richTextBox1.Text = richTextBox1.Text + "LmpSubversion:" + bluetoothRadio.LmpSubversion + "\r\n";

                richTextBox1.Text = richTextBox1.Text + "Local address:" + bluetoothRadio.LocalAddress + "\r\n";

                richTextBox1.Text = richTextBox1.Text + "Manufacturer:" + bluetoothRadio.Manufacturer + "\r\n";

                richTextBox1.Text = richTextBox1.Text + "Mode:" + bluetoothRadio.Mode+"\r\n";

                richTextBox1.Text = richTextBox1.Text + "Name:" + bluetoothRadio.Name + "\r\n";

                richTextBox1.Text = richTextBox1.Text + "Remote:" + bluetoothRadio.Name + "\r\n";

                richTextBox1.Text = richTextBox1.Text + "SoftwareManfacturer:" + bluetoothRadio.SoftwareManufacturer + "\r\n";

                richTextBox1.Text = richTextBox1.Text + "StackFactory" + bluetoothRadio.StackFactory + "\r\n";
            }
        }

    }


兩台PC使用rfcomm通訊



using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
using System.IO;

namespace bluetooth_rfcomm
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BluetoothClient client = new BluetoothClient();
            BluetoothDeviceInfo[] devices = client.DiscoverDevices();
            BluetoothDeviceInfo device = null;

            richTextBox1.Text = "";
            foreach(BluetoothDeviceInfo d in devices)
            {
                richTextBox1.Text = richTextBox1.Text + " " + d.DeviceName + "\n";

                if (d.DeviceName=="NB-13")
                {
                    device = d;
                    break;
                }
            }

            if(device!=null)
            {
                richTextBox1.Text = richTextBox1.Text + "connect start\n";

                client.Connect(device.DeviceAddress, BluetoothService.SerialPort);

                Stream perStream = client.GetStream();

                byte[] buffer = new byte[200];
                perStream.Read(buffer, 0, 40);
                string data = System.Text.ASCIIEncoding.ASCII.GetString(buffer, 0, 40);

                richTextBox1.Text = richTextBox1.Text + "DATA:"+data+"\n";


                perStream.Close();

            }
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.Text ="\n";

            BluetoothRadio myRadio = BluetoothRadio.PrimaryRadio;


            if (myRadio == null)
            {
                richTextBox1.Text = "No radio HW or unsupported SW stack";
                return;
            }

            richTextBox1.Text = richTextBox1.Text+"Radio,address:" + myRadio.LocalAddress.ToString() + "\n";
            richTextBox1.Text = richTextBox1.Text + "Mode:" + myRadio.Mode.ToString() + "\n";
            richTextBox1.Text = richTextBox1.Text + "Name: " + myRadio.Name + " LmpSubversion:" + myRadio.LmpSubversion + "\n";
            richTextBox1.Text = richTextBox1.Text + "ClassofDevice:" + myRadio.ClassOfDevice.ToString() + "   device:" + myRadio.ClassOfDevice.Device.ToString() + "  Service:" + myRadio.ClassOfDevice.Service.ToString() + "\n";

            myRadio.Mode = RadioMode.Discoverable;
            richTextBox1.Text = richTextBox1.Text + "Radio Mode now:" + myRadio.Mode.ToString();

            richTextBox1.Text = richTextBox1.Text + "\n";

            Application.DoEvents();

            BluetoothListener listener = new BluetoothListener(BluetoothService.SerialPort);
            listener.Start();
            richTextBox1.Text = richTextBox1.Text + "service startd:\n";

            

            BluetoothClient client = listener.AcceptBluetoothClient();
            richTextBox1.Text = richTextBox1 + "Got a request\n";

            Stream peerStream=client.GetStream();

            string dataToSend = "Hello from service";
  
            byte[] dataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(dataToSend);

            peerStream.Write(dataBuffer, 0, dataBuffer.Length);





        }
    }

}

















不錯的文章:
How to find Bluetooth MAC Address in Windows
https://macaddresschanger.com/how-to-find-bluetooth-mac-address-windows

連接方式介紹
http://32feet.codeplex.com/documentation

32feet.net 蓝牙虚拟串口编程
https://tisyang.github.io/2016/05/bluetooth-serialport-with-32feet-net-library/

Windows Moible, Wince 使用.NET Compact Framework进行蓝牙(Bluetooth)开发 之 32feet.NET
http://www.cnblogs.com/procoder/archive/2009/05/14/1456243.html

C#编程连接蓝牙设备,文件收发
http://hzy3774.iteye.com/blog/1735163

2017年8月29日 星期二

[C#]Windows Service

先記起來...YA!!

撰寫在背景執行的服務 (Windows Service)
http://j796160836.pixnet.net/blog/post/40306051

2017年8月8日 星期二

[C#]讀寫 excel file


不錯的文章

http://lhzyaminabe.blogspot.tw/2015/10/nugetnpoi.html
https://stackoverflow.com/questions/10240029/how-to-install-a-nuget-package-nupkg-file--locally

2017年6月23日 星期五

Linux Shell 學習

看 Linux Shell 程式設計實力養成 的小筆記



背景執行:指令後面加上一個"&"
xclock&

指令一;指令二
不論結果如何,指令一執行後,接著執行指令二

指令一&&指令二
指令一正確執行後,接著執行指令二,若指令一錯誤,則不執行指令二

指令一||指令二
指令一正確執行後,不執行執行指令二,若指令一錯誤,則執行指令二



執行其他指令方法
(1)C語言源碼
使用tar指令解開,進入解開目錄後,依序執行如下指令:
./Configure
make
make install
執行後,編譯的執行檔通常放在/usr/local/服務名稱/bin/之下

(2)Shell Script
執行Shell Script的方式有三,分述如下:
賦予執行權限(chmod 755 script)後,直接執行(./Script)
執行Source script.sh(注意副檔名必須是sh)
執行. script.sh(. 與檔名間,要有空格,副檔名必須是sh)
執行 bash script.sh(與Shell Script及. script.shv相似,唯一的差別是會在另一個Bash環境中執行Script,執行Script所產生的環境變數與原來指令操作環境的變數不會共用



輸入輸出導向
cmd < file_name
cmd從file_name毒入資料
cmd> output_file <input_file
cmd從iput_file毒入資料,處理完畢後,處理完畢後再將資料寫回 output_file

導向空集合
ls file1 file2 >/dev/null

導向其他終端機
cat> /dev/pts/0  -->只需在TTY裝置前加上 /dev/即可


特殊字元
萬用字元 *
單一字元 ?
跳脫字元 \
跳脫字元可以用來去除字元的特殊涵義,例如:
#echo $PATH
/usr/local/sbin:/usr/local/bin

#echo \$PATH
$PATH

跳脫字元加上特定英文數字,則會有另外含意
\n 換行
\r Enter
\v 垂直對齊
\a alert(發出聲音)
\Oxx 轉換成八進位 ASCII 碼

跳脫字元放在指令列最後,代表這行指令尚未輸入完畢,會接續下一行的內容


單引號
#echo '$tmpdate'
$tmpdate

雙引號
雙引號類似單引號,保留部分字面意義(可以使用部分特殊符號 $,",\)
#echo "Let's play ball"
Let's play ball

#echo "$PATH"
/usr/local/sbin:/usr/local/bin

字元集合(中括號)
[a-z]:所有小寫英文字母
[0-9]:所有數字
[]呈現的是集合中的單一字元,例如[abc]表示a或是b或是c,而[abc][12]表示a1,a2,b1,b2,c1或是c2
[]字頭加上!,就是非的意思,例如:[!n-p36BY]就是指不包含n,o,p,3,6,B以及Y

括號擴展(大括號)
{vs,wu,pro}ftp 表示 vsftp,wuftp,proftp

#echo {a,b,c}{1,2}
a1,a2,b1,b2,c1,c2

#echo {1..10}
1 2 3 4 5 6 7 8 9 10

#echo {1..10..3}
1 4 7 10

grep 可尋找檔案中的字串,使用root搜尋會找出內文中所有包含root的行,但若加上^root,則僅會顯示 root 開頭的行

#grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash

搜尋字尾符號為:的行
在字元後面加上$字元,代表該字元必須是結束符號(該行最後一個字元)才符合條件
#grep ;$ /etc/group
jack:x:100:

得到一個以c開頭且以h結尾的所有5位元字元的英語單詞
grep '\<c...h\>' /usr/share/dict/words
catch
clash
cloth

影響系統或服務運行的指令
halt 關閉點腦

init 改變系統的運行等級
init 0 關機
init 6 重新啟動

iptables 資料包處理與安全管理的指令

kill 傳送資訊給程序

pkill 傳送信號給指定的程序

reboot 重新啟動電腦

service 開啟或關閉服務
servie sshd stop 關閉sshd而無法遠端連機

shutdown 關閉機器

影響資料儲存的指令
cfdisk 設定硬碟分割區
e2fsck 檢驗 ext2 與 ext3 檔案系統
rm 刪除檔或目錄
swapoff 關閉指定的交換區空間
umount 卸載檔案系統


要知道目前所使用的shell
#echo $SHELL
/bin/bash

如果要設定環境變數指令為 export 變數名(不可加上$)

參數變數
. test1.sh arg1 arg2 arg3,所以
$1=arg1
$2=arg2
$3=arg3
$@=test1.sh arg1 arg2 arg3
$#=這函數被執行的次數

管線命令 | (Pipe)
管線命令僅會處理standard output,對於standard error output 會予以忽略
管線命令必須要能夠接受來自前一個指令的資料成為standard iput 繼續處理才行

擷取命令:
cut
cut -d '分隔字元' -f fields
cut -c 字元間隔

grep
grep [-acinv][--color=auto] '搜尋字串' filename
-a:將binary檔案以text檔案方式加以搜尋
-c:計算找到'搜尋字串'的次數
-i:忽略大小寫
-n:順便輸出行號
-v:反向選擇

排序指令
sort

重複資料只出現一次
uniq [-ic]
-i:忽略大小寫
-c:進行計數

知道多少行,多少字
wc
wc [-lwm]
-l:僅列出行
-w:僅列出多少字(英文單字)
-m:多少字元


雙向導向
tee [-a] file
-a:累積進 file 中
同時將資料流分別送到檔案及螢幕


刪除一段訊息當中的文字,或者進行文字訊息的替換
tr [-ds] SET1
-d:刪除訊息當中的SET1這個字串
-a:取代掉重複的字元


^符號在[]內代表反向選擇,在[]之外則代表定位在行首的意義

grep -n '\.$' aa.txt
.有其他意義(.代表一定有個任意字元,*代表重複前一個字元,0到無窮多次),必須使用\來加以解除其特殊意義

grep -n '^$' aa.txt
找出空白行

努力中...












2017年6月9日 星期五

[C#]Interprocess Communications (message queue)

搞好久,找到範例了...
但是使用 toolbox 拉的 messagequeue1 就是不會動....

不錯得範例
https://dotblogs.com.tw/calm/2013/12/01/132098