關於我自己

2012年6月12日 星期二

NXT利用藍芽Com RS232技術傳送指令到PC

著個步驟~是我要利用NXT傳送指令到Android手機的先前測試工具

1.寫在NXT寫入程式 (利用SendRemoteString涵數)
2.
因為我的作業系統是Windows 7 所以沒有hyperterminal.exe程式


所以我到此網站下載
(你會在網站內看到zip-package from here等字串,按下去就可以下載)
http://digitizor.com/2009/08/29/install-winxp-hyperterminal-client-on-windows-vista-or-windows-7-free/

3.下載後解壓縮你會看到
4.在此之前先用電腦連結NXT
會在工作管理員產生Port 連結埠~然後再裝置跟印表機的屬性你會看到硬體的連接埠
我的是Com3 ~ 正確來說Com3是傳輸,而Com7接收
就像我在上一遍"Android 藍芽連結NXT 控制馬達"裡面有貼NXT螢幕的連接符號
傳送與接收,因為太懶的解釋,直接寫有連接沒連接


5.連接好之後~請執行hyperterminal.exe 去設置Com   (記得要連之前要開NXT的主機)
連好之後會出現 ~  11€    11€    11€   11  (因為我在NXT 按了4次執行)

做個簡單的應用 ~ 顏色偵測
目的:利用NXT藉由藍芽傳送值到電腦
所以用顏色感測器當做示範
當顏色感測器 ~收到值之後借由NXT將字串傳輸到電腦

影片:


程式碼:
 task main()
{

  int actual_position=0;
  SetSensorColorFull(S2);

  while (true)
  {
   ClearScreen( );
  actual_position = Sensor(S2);
  switch(actual_position)
  {
  case 1:     //Black Color
       TextOut(50,LCD_LINE2,"Black\n");
       SendRemoteString( 0, 0, "Black\n" );
  break;
   case 2:     //Blue Color
       TextOut(50,LCD_LINE2,"Blue\n");
        SendRemoteString( 0, 0, "Blue\n" );
  break;
   case 3:     //Green Color
       TextOut(50,LCD_LINE2,"Green\n");
        SendRemoteString( 0, 0, "Green\n" );
  break;
   case 4:     //Yellow Color
       TextOut(50,LCD_LINE2,"Yellow\n");
        SendRemoteString( 0, 0, "Yellow\n" );
  break;
   case 5:     //Red Color
       TextOut(50,LCD_LINE2,"Red\n");
        SendRemoteString( 0, 0, "Red\n" );
  break;
   case 6:     //White Color
       TextOut(50,LCD_LINE2,"White\n");
        SendRemoteString( 0, 0, "White\n" );
  break;
  }
  Wait(1000);
  }
}



使用Visual Basic 6.0 程式來接收NXT傳來的值

影片:

介面圖:

VB 主程式碼:
被空白字元所煩惱,一直刪之不去,利用過Trim刪除空白涵式跟去查空白的Acsii有查到13跟9
利用replace(字串,Chr(13),"")都沒辦法解決,只好用著種煩人的寫法,有點冗長


Private Sub Command1_Click()

MSComm1.CommPort = 3     '開啟的連接埠
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeBinary
If (MSComm1.PortOpen = True) Then MSComm1.PortOpen = False
MSComm1.PortOpen = True
Timer1.Enabled = True
End Sub

Private Sub Command2_Click()
MSComm1.PortOpen = False '關閉
End Sub


Private Sub Timer1_Timer()
Dim ColorValue As Variant
Dim buf() As Byte
Dim v1, v2, v3, v4, v5, v6 As Integer
Dim PictureColor As String

If MSComm1.InBufferSize > 0 Then
   ColorValue = MSComm1.Input
   buf = ColorValue
 
   For i = 0 To UBound(buf)
       If Chr(buf(i)) = " " Then
       Else
       Text1.Text = Text1.Text & Chr(buf(i))
       Text2.Text = Text2.Text & Chr(buf(i))
       End If
   Next i
  
   Text1.Text = Text1.Text & vbCrLf

   v1 = InStr(1, Text2.Text, "Red", 1)
   v2 = InStr(1, Text2.Text, "Black", 1)
   v3 = InStr(1, Text2.Text, "Blue", 1)
   v4 = InStr(1, Text2.Text, "Green", 1)
   v5 = InStr(1, Text2.Text, "Yello", 1)
   v6 = InStr(1, Text2.Text, "White", 1)
   Label1.Caption = Text2.Text
   If v1 > 0 Then
   Picture1.BackColor = RGB(255, 0, 0) '紅色
   End If
   If v2 > 0 Then
   Picture1.BackColor = RGB(0, 0, 0) '黑色
   End If
   If v3 > 0 Then
   Picture1.BackColor = RGB(0, 0, 255) '藍色
   End If
   If v4 > 0 Then
   Picture1.BackColor = RGB(0, 255, 0) '綠色
   End If
   If v5 > 0 Then
   Picture1.BackColor = RGB(255, 255, 0) '黃色
   End If
   If v6 > 0 Then
   Picture1.BackColor = RGB(255, 255, 255) '白色
   End If
   Text2.Text = ""
  
End If

End Sub

======================================
VB 6.0 是使用MSCOMM元件(我比較常用~因為幾乎用拉的省很多時間)
C++ 則是用Createfile涵式Readfile涵式WriteFile涵式
一般打Google打Createfile就會有很多相關資訊

如果要用免費的軟體C++可以考慮Dev-C ++

如果想用有圖形介面又免費的可以考慮
Microsoft Visual Studio 2008 Express 版 SP1
http://www.microsoft.com/zh-tw/download/details.aspx?id=20682

裡面有VB.NET跟C#可以用的說

VB.NET開啟Com Port的方法(VB.NET寫法幾乎跟C#都是大同小異)

VB.NET 開打Com的範例
Imports System.IO.Ports
Imports System.Threading
Imports System.Text
Public Class Form1
    Public myPort As New System.IO.Ports.SerialPort

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       '設定參數
        With myPort
            .BaudRate = 9600
            .DataBits = 8
            .Parity = Parity.None
            .StopBits = StopBits.One
            .PortName = "COM3"
            '.ReadTimeout = 1000
            .Encoding = System.Text.Encoding.ASCII
            .ReadTimeout = 1000
        End With
        Dim read_word As String
        read_word = ""
        Try
            myPort.Open() '開啟

            '    myPort.DtrEnable = True
            Label1.Text = myPort.IsOpen
            myPort.Write("AT" & vbCrLf)   '寫入 AT等字串


            myPort.RtsEnable = True
            delay(5000)
            read_word = myPort.ReadExisting()
            TextBox2.Text = read_word
            myPort.Close()
            Label1.Text = myPort.IsOpen

            ' TextBox2.Text &= myPort.ReadExisting
            '  TextBox2.Text &= myPort.ReadLine & vbCrLf
            '   Timer1.Enabled = True
        Catch ex As Exception
            ex.Message.ToString()
        End Tr

 '我是習慣用時間來接收 ~ 其實還有其他COM接收方法 DataReceived  ~ 只要有有訊息就觸動

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim InStr As String

        InStr = ""
        Try
            myPort.ReadTimeout = 1000
            InStr = myPort.ReadExisting()
            If InStr.Length = 0 Then
                Exit Sub
            Else
                TextBox2.Text += InStr
                TextBox2.Text += "ok"
            End If
        Catch ex As Exception
            MessageBox.Show("讀取錯誤:" + ex.ToString, "錯誤通知", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try

    End Sub

沒有留言:

張貼留言