Code Trong Vô Thức
Đăng Nhập
Tìm kiếm
Display results as :
Advanced Search

Share
Xem chủ đề cũ hơnXem chủ đề mới hơn
avatar
Lang thang !!
Lang thang !!
Gender : Nam

Posts Posts : 5183
Thanked : 915
Coins Coins : 12043
Tham Gia : 26/08/2008
Birthday : 05/08/1992

Status Status : 0 - 0 thể - 0 bao giờ ^^
Xem lý lịch thành viên
13022017
https://nghiaducvn.wordpress.com/2008/05/18/l%E1%BA%ADp-trinh-driver-tren-windows-cai-d%E1%BA%B7t-b%E1%BB%99-cong-c%E1%BB%A5-l%E1%BA%ADp-trinh/

http://www.dientuvietnam.net/forums/forum/vi-%C4%91i%E1%BB%81u-khi%E1%BB%83n-mcu-b%E1%BB%99-%C4%91i%E1%BB%81u-khi%E1%BB%83n-t%C3%ADn-hi%E1%BB%87u-s%E1%BB%91-dsc/t%E1%BB%AB-pic-t%E1%BB%9Bi-dspic/101594-tut-how-to-communication-a-usb-device-with-visual-studio-2010

https://msdn.microsoft.com/en-us/library/windows/hardware/hh706187(v=vs.85).aspx

http://www.slideshare.net/CongPho/hng-dn-giao-tip-serial-port-s-dng-vb

http://www.naughter.com/serialport.html

https://www.youtube.com/watch?v=-qMpPR5JBwc

https://www.youtube.com/watch?v=Pv4litTOKUc
Attachments
TUT02.01.PVN.LINH.010306.pdf You don't have permission to download attachments.(400 Kb) Downloaded 1 times
Share this post on:Excite BookmarksDiggRedditDel.icio.usGoogleLiveSlashdotNetscapeTechnoratiStumbleUponNewsvineFurlYahooSmarking

Comments

avatar
on Wed 15 Feb 2017, 06:11NShinjuku
Code:
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;

/*
 * USB-I2C example.
 * Uses a USB-I2C to read the range and light values back from an SRF08.
 *
 */

namespace usb_i2c
{
    public partial class Form1 : Form
    {
        static SerialPort USB_PORT;
        byte[] SerBuf = new byte[64];
        byte usb_found;

        public Form1()
        {
            InitializeComponent();
            USB_PORT = new SerialPort();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void transmit(byte write_bytes)
        {
            try
            {
                if (usb_found == 1) USB_PORT.Write(SerBuf, 0, write_bytes);      // writes specified amount of SerBuf out on COM port
            }
            catch (Exception)
            {
                usb_found = 0;
                MessageBox.Show("write fail");
                return;
            }
        }

        private void receive(byte read_bytes)
        {
            byte x;
            for (x = 0; x < read_bytes; x++)      // this will call the read function for the passed number times,
            {                                      // this way it ensures each byte has been correctly recieved while
                try                                // still using timeouts
                {
                    if (usb_found == 1) USB_PORT.Read(SerBuf, x, 1);    // retrieves 1 byte at a time and places in SerBuf at position x
                }
                catch (Exception)                  // timeout or other error occured, set lost comms indicator
                {
                    usb_found = 0;
                    MessageBox.Show("read fail");
                    return;
                }
            }
        }

        private void comboBox_comport_MouseDown(object sender, MouseEventArgs e)
        {
            // Grab the list of available COM ports and place them in the combo box
            comboBox_comport.Items.Clear();
            string[] comportnames = new string[100];
            comportnames = SerialPort.GetPortNames();
            var sortedList = comportnames.OrderBy(port => Convert.ToInt32(port.Replace("COM", string.Empty)));
            foreach (string port in sortedList)
            {
                comboBox_comport.Items.Add(port);                      // places each "COMx" name into combobox
            }
        }

        private void comboBox_comport_SelectedIndexChanged(object sender, EventArgs e)
        {
            timer1.Stop();
            try
            {
                if (USB_PORT.IsOpen) USB_PORT.Close(); // close any existing handle
            }
            catch {
                MessageBox.Show("Error closing to COM port", "USB-I2C", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                Application.Exit();
            }
            try
            {
                USB_PORT.PortName = comboBox_comport.Text;    // retrieves "COMx" from selection in combo box
                USB_PORT.Parity = 0;
                USB_PORT.BaudRate = 19200;
                USB_PORT.StopBits = StopBits.Two;
                USB_PORT.DataBits = 8;
                USB_PORT.ReadTimeout = 500;
                USB_PORT.WriteTimeout = 500;
                USB_PORT.Open();
                usb_found = 1;
                timer1.Enabled = true;
                timer1.Start();
            }
            catch
            {
                MessageBox.Show("Error opening to COM port", "USB-I2C", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                usb_found = 0;
                timer1.Stop();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (usb_found == 1)
            {
                // Start a ranging from the SRF08
                SerBuf[0] = 0x55;
                SerBuf[1] = 0xE0;
                SerBuf[2] = 0x00;
                SerBuf[3] = 0x01;
                SerBuf[4] = 0x51;
                transmit(5);
                receive(1);

                if (SerBuf[0] == 0)    // If we receive a 0 then there has been a problem writing to the I2C device
                {
                    MessageBox.Show("Error writing to I2C device", "USB-I2C", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    usb_found = 0;
                }

                System.Threading.Thread.Sleep(100); // Wait for the ranging to happen

                // Read back the SRF08 software value, Light value and range from the SRF08
                SerBuf[0] = 0x55;
                SerBuf[1] = 0xE1;
                SerBuf[2] = 0x00;
                SerBuf[3] = 0x04;
                transmit(4);
                receive(4);

                // Calcualte the range
                int range = SerBuf[2];
                range <<= 8;
                range += SerBuf[3];

                label_ver.Text = "SRF08 ver : " + SerBuf[0].ToString();
                label_light.Text = "Light : " + SerBuf[1].ToString();
                label_range.Text = "Range : " + range.ToString();
            }
        }
    }
}
Xem chủ đề cũ hơnXem chủ đề mới hơn
Permissions in this forum:
Bạn không có quyền trả lời bài viết