วันศุกร์ที่ 15 มกราคม พ.ศ. 2559

ตัวอย่าง โค็ด vb hardware interface

Develop custom USB HID applications 
using existing Windows drivers! 
http://www.kadtronix.com/usbhidapi_usr.htm

The following image shows how to wire a standard PIR motion detector.  An external source is required for powering the detector.  (Note:  The Kadtronix USB Motion Detector is pre-wired and requires no external power source as it derives its power directly from the USB cable.  No additional wiring or assembly is required.)
http://www.kadtronix.com/digio/tutorial/tutorial.htm


ตัวอย่าง โค็ด vb hardware interface

บทความนี้แสดงตัวอย่างแม่แบบพื้นฐานสำหรับการใช้เหตุการณ์ซึ่งควบคุมโดยข้อมูลที่ได้รับชุดคำสั่งกับเหตุการณ์ OnComm นี่คือวิธีการรับข้อมูลโดยใช้ตัวควบคุม MSComm ทุกชนิดแนะนำ สำหรับข้อมูลเกี่ยวกับการแปลงข้อมูล binary โปรดดูส่วนการอ้างอิงที่ส่วนท้ายของบทความนี้https://support.microsoft.com/th-th/kb/194922


เมื่อใช้ตัวควบคุม MSCOMM เพื่อส่ง และรับข้อมูลพอร์ตอนุกรม ดีที่สุดคือการใช้เชิงการสื่อสารเหตุการณ์ วิธีการนี้มีประสิทธิภาพมากกว่าการหยั่งเสียง เท่านั้นใช้เวลาน้อยกว่า CPU และจะเท่านั้นเริ่มการกระทำเมื่อได้รับการป้อนข้อมูล ข้างล่างนี้เป็นแม่แบบไปใช้ซึ่งจะอนุญาตให้มีมากประสิทธิภาพรับของอักขระที่มีการสูญเสียข้อมูลที่น้อยที่สุดอย่างง่าย 

แนวคิดคือการ ใช้เหตุการณ์ OnComm เพื่อรับข้อมูล เขียนข้อมูลดิบให้เป็นสตริแล้ว และส่งต่อชุดคำสั่งอื่นเพื่อทำการประมวลผลข้อมูลดิบ การส่งผ่านข้อมูลไปชุดคำสั่งอื่นทำให้เหตุการณ์ OnComm เพื่อรับทุกเหตุการณ์โดยไม่มีการ "ตรึง ในขณะแยกวิเคราะห์แท้จริงของสายอักขระเกิดขึ้นในที่อื่น 

ตัวอย่างต่อไปนี้จะสมมติว่า คุณมีสายเคเบิลโมเด็ม null แนบระหว่าง COM1 และ COM2 สิ่งนี้จำเป็นสำหรับการทดสอบเท่านั้นApp1 เป็นแอพลิเคชัน COM1, App2 เป็นแอพลิเคชัน COM2 

ขั้นตอนการสร้าง App1

  1. สร้างโครงการ EXE มาตรฐานใหม่ Form1 จะถูกสร้างขึ้น โดยค่าเริ่มต้น
  2. เลือกคอมโพเนนต์จากเมนูโครงการ การตรวจสอบ "Microsoft สารควบคุม" และคลิกตกลง
  3. เพิ่มตัวควบคุม MSCOMM ไปยังแบบฟอร์ม
  4. เพิ่มกล่องข้อความและ CommandButton ที่ไปยังแบบฟอร์ม เปลี่ยนคุณสมบัติของกล่องข้อความแบบหลายบรรทัดเป็น True
  5. เพิ่มรหัสต่อไปนี้ลงในหน้าต่างรหัสของ Form1:
    
          Private Sub Form_Load()
             Form1.Caption = "App1"
             With MSCOMM1
                .Handshaking = 2 - comRTS
                .RThreshold = 1
                .RTSEnable = True
                .Settings = "9600,n,8,1"
                .SThreshold = 1
                .PortOpen = True
                ' Leave all other settings as default values.
             End With
             Command1.Caption = "&Send"
             Text1.Text = "Test string from App1 "
          End Sub
    
          Private Sub Command1_Click()
             MSComm1.Output = Text1.Text
          End Sub
    
          Private Sub Form_Unload(Cancel As Integer)
             MSComm1.PortOpen = False
          End Sub
    
          

