長輩視訊-window端


這邊主要是接收Arduino傳過來的指令,我用C#做的。
程式執行視窗
用這程式控制下面的網頁
先選擇好com port 在點下start就能夠開始執行了




//#define open_chrome_64bit
//#define dont_open_chrome

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO.Ports; //要使用serial port需要引用它
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        SerialPort serialPort;
        bool continuethread = false;
        Thread readThread;
        int seriallifenum = 2;
        int seriallife;
        int serial_state = 0;
        
        public Form1()
        {
            InitializeComponent();

            starbutton.Enabled = false;
            stopbutton.Enabled = false;
            listBox1.Items.Clear();
            string[] myPorts = SerialPort.GetPortNames(); //取得所有port的名字的方法

            comboBox1.DataSource = myPorts;   //直接取得所有port的名字
            if(comboBox1.Items.Count > 1)
            {
                comboBox1.SelectedIndex = 1;
            }
            //serialPort = new SerialPort(comboBox1.Text, 9600, Parity.None, 8, StopBits.One);
            serialPort = new SerialPort();
            Console.WriteLine("The following serial ports were found: {0}", comboBox1.Text);
            mycode mycode = new mycode();

            if (comboBox1.Text != "")
            {
                starbutton.Enabled = true;
            }

            Closing += new CancelEventHandler(Form1_Closing);
        }


        /*  thread modify form  */
        //宣告一個委派,定義參數
        public delegate void btnchang();
        public delegate void mylog(string message);
        public delegate void threaddie();

        /*** 開始執行通訊後,切換按鈕狀態 ***/
        public void changbtn()
        {
            starbutton.Enabled = false;
            stopbutton.Enabled = true;
        }

        /*** timeout close thread. 超時關閉thread ***/
        public void threadstop()
        {
            listboxlog(string.Format("{0} {1} {2}", "com port", serialPort.PortName, "No response"));
            continuethread = false;
            serialPort.Close();
            serial_state = 0;

            Console.WriteLine("com port close");

            starbutton.Enabled = true;
            stopbutton.Enabled = false;
            listboxlog(string.Format("{0} {1} {2}", "com port", serialPort.PortName, "close"));

            error_messagebox();
        }
        /*  thread modify form  */

        /*** this is read message of serial port function. 讀取comport傳來的訊息 ***/
        public void Read()
        {
            btnchang btn = new btnchang(changbtn);
            this.BeginInvoke(btn);
            
            while (continuethread)
            {
                string message = "abc";
                try
                {
                    /********
                     * step:
                     * read serial port
                     * if (pick up phone)
                     * if (hang up phone)
                     * if (volume up)
                     * if (volume down)
                     * ******/
                    message = serialPort.ReadLine();
                    message = message.Remove(message.Length-1, 1);
                    Console.WriteLine(message);

                    //實作委派,指向員執行緒中被呼叫的Method
                    mylog mylog = new mylog(listboxlog);
                    this.BeginInvoke(mylog, new Object[] { string.Format("{0}{1}", "receive : ", message) });

                    StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
                    if (stringComparer.Equals("open", message) & serial_state == 1)
                    {
                        this.BeginInvoke(mylog, new Object[] { string.Format("{0}{1}", "do : ", message) });
                        mycode.open_hangouts();
                        serialPort.ReadTimeout = 30*60*1000;
                        serial_state = 2;
                    }
                    else if (stringComparer.Equals("pick up", message) & serial_state == 2)
                    {
                        this.BeginInvoke(mylog, new Object[] { string.Format("{0}{1}", "do : ", message) });
                        mycode.pick_up1();
                        mycode.pick_up2();
                    }
                    else if (stringComparer.Equals("hang up", message) & serial_state == 2)
                    {
                        this.BeginInvoke(mylog, new Object[] { string.Format("{0}{1}", "do : ", message) });
                        mycode.findHangoutsWindow();
                        mycode.CloseChatRoom();
                    }
                    else if (stringComparer.Equals("vol up", message) & serial_state == 2)
                    {
                        this.BeginInvoke(mylog, new Object[] { string.Format("{0}{1}", "do : ", message) });
                        mycode.btnVup_Click();
                        mycode.vol_up();
                    }
                    else if (stringComparer.Equals("vol down", message) & serial_state == 2)
                    {
                        this.BeginInvoke(mylog, new Object[] { string.Format("{0}{1}", "do : ", message) });
                        mycode.btnVdown_Click();
                        mycode.vol_down();
                    }
                    else if (stringComparer.Equals("connecting", message) & serial_state == 2)
                    {
                        seriallife = seriallifenum;
                    }
                }
                catch (TimeoutException) {
                    Console.WriteLine("TimeoutException");
                    serialPort.WriteLine("connect");
                    seriallife = seriallife -1;
                    if(seriallife < 1)
                    {
                        threaddie rstop = new threaddie(threadstop);
                        this.BeginInvoke(rstop);
                        continuethread = false;
                    }
                }
                catch (InvalidOperationException)
                {/*執行中通訊(com port)被拔除*/
                    Console.WriteLine("InvalidOperationException");
                    threaddie rstop = new threaddie(threadstop);
                    this.BeginInvoke(rstop);
                    continuethread = false;
                }
                catch (System.IO.IOException)
                {/*執行中通訊(com port)被拔除*/
                    Console.WriteLine("System.IO.IOException");
                    threaddie rstop = new threaddie(threadstop);
                    this.BeginInvoke(rstop);
                    continuethread = false;
                }
            }  
        }
        /*** start 按鈕按下 ***/
        private void startbutton_Click(object sender, EventArgs e)
        {
            listboxlog("startbutton_Click");
            if (comboBox1.Text != "")
            {
                StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
                serialPort = new SerialPort(comboBox1.Text, 9600, Parity.None, 8, StopBits.One);
                readThread = new Thread(Read);
                continuethread = true;
                seriallife = seriallifenum;
                serialPort.ReadTimeout = 1000;
                //readThread.IsBackground = true;
                try
                {
                    serialPort.Open();
                    if (!readThread.IsAlive)
                    {
                        readThread.Start();
                        listboxlog("readThread Start");
                    }
               
                    Console.WriteLine("com port start");
                    listboxlog(string.Format("{0} {1} {2}", "com port", serialPort.PortName, "start"));
                    serialPort.WriteLine("serial start");
                    serial_state = 1;

                }
                catch (System.IO.IOException)
                {
                    stop_serial_and_thread(false);
                    error_messagebox();
                }
            }
        }

        /*** stop 按鈕按下 ***/
        private void stopbutton_Click(object sender, EventArgs e)
        {
            listboxlog("stopbutton_Click");
            stop_serial_and_thread(true);

        }

        /*** application close 程式被關閉 ***/
        private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (stopbutton.Enabled)
            {
                stop_serial_and_thread(true);
            }
        }
        /*** 關閉 com port 連線,與 thread ***/
        private void stop_serial_and_thread(bool have_serialport)
        {
            if (have_serialport)
            {
                serialPort.WriteLine("serial quit");
            }
            continuethread = false;
            if (readThread != null && readThread.IsAlive)
            {
                readThread.Join();
                readThread = null;
                if (have_serialport)
                {
                    serialPort.Close();
                }
            }

            Console.WriteLine("com port close");
            serial_state = 0;
            starbutton.Enabled = true;
            stopbutton.Enabled = false;
            listboxlog(string.Format("{0} {1} {2}", "com port", serialPort.PortName, "close"));
        }

        /*** 按下下拉是選單的動作 ***/
        private void comboBox1_Click(object sender, EventArgs e)
        {
            string[] myPorts = SerialPort.GetPortNames(); //取得所有port的名字的方法

            comboBox1.DataSource = myPorts;   //直接取得所有port的名字
            if (comboBox1.Text != "")
            {
                starbutton.Enabled = true;
            }
            if (comboBox1.Text == "")
            {
                starbutton.Enabled = false;
            }
        }

        /*** application log record 程式執行紀錄 ***/
        private void listboxlog(string message)
        {            
            if ((listBox1.Items.Count - 1) >= 50000)
            {
                listBox1.Items.RemoveAt(0);
                //listBox1.SetSelected(5, true);
            }
            string date = DateTime.Now.ToShortDateString();
            string time = DateTime.Now.ToString("HH:mm ss");
            listBox1.Items.Add(string.Format("{0} {1}  {2}", date, time, message));
            listBox1.TopIndex = listBox1.Items.Count - 1;
        }

        /*** 連線錯誤訊息框 ***/
        private void error_messagebox()
        {
            this.Activate();
            MessageBox.Show(comboBox1.Text + " 使用無效,請選擇一種方式修正:\n\n  1.請等待10秒再按下開始(start)\n  2.修改正確的 com port 值\n  3.重新開啟本程式\n  4.重新插入設備.", "視訊控制程式 連線錯誤");
        }
    }


    /************************
     * 
     * mycode class
     * 
     * 1. open hangouts app
     * 2. pick up phone
     * 3. close chat room
     * 4. hang up phone (close videophone room)
     * 5. volume up
     * 6. volume down
     * 7. volume mute
     * 
     * **********************/

    public class mycode
    {
        public static void open_hangouts()
        {
            Process chrome = new Process();
            // FileName 是要執行的檔案
#if !open_chrome_64bit
            string fname = @"C:\Program Files\Google\Chrome\Application\chrome.exe";
#else
            string fname = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
#endif           
            chrome.StartInfo.FileName = fname;
            chrome.StartInfo.Arguments = "https://hangouts.google.com/?hl=zh-TW";
#if !dont_open_chrome
            chrome.Start();
#endif
        }

        public static void pick_up1()
        {
            /**
             * 按下剛打來的"接聽"按鈕
             * 1.高的-聊天室視窗
             * x    1150    1200    1263
             * y    265     280     295
             * 
             * 2.低的-聊天室視窗
             * x    1148            1262  幾乎同上
             * y    301     316     331
             * 
             * 要低到高
             **/
            int x = 1190;
            int y = 316;
            Cursor.Position = new Point(x, y);
            ClassMouse.LeftClick();
            
            y = 280;
            Cursor.Position = new Point(x, y);
            ClassMouse.LeftClick();

        }
        public static void pick_up2()
        {
            /**
             * 按下"加入對話
             * 
             * x    1016    1152    1272
             * 
             * 1.高的-聊天室視窗
             * y    215     235     255    
             * 
             * 2.低的-聊天室視窗
             * y    250     270     290
             * 
             * 要低到高
             **/
            int x = 1190;
            int y = 270;
            Cursor.Position = new Point(x, y);
            ClassMouse.LeftClick();
            
            y = 235;
            Cursor.Position = new Point(x, y);
            ClassMouse.LeftClick();
        }
        public static void CloseChatRoom()
        {
            /**
             * 按下"X"關閉聊天室
             * 
             * x    1250    1255    1264
             * 
             * 1.高的-聊天室視窗
             * y    156     161     166
             * 
             * 2.低的-聊天室視窗
             * y    190     195     200
             * 
             * 要高到低
            **/
            int x = 1255;
            int y = 161;
            Cursor.Position = new Point(x, y);
            ClassMouse.LeftClick();

            y = 195;
            Cursor.Position = new Point(x, y);
            ClassMouse.LeftClick();
        }

        /* *
         * keyboard ctrl
         * */
        // Get a handle to an application window.
        [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
        public static extern IntPtr FindWindow(string lpClassName,
            string lpWindowName);

        // Activate an application window.
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        // Send a series of key presses to the Calculator application.
        public static void Calculator()
        {
            // Get a handle to the Calculator application. The window class
            // and window name were obtained using the Spy++ tool.
            IntPtr calculatorHandle = FindWindow("ApplicationFrameWindow", "小算盤");
            //Chrome_WidgetWin_1
            //Hangouts 視訊通話

            // Verify that Calculator is a running process.
            if (calculatorHandle == IntPtr.Zero)
            {
                MessageBox.Show("Calculator is not running.");
                return;
            }

            // Make Calculator the foreground application and send it 
            // a set of calculations.
            SetForegroundWindow(calculatorHandle);
            /*
            SendKeys.SendWait("111");
            SendKeys.SendWait("*");
            SendKeys.SendWait("11");
            SendKeys.SendWait("=");*/
        }
        public static void findHangoutsWindow()
        {
            // Get a handle to the application. 
            // The window class and window name were obtained using the Spy++ tool.
            IntPtr HangoutsHandle = FindWindow(null, "Hangouts 視訊通話 - Google Chrome");
            //Chrome_WidgetWin_1
            //Hangouts 視訊通話
            //Hangouts 視訊通話 - Google Chrome
            
            // Verify that Calculator is a running process.
            if (HangoutsHandle == IntPtr.Zero)
            {
                //MessageBox.Show("Calculator is not running.");
                return;
            }

            // Make Calculator the foreground application and send it 
            // a set of calculations.
            SetForegroundWindow(HangoutsHandle);
            Thread.Sleep(1000);
            SendKeys.SendWait("^w");

            while (HangoutsHandle != IntPtr.Zero) {
                Thread.Sleep(1000);
                HangoutsHandle = FindWindow(null, "Hangouts 視訊通話 - Google Chrome");
            }

        }
        public static void ctrl_atl_del()
        {
            SendKeys.SendWait("^+{ESC}");
            //SendKeys.SendWait("^%{DELETE}");
            //SendKeys.Send("%{F4}");
        }

        /**
        * xp 
        * http://www.cnblogs.com/tmywu/archive/2013/03/18/2965585.html
        * http://fecbob.pixnet.net/blog/post/38064589-c%23-%E9%9F%B3%E9%87%8F%E6%8E%A7%E5%88%B6%E5%99%A8
        **/
        //運用WinAPI控制電腦靜音與音量
        private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
        private const int APPCOMMAND_VOLUME_UP = 0x0a0000;
        private const int APPCOMMAND_VOLUME_DOWN = 0x090000;
        private const int WM_APPCOMMAND = 0x319;
        //private static IntPtr Handle;
        public static Process p = Process.GetCurrentProcess();

        [DllImport("user32.dll")]
        public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

        public static void btnVup_Click()
        {
            // 聲音變大 
            SendMessageW(p.Handle, WM_APPCOMMAND, p.Handle, (IntPtr)APPCOMMAND_VOLUME_UP);
        }

        public static void btnVdown_Click()
        {
            // 聲音變小 
            SendMessageW(p.Handle, WM_APPCOMMAND, p.Handle, (IntPtr)APPCOMMAND_VOLUME_DOWN);
        }

        public static void btnMute_Click()
        {
            // 靜音 
            SendMessageW(p.Handle, WM_APPCOMMAND, p.Handle, (IntPtr)APPCOMMAND_VOLUME_MUTE);
        }

        /**
         * 
         * 
         * */

        [DllImport("winmm.dll")]
        public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);

        [DllImport("winmm.dll")]
        public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);

        public static void vol_up()
        {
            // 設定變數CurrVol用以取得目前音量
            uint CurrVol = 0;
            // 透過waveOutGetVolume取得目前音量給變數CurrVol
            waveOutGetVolume(IntPtr.Zero, out CurrVol);
            // 計算音量大小
            ushort CalcVol = (ushort)(CurrVol & 0x0000ffff);
            // 將音量大小分為10階,並指定給trackWaved控制項
            int vol_value = CalcVol / (ushort.MaxValue / 10);
            Console.WriteLine(CalcVol / (ushort.MaxValue / 10));
            vol_value = vol_value + 1;
            ValueChanged(vol_value);

        }

        public static void vol_down()
        {
            // 設定變數CurrVol用以取得目前音量
            uint CurrVol = 0;
            // 透過waveOutGetVolume取得目前音量給變數CurrVol
            waveOutGetVolume(IntPtr.Zero, out CurrVol);
            // 計算音量大小
            ushort CalcVol = (ushort)(CurrVol & 0x0000ffff);
            // 將音量大小分為10階,並指定給trackWaved控制項
            int vol_value = CalcVol / (ushort.MaxValue / 10);
            Console.WriteLine(CalcVol / (ushort.MaxValue / 10));
            vol_value = vol_value - 1;
            ValueChanged(vol_value);

        }

        // 當控制項trackWave的值改變時,設定音量
        private static void ValueChanged(int value)
        {
            // 由trackWave上的值計算要設定的音量大小
            int NewVolume = ((ushort.MaxValue / 10) * value);
            // 設定相同的數值於左聲道與右聲道
            uint NewVolumeAllChannels = (((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16));
            // 設定音量
            waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);
        }
    }
}

留言

這個網誌中的熱門文章

C# 模擬鍵盤滑鼠控制電腦

python pyautogui 簡介

android 定時通知(永久長期的) 本篇只講AlarmManager使用

raspberrypi 開機自動執行程式 與 在terminal開啟第二個terminal執行python

python nn 聲音辨識 -1 傅立葉轉換