Controllo elettrovalvola 9v con arduino

tia89p:
Appena riesco ci provo, ma il oncomm è in pratica un trigger che controlla la porta seriale? quindi la funzione scritta in oncomm va in esecuzione solo quando arriva qualcosa?

Si, però è controllato da altre due proprietà e bisogna verificare che l'evento non sia scattato per degli errori.
Di solito imposto:

MsCom.InputLen = 1
MsCom.RThreshold = 0
MsCom.SThreshold = 0

Poi

Private Sub MsCom_OnComm()
  If MsCom.CommEvent <> comEvReceive Then     ' se errore
    Select Case MsCom.CommEvent
      Case comEventBreak:    Assert "A Break was received."    ' errors
      Case comEventCDTO:     Assert "CD (RLSD) Timeout."
      Case comEventCTSTO:    Assert "CTS Timeout."
      Case comEventDSRTO:    Assert "DSR Timeout."
      Case comEventFrame:    Assert "Framing Error"
      Case comEventOverrun:  Assert "Data Lost."
      Case comEventRxOver:   Assert "Receive buffer overflow."
      Case comEventRxParity: Assert "Parity Error."
      Case comEventTxFull:   Assert "Transmit buffer full."
      Case comEventDCB:      Assert "Unexpected error retrieving DCB]"   ' Events
      Case comEvCD:          Assert "Change in the CD line."
      Case comEvCTS:         Assert "Change in the CTS line."
      Case comEvDSR:         Assert "Change in the DSR line."
      Case comEvRing:        Assert "Change in the Ring Indicator."
      Case comEvSend:        Assert "There are SThreshold number of characters in the transmit buffer."
      Case comEvEOF:         Assert "An EOF charater was found in                ' the input stream"
    End Select
    Exit Sub
  End If
  ' Qui ho i dati, char per char
  Dim l_Chr As String
  l_Chr = MsCom.Input
  ...
  l_Curr = l_Curr & l_Chr     ' l_Curr variabile globale
  ...
  If l_Chr = K_EOT Then ....   ' carattere fine messaggio
End Sub

Ma di solito mi organizzo un protocollo, ovvero mando i dati mettendo davanti un carattere prestabilito, poi i dati e poi alla fine un altro carattere, esempio #messaggio@ #=>inizio @=>fine msg

P.S. allego un esempietto tra vb6 e arduino, vb riceve valore potenziometro e può spedire ad arduino stato led.

VB6_Serial.ino (832 Bytes)

Vb6.rar (12.7 KB)