ขั้นตอนการสร้าง App2

  1. เริ่มอินสแตนซ์ใหม่ของ Visual Basic
  2. สร้างโครงการ EXE มาตรฐานใหม่ Form1 จะถูกสร้างขึ้น โดยค่าเริ่มต้น
  3. เลือกคอมโพเนนต์จากเมนูโครงการ การตรวจสอบ "Microsoft สารควบคุม" และคลิกตกลง
  4. เพิ่มตัวควบคุม MSCOMM ไปยังแบบฟอร์ม
  5. เพิ่มกล่องข้อความไปยังแบบฟอร์ม เปลี่ยนคุณสมบัติของกล่องข้อความแบบหลายบรรทัดเป็น True ขยาย TextBox ดังนั้นจะครอบคลุมส่วนใหญ่ของแบบฟอร์ม เป็นคุณจะสามารถแสดงข้อมูลทั้งหมดได้รับในนั้น
  6. เพิ่มรหัสต่อไปนี้ลงในหน้าต่างรหัสของ Form1:
    
          Private Sub Form_Load()
             Form1.Caption = "App2"
             With MSComm1
                .CommPort = 2
                .Handshaking = 2 - comRTS
                .RThreshold = 1
                .RTSEnable = True
                .Settings = "9600,n,8,1"
                .SThreshold = 1
                .PortOpen = True
                ' Leave all other settings as default values.
             End With
             Text1.Text = ""
          End Sub
    
          Private Sub Form_Unload(Cancel As Integer)
             MSComm1.PortOpen = False
          End Sub
    
          Private Sub MSComm1_OnComm()
             Dim InBuff As String
    
             Select Case MSComm1.CommEvent
             ' Handle each event or error by placing
             ' code below each case statement.
    
             ' This template is found in the Example
             ' section of the OnComm event Help topic
             ' in VB Help.
    
             ' Errors
                Case comEventBreak   ' A Break was received.
                Case comEventCDTO    ' CD (RLSD) Timeout.
                Case comEventCTSTO   ' CTS Timeout.
                Case comEventDSRTO   ' DSR Timeout.
                Case comEventFrame   ' Framing Error.
                Case comEventOverrun ' Data Lost.
                Case comEventRxOver  ' Receive buffer overflow.
                Case comEventRxParity   ' Parity Error.
                Case comEventTxFull  ' Transmit buffer full.
                Case comEventDCB     ' Unexpected error retrieving DCB]
    
             ' Events
                Case comEvCD   ' Change in the CD line.
                Case comEvCTS  ' Change in the CTS line.
                Case comEvDSR  ' Change in the DSR line.
                Case comEvRing ' Change in the Ring Indicator.
                Case comEvReceive ' Received RThreshold # of chars.
                   InBuff = MSComm1.Input
                   Call HandleInput(InBuff)
                Case comEvSend ' There are SThreshold number of
                               ' characters in the transmit buffer.
                Case comEvEOF  ' An EOF character was found in the
                               ' input stream.
             End Select
    
          End Sub
    
          Sub HandleInput(InBuff As String)
             ' This is where you will process your input. This
             ' includes trapping characters, parsing strings,
             ' separating data fields, etc. For this case, you
             ' are simply going to display the data in the TextBox.
             Text1.SelStart = Len(Text1.Text)
             Text1.SelText = InBuff
          End Sub
    
          

ขั้นตอนในการเรียกใช้โปรแกรมประยุกต์

  1. กดแป้น F5 หรือคลิกปุ่มเรียกใช้ในแต่ละโครงการ คุณจะต้องย้าย apps ล้อมรอบเพื่อให้คุณสามารถเห็นหน้าต่างทั้งสองทำงานในเวลาเดียวกัน
  2. ตรวจสอบให้แน่ใจว่า คุณมีสายเคเบิลโมเด็ม null มาตรฐานระหว่าง COM1 และ COM2 หรือเปลี่ยนแปลงคุณสมบัติ MSComm1.Port ให้ตรงกับพอร์สารที่คุณกำลังใช้
  3. คลิกการ CommandButton บน App1 คุณควรเห็นข้อความ "ทดสอบสตริ App1" ปรากฏในกล่องข้อความของ App2 สำหรับแต่ละปุ่มให้คลิก

1 ความคิดเห็น:

  1. industry colleagues, miu miu replica handbags must be gold , platinum and other precious metals and mother of pearl dial, leather strap, matched with diamonds to be able to achieve perfection. This phase of the moon Montblanc diamond ladie replica rolex discount gold replica watch uk since ancient times, the moon and the stars symbolize the life cycle, it is the way the replica breitling watches sale ancient .

    ตอบลบ