Serial comunication problem

Hi people !
I'm really intrigate with this problem.
I'm using powershell script for comunication between pc and arduino but i think that i'm having encoding problem or something like that, cuz, for exemple, when I pass a number from shell scritp, arduino receive any other thing but not this number.
Can you helpe me ?

Shell script:

$port= new-Object System.IO.Ports.SerialPort COM3,9600,None,8,one
$port.DtrEnable="true"

$port.open()

$port.WriteLine(1)
$port.Close()

Arduino code:

#include<LiquidCrystal.h>

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

int var =0;

void setup()
{

lcd.begin(16, 2);
Serial.begin(9600);

}

void loop()
{
lcd.clear();

var=Serial.read();
lcd.setCursor(0, 0);
lcd.print(var);
delay(1000);

}

What you're seeing on the LCD is the ASCII codes for the characters you sent to the Arduino. You can fix this by using lcd.write() instead of lcd.print(). print() uses the data type of the parameter passed to it to determine how it should be displayed. Since the data type of var is int, print() thinks it should display the numerical value. If the type of var was char, print() would instead display the character associated with the ASCII code for the value of var. write() doesn't do any of that fancy stuff. It assumes that the parameter is an ASCII code and always displays the character representation of that code.

Please use code tags when posting code.

This line reads a "character" regardless of whether one is actually available.

var=Serial.read();

Serial Input Basics

Thaks for your help. I understood what you said. That was really interesting cuz i didn't know about that.
But, as you can see, at the shell script, i'm passing number 1(int). Now its showing on lcd -1 following of some squares. I didn't change the code.

That's easy

You aren't waiting to update the LCD screen until you have data in the serial RX buffer. Because of this, when you call var=Serial.read(); you have a high likelihood of getting a "no data in the buffer" error code '-1'. Even if you do get the char you sent with the script, it will be overwritten by a '-1' when you call var=Serial.read(); during the next iteration of loop()

Try this instead:

#include<LiquidCrystal.h>

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

int var =0;

void setup()
{
 
  lcd.begin(16, 2);
  Serial.begin(9600);
  
}
 
void loop()
{
    if(Serial.available())
    {
        lcd.clear();
        var=Serial.read();
        lcd.setCursor(0, 0);
        lcd.print(var);
    }
}

I changed the code to:

#include<LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
int var =0;
void setup()
{
 
  lcd.begin(16, 2);
  Serial.begin(9600);
  
}
 
void loop()
{
  lcd.clear();
 if(Serial.available()>0)
 {
  var = Serial.read();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(var);
  delay(1000);
 } /*
 else
 {
   
   lcd.setCursor(0, 0);
   lcd.print("No connection");
   lcd.setCursor(0,1);
   lcd.print("available!");
 }*/

But still showing the squares.

put the lcd.clear(); inside the if statement and get rid of the else part - or else you will be clearing out the LCD screen when you don't want to

Good observation ! I did that, but problem persist. Thanks for your help guys. Is there another way to fix that ?