Ftd2xx_net

Bonsoir,

Vu le temps que j’ai passé a grappiller de petites infos de ci de la pour arriver a gérer la carte GCE. Voici mon code en VB.NET en utilisant le driver FTD2XX_Net (ca fait plus propre que les vieux exemples de FTDI).

Et vraiment un grand merci a ce post en C# qui m’a mis sur la bonne route http://ctieware.eng.monash.edu.au/twiki/pub/WSensornets/SecondGenerationPacketRadioModule/Window1.xaml.cs.html
Le code ci-dessous est juste légèrement adapté a notre besoin.

J’espère qu’il servira au plus grand nombre.

Bonne soirée,
Phil.

<img src=‹ /uploads/default/original/1X/56fd5962c295ee1427c639968f9115afbae9044c.png › width=‹ 605 › height=‹ 409 › title=‹ Apercu de l’appli. ›>

Imports FTD2XX_NET
Imports System.ComponentModel
Imports System.Threading
Imports System.Reflection

Public Class Form1
    Public strRX As String = ""
    Private Ftdi_Device As FTDI = New FTDI

    ' Connection handle to the FTDI chippy
    Private DataReceived As EventWaitHandle = New EventWaitHandle(False, EventResetMode.AutoReset)

    ' Handle for data received events
    'Private DataHandler As BackgroundWorker = New BackgroundWorker
    Public Delegate Sub DelegateLog(ByVal text As String)

    ' Second thread for processing data received
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        '  AddHandler DataHandler.DoWork, AddressOf Me.DataHandler_DoWork
        ComboBaud.Items.Add("9600")
        ComboBaud.Items.Add("2400")
        ComboBaud.Items.Add("4800")
        ComboBaud.Items.Add("14400")
        ComboBaud.Items.Add("19200")
        ComboBaud.SelectedIndex = 0
    End Sub
    Private Sub ButtonScan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonScan.Click
        Dim device_count As UInt32 = 0
        Dim ftStatus As FTDI.FT_STATUS = ftdi.FT_STATUS.FT_OK
        ListDevices.Items.Clear()
        ' Try to connect to the driver, get some info
        If (Ftdi_Device.GetNumberOfDevices(device_count) <> ftdi.FT_STATUS.FT_OK) Then
            GUITextAppend(("Failed to get number of devices! (" + (ftStatus.ToString + ")")))
            Return
        End If
        GUITextAppend(("Number of FTDI Devices : " + (device_count.ToString + "")))
        ' Parse devices to find out about them
        Dim DeviceList() As FTDI.FT_DEVICE_INFO_NODE = New FTDI.FT_DEVICE_INFO_NODE((device_count) - 1) {}
        If (Ftdi_Device.GetDeviceList(DeviceList) <> ftdi.FT_STATUS.FT_OK) Then
            GUITextAppend(("Failed to find device details! (" + (ftStatus.ToString + ")")))
            Return
        End If
        GUITextAppend("Details:")
        For Each device As FTDI.FT_DEVICE_INFO_NODE In DeviceList
            If (device.Description = "FT232R USB UART") Then
                GUITextAppend(("Type: " _
                                + (device.Type.ToString + ("" & Environment.NewLine & "ID: " _
                                + (device.ID.ToString + ("" & Environment.NewLine & "Serial Number: " _
                                + (device.SerialNumber + ("" & Environment.NewLine & "Description: " _
                                + (device.Description.ToString + "" & Environment.NewLine)))))))))
                ListDevices.Items.Add(device.SerialNumber)
            End If
        Next
        If (ListDevices.Items.Count > 0) Then
            ListDevices.SelectedIndex = 0
            ButtonConnect.Enabled = True
            Me.AcceptButton = ButtonConnect
        End If
    End Sub

    Private Sub ButtonConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonConnect.Click

        Dim device_count As UInt32 = 0
        Dim ftStatus As FTDI.FT_STATUS = ftdi.FT_STATUS.FT_OK
        Dim serial As String = ""
        ' Open a device called ...
        If (Ftdi_Device.OpenBySerialNumber(ListDevices.SelectedItem.ToString) <> ftdi.FT_STATUS.FT_OK) Then
            GUITextAppend(("Couldn't open Device! (" + (ftStatus.ToString + ")")))
            Return
        End If
        ' Find out which device we opened (in case more than one packet radio is plugged in).
        Ftdi_Device.GetSerialNumber(serial)
        GUITextAppend(("Opened " + (serial + "")))
        ' Set the Baud Rate
        If (Ftdi_Device.SetBaudRate(UInt32.Parse(ComboBaud.SelectedItem.ToString)) <> ftdi.FT_STATUS.FT_OK) Then
            GUITextAppend(("Failed to set baud rate (" + (ftStatus.ToString + ")")))
            Return
        End If
        ' Set 8 bit transmission, 1 stop bit etc
        If (Ftdi_Device.SetDataCharacteristics(ftdi.FT_DATA_BITS.FT_BITS_8, ftdi.FT_STOP_BITS.FT_STOP_BITS_1, ftdi.FT_PARITY.FT_PARITY_NONE) <> ftdi.FT_STATUS.FT_OK) Then
            GUITextAppend(("Failed to set characteristics (" + (ftStatus.ToString + ")")))
            Return
        End If
        GUITextAppend(("Baud Rate: " + (ComboBaud.SelectedItem.ToString + " | Stop Bits: 1 | Parity: None")))
        ' Set the RX/TX timeouts
        If (Ftdi_Device.SetTimeouts(0, 0) <> ftdi.FT_STATUS.FT_OK) Then
            GUITextAppend(("Failed to set timeouts (" + (ftStatus.ToString + ")")))
            Return
        End If
        ' Start the received data thread
        If Not BackgroundWorker1.IsBusy Then
            BackgroundWorker1.RunWorkerAsync()
        End If
        ' Subscribe to FT_EVENT_RXCHAR events
        Ftdi_Device.SetEventNotification(ftdi.FT_EVENTS.FT_EVENT_RXCHAR, DataReceived)
        GUITextAppend("" & Environment.NewLine & "Ready!")
        ButtonSend.Enabled = True
        Me.AcceptButton = ButtonSend
        TextChat.Focus()
    End Sub

    Private Sub ButtonSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSend.Click
        Dim ftStatus As FTDI.FT_STATUS = ftdi.FT_STATUS.FT_OK
        Dim bytes_written As UInt32 = 0
        ' Try to send the data
        If (Ftdi_Device.Write((TextChat.Text + ""), (TextChat.Text.Length), bytes_written) <> FTDI.FT_STATUS.FT_OK) Then
            GUITextAppend(("Failed to write text (" + (ftStatus.ToString + ")")))
            Return
        End If
        GUITextAppend(("Sent '" + TextChat.Text + "' (" + (bytes_written.ToString + " bytes)")))
        TextChat.Clear()
    End Sub
    Public Sub GUITextAppend(ByVal message As String)
        If Me.InvokeRequired Then
            Me.Invoke(New DelegateLog(AddressOf MethodBase.GetCurrentMethod), New Object() {message})
        Else
            With txtResults
                .Text += message + Environment.NewLine
                .SelectionStart = .Text.Length
                .ScrollToCaret()
            End With
        End If
    End Sub

    Private Sub backgroundworker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim ftStatus As FTDI.FT_STATUS = FTDI.FT_STATUS.FT_OK
        Dim readData As String = ""
        Dim numBytes As UInt32 = 0

        ' Loop forever
        Do While 1 = 1
            ' Wait for a FT_EVENT_RXCHAR event
            If DataReceived.WaitOne(100) Then
                ' Find out how many bytes have been received
                If (Ftdi_Device.GetRxBytesAvailable(numBytes) <> FTDI.FT_STATUS.FT_OK) Then
                    'TODO: Warning!!! continue If
                End If
                ' Read the bytes out
                If (Ftdi_Device.Read(readData, numBytes, numBytes) <> FTDI.FT_STATUS.FT_OK) Then
                    'TODO: Warning!!! continue If
                End If
                strRX = readData
                BackgroundWorker1.ReportProgress(1) ' To force an update of the ProgressChanged and display the strRx
            End If
            If BackgroundWorker1.CancellationPending Then
                Exit Do
            End If

        Loop
    End Sub

    Private Sub backgroundworker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        GUITextAppend("Reply: " & strRX)
    End Sub

End Class