Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPublic Class Form1
Dim inputData As Long
Private Sub Form1_Load() Handles MyBase.Load
Try
SerialPort1.Open()
Catch ex As Exception
Me.Text = ex.Message
End Try
End Sub
Private Sub Button2_Click() Handles Button2.Click
SerialPort1.Close()
End Sub
Sub Calcul_checksum()
Dim TabEnvoyé As Byte() = New Byte(11) {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
TabEnvoyé(0) = &H2
TabEnvoyé(1) = &H3F
TabEnvoyé(2) = &H30
TabEnvoyé(3) = &H31
TabEnvoyé(4) = &H31
TabEnvoyé(5) = &H43
TabEnvoyé(6) = &H52
TabEnvoyé(7) = &H49
TabEnvoyé(8) = &H46
TabEnvoyé(9) = &H46
TabEnvoyé(10) = &H3D
TabEnvoyé(11) = &H3
Dim Somme As Integer = 0
For Each x As Byte In TabEnvoyé
Somme = Somme + x
Next
Dim bt As Byte()
bt = BitConverter.GetBytes(Somme)
Dim a1 As String = Hex(bt(0))
Dim aa As String = Hex(Somme)
SerialPort1.Write(TabEnvoyé, 0, 12)
End Sub
Private Sub serialport1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'déclaration des variables
Dim budd(13) As Byte
Dim strRecu As String = Nothing
' lecture de la trame recu "sur 12 bytes"
SerialPort1.Read(budd, 0, 12)
' fermeture du port
SerialPort1.Close()
For ii As Integer = 0 To 11
strRecu = strRecu & ":" & Hex(budd(ii))
Next
' Affichage de la trame
MessageBox.Show(strRecu)
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
End Class
Imports System.IO.Ports Public Class CIMOserie Private sp As SerialPort Private debutTrame As String = Chr(2) Private finTrame As String = Chr(3) Property numAutom As String = "1" 'Numéro de l'automate Sub New(ByVal portCom As String) Dim automate As New CIMOserie("COM37") sp = New SerialPort(portCom, 19200, Parity.None, 8, StopBits.One) sp.RtsEnable = True sp.Open() End Sub 'Calcul du Checksum Private Function CalculerCRC(ByVal trame As String) As String Dim crc As Integer = 0 For i = 0 To trame.Length - 1 crc += (Asc(trame(i))) Next Return (crc Mod 256).ToString("X2") End Function 'Cette fonction permet de lire, pas d'écrire (il faudra rajouter les données après la voie, comme expliqué dans la doc) Private Function EnvoyerTrame(ByVal cmde As String, ByVal voie As String) As String Dim _trame As String = debutTrame + "?" + numAutom + "1" + cmde + voie _trame += CalculerCRC(_trame) + finTrame sp.Write(_trame) Return sp.ReadTo(finTrame) + finTrame End Function 'Fonction exemple pour lire toutes les Entrées Public Function LireToutesLesEntrees() As String Return EnvoyerTrame("CRI", "FF") End Function 'Fonction exemple pour lire une Sortie (par ex LireUneSortie("02")) Public Function LireUneSortie(ByVal voie As String) As String Return EnvoyerTrame("CRQ", voie) End Function End Class
Imports System.IO.Ports Public Class CIMOserie Private sp As SerialPort Private debutTrame As String = Chr(2) Private finTrame As String = Chr(3) Property numAutom As String = "1" 'Numéro de l'automate Sub New(ByVal portCom As String) 'Dim automate As New CIMOserie("COM37") (par exemple) sp = New SerialPort(portCom, 19200, Parity.None, 8, StopBits.One) sp.RtsEnable = True sp.Open() End Sub 'Calcul du Checksum Private Function CalculerCRC(ByVal trame As String) As String Dim crc As Integer = 0 For i = 0 To trame.Length - 1 crc += (Asc(trame(i))) Next Return (crc Mod 256).ToString("X2") End Function 'Cette fonction permet de lire, pas d'écrire (il faudra rajouter les données après la voie, comme expliqué dans la doc) Private Function EnvoyerTrame(ByVal cmde As String, ByVal voie As String) As String Dim _trame As String = debutTrame + "?" + numAutom + "1" + cmde + voie _trame += CalculerCRC(_trame) + finTrame sp.Write(_trame) Return sp.ReadTo(finTrame) + finTrame End Function 'Fonction exemple pour lire toutes les Entrées Public Function LireToutesLesEntrees() As String Return EnvoyerTrame("CRI", "FF") End Function 'Fonction exemple pour lire une Sortie (par ex LireUneSortie("02")) Public Function LireUneSortie(ByVal voie As String) As String Return EnvoyerTrame("CRQ", voie) End Function End Class