thanks a lot for all your help i finally got eberything lined up and working
i understood that serial.read returns bytes not integer and that to compare strings we need strcmp()
thanks
the code which FINALLY worked is
Private Sub Form_Load()
MSComm1.CommPort = 3
' 9600 baud, no parity, 8 data, and 1 stop bit.
MSComm1.Settings = "9600,N,8,1"
' Tell the control to read entire buffer when Input
' is used.
MSComm1.EOFEnable = True
MSComm1.InputLen = 0
MSComm1.RThreshold = 4 ' IMPORTANT change this to change the number of characters to be received
MSComm1.SThreshold = 3
' Open the port.
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Dim inbuffer() As Byte 'Declare an array of bytes
Dim b() As Byte
Dim i As Long
Dim s As String
Select Case Me.MSComm1.CommEvent
Case comEvReceive
ReDim inbuffer(Me.MSComm1.InBufferCount) 'Specify the size of the array. InBuffercount gives the number of characters in the InputBuffer
inbuffer = Me.MSComm1.Input 'Read the InputBuffer
For i = 0 To UBound(inbuffer) 'Ubound(inbuffer) gives the upper bound of the array, which is equal to the number of characters in the InputBuffer
Me.txtreceive.Text = Me.txtreceive.Text & Chr$(inbuffer(i)) 'TxtReceive is a text box
Next i
s = StrConv(inbuffer, vbUnicode)
End Select
MsgBox s
Dim string2 As String
string2 = "1234"
If StrComp(s, string2, vbTextCompare) = 0 Then
MsgBox s
MSComm1.Output = "YES"
End
Else
MSComm1.PortOpen = False
End If
End Sub
and in the arduino side
char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character
int ledpin =13;
void establishContact();
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
Serial.flush();
pinMode(ledpin, OUTPUT); // digital sensor is on digital pin 2
establishContact(); // send a byte to establish contact until receiver responds
}
void loop()
{
// if we get a valid byte, read analog ins:
while(Serial.available() > 0) // Don't read unless
// there you know there is data
{
//digitalWrite(ledpin,HIGH);
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
}
if (strcmp(inData,"YES")==0)
{
digitalWrite(ledpin,HIGH);
Serial.print(inData);
}
}
void establishContact()
{
// while (Serial.available() <= 0)
//{
Serial.print("1234");
// send an initial string
delay(300);
// }
}
thank you