1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| Imports Excel = Microsoft.Office.Interop.Excel
Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
'Imports System.Text.RegularExpressions
Public Class Form1
'Déclaration des variables Excel
Dim oExcel As Excel.Application 'déclaration de ma nouvelle application excel
Dim oBook As Excel.Workbook 'Déclaration de mon tableur Excel
Dim oSheet As Excel.Worksheet 'Déclaration de ma feuille Excel
Dim Compteur As Long = 2
Dim myPort As Array 'COM Ports detected on the system will be stored here
Dim nom As String 'variable de nom de fichier
Dim buffer As String 'Données reçues par le port série de la part de l'arduino
Dim sous_chaine1 As String
Dim sous_chaine2 As String
Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
Public Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
SerialPort4.PortName = cmbPort.Text 'Set SerialPort1 to the selected COM port at startup
SerialPort4.BaudRate = cmbBaud.Text 'Set Baud rate to the selected value on
'Other Serial Port Property
SerialPort4.Parity = IO.Ports.Parity.None
SerialPort4.StopBits = IO.Ports.StopBits.One
SerialPort4.DataBits = 8
SerialPort4.Open()
'---------Excel-------------
'Ouverture de l'application
oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
'/!\Ligne importante: Permet la visibilité de la création de la fiche /!\
'Ouverture d'un nouveau classeur
oBook = oExcel.Workbooks.Add
oSheet = oBook.ActiveSheet
'Ouverture d'une nouvelle feuille
oSheet.Cells(1, 1) = "Valeur Courant (mA)"
oSheet.Cells(1, 2) = "Valeur Temperature (°C)"
'compteur = 2 'réinitialisation de mon compteur de boule à chaque nouvelle ouverture de tableur
'---------Excel-------------
btnConnect.Enabled = False 'Désactive bouton connexion
btnDisconnect.Enabled = True 'Active bouton déconnexion
'--------Ecriture dans la feuille Excel---------
End Sub
Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
SerialPort4.Close() 'Close our Serial Port
' fichier = InputBox("Indiquez le nom du fichier")
nom = InputBox("Indiquez le nom du fichier")
'----------Excel----------
oBook.SaveAs("C:\Users\Johan\AppData\Roaming\Microsoft\Windows\Libraries\Documents.library-ms\" & nom) 'Sauvegarde du document
oExcel.ActiveWorkbook.Close() 'on ferme le classeur
oExcel.Visible = False 'on ferme Excel
'Désallocation mémoire
oExcel = Nothing
oBook = Nothing
oSheet = Nothing
'----------Excel----------
btnConnect.Enabled = True
btnDisconnect.Enabled = False
End Sub
Private Sub SerialPort4_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort4.DataReceived
'Partie sur laquelle un problème se pose
'---------------------------------------------------------------------------------------------------------
Dim Incoming As String = SerialPort4.ReadLine() '
'Ecriture dans la feuille Excel
oSheet.Cells(Compteur, 1) = sous_chaine1
oSheet.Cells(Compteur, 2) = sous_chaine2
'Compteur de boucle
Compteur = Compteur + 1
'Pause systeme 2s
System.Threading.Thread.Sleep(2000)
'--------Ecriture dans la feuille Excel---------
'-----------------------------------------------------------------------------------------------------------------------
End Sub
Private Function UnicodeBytesToString(ByVal SerialPort4() As Byte) As String
Return System.Text.Encoding.Unicode.GetString(SerialPort4)
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available
cmbBaud.Items.Add(9600) 'Populate the cmbBaud Combo box to common baud rates used
cmbBaud.Items.Add(19200)
cmbBaud.Items.Add(38400)
cmbBaud.Items.Add(57600)
cmbBaud.Items.Add(115200)
For i = 0 To UBound(myPort)
cmbPort.Items.Add(myPort(i))
Next
cmbPort.Text = cmbPort.Items.Item(0) 'Set cmbPort text to the first COM port detected
cmbBaud.Text = cmbBaud.Items.Item(0) 'Set cmbBaud text to the first Baud rate on the list
btnDisconnect.Enabled = False 'Initially Disconnect Button is Disabled
End Sub
Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged
If SerialPort4.IsOpen = False Then
SerialPort4.BaudRate = cmbBaud.Text 'pop a message box to user if he is changing baud rate
Else 'without disconnecting first.
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub
Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
If SerialPort4.IsOpen = False Then
SerialPort4.PortName = cmbPort.Text 'pop a message box to user if he is changing ports
Else 'without disconnecting first.
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub
End Class |
Partager