I have been working with Arduino serial communication for a while, and have done quite a lot of reading on the topic, however I still get confused as to how the data is actually transmitted.
To the best of my understanding, this is more or less how the serial communication works, however some steps are probably wrong so please correct me:
(I am especially confused about when is the data converted to ASCII values and how it is interpreted)
I send a message from Arduino using:
void encodage(){
while(Serial.available()) // read the incoming byte:
{
String caractere = String(Serial.read(), BIN);
while (caractere.length()<8)
{caractere=String(0)+caractere; }
Serial.print(" msg on BIN[");Serial.print(caractere);
Serial.println("]");
messageEncode += caractere; } }//ajoute les 8 bits à ma grande chaine binaire
And the result in serial monitor that I see come on as follows in the picture I do not know who came number I want a "1010" to get rid of them because they spoil the sending of my bit
1. Connect your Arduino UNO with PC using USB Port.
2. Upload the following sketch.
void setup()
{
Serial.begin(9600);
}
void loop()
{
byte n = Serial.available();
if (n !=0 ) //at least 1 charcater has come from Serial Moniotr
{
char x = Serial.read(); //read arrived charcater from Buffer and save in x
Serial.println(x); //send the charcater back to Serial Moniotr
Serial.print(x, BIN); //shows: 1100001
Serial.println(x, HEX); //shows: 61
}
}
3. Open the Serial Monitor; you see something like Fig-1.
Figure-1: Serial Monitor
4. In the InputBox of the Serial Monitor, enetr a from the Keyboard and then click on the Send button. Check that the OutputBox of Serial Monitor shows as Fig-2.
Figure-2:
5. Interpretation of the displayed message of Fig-2. (1) When we have entered a in the InputBox and then clicked on the Send button, the bit pattern 01100001 (called ASCII code of a, Fig-3) is transmitted towards UNO.
(2) UNO has received the code and saved in variable char x;. The UNO has sent back the same code to Serial Monitor by executing the code Serial.println(x); as a result, a has appeared on the OutputBox of Serial Monitor.
(3) UNO has also executed this code: Serial.println(x, BIN); as a result, the bit pattern that it received from Serial Monitor (01100001) has appered again on the OutputBox of Serial Monitor with missing leading zero (0). Note that the Serial Monitor does not print keading zero (0) unless you tell it to do so.
(4) Uno has executed this code: Serial.println(x, HEX); as a result, the hex equivalent of 01100001 (61) has appeared on the OutputBox of Serial Monitor.