@PaulS and Robin2: Thanks for replying. I've been looking at different methods to do this kind of serial transfer and I think I'll list them down here for folks who come and check this later as well. But I'd some small doubts in each of these techniques, which I believe you can help me with.
-
Sending data through
bytedata type:
byte b = 234; //limited to value 255
serialPort.write(b);
Arduino side:
int i = Serial.read(); //read the byte into int data type
What will happen if I try to assign char by passing byte from C#?
char c = Serial.read(); //
A byte typically means any data which can be binary/hex etc. as well. When I'd written byte b= 234; in the above program, is there an implicit conversion from int to byte in C#?
-
Sending multi-digit integers through
bytedata type
In C#, I can use BitConverter class to convert int (4 bytes in C#) to convert to 4 bytes of data and write to Serial as:
byte b[]; //where the values to b will be assigned by BitConverter class.
serialPort.write(b); //writes 4 bytes to serial
Arduino side:
How do I concatenate these 4 bytes to get my integer now in this Arduino C++ program?
-
Sending multi-digit integers through strings data type:
We can build a string (which is a collection of chars) and end it with a line feed or newline character explicitly in my program. Then, we can transfer this string over Serial which can be read by Arduino program to build an integer using decimal weight.
string s = "123\n"
serialPort.write(s);
Arduino side:
char incomingByte;
int incomingValue;
void loop(){
if(Serial.available() > 0)
incomingValue = 0;
while(1){
incomingByte = Serial.read();
if(incomingByte == '\n') break;
integerValue = (incomingByte - 48) + integerValue;
}
}
-
Serial.parseInt()as PaulS mentioned. Don't know how it's used. Maybe PaulS can throw some light.
What's the most efficient way to send the data out the above? Assume that the length of digits in the integer will not be known and the Arduino program should be able to accept values >255.