Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to connect with a temp sensor that use ASCII commands ... when i use arduino serial monitor to test commands the sensor respond well, but when i try to use C# i get different ressult.

Through arduino serial monitor (all great):
The command that i use: ?2T
The sensor Respond: !2T0501.3
where "!2T" is a response confirmation and "0501.3" is the actual temperature meassure.

But when i use C# (problem):
The command that i use: ?2T\r "because i need the carriage return"
The sensor Respond: i??X?Y?K5

i think that is a very basic format problem but i kind of new in C# and i don't know how to fix it.

What I have tried:

public Form1()
{
    InitializeComponent();
    serialPort1.Open();
    //serialPort1.DtrEnable = true;
    //serialPort1.RtsEnable = true;
    serialPort1.ReadTimeout = 500;

    timer1.Interval = 1000;
    timer1.Tick += new EventHandler(timer1_Tick);
    timer1.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)
{
    if (!serialPort1.IsOpen)
        serialPort1.Open();

    serialPort1.Write("?2T\r");
    Task.Delay(50);

    textBox1.Text = serialPort1.ReadLine();
}

private void button1_Click(object sender, EventArgs e)
{
    serialPort1.DiscardOutBuffer();
    serialPort1.DiscardInBuffer();
    serialPort1.BaseStream.Dispose();
    serialPort1.Close();
    this.Close();
}
Posted
Comments
[no name] 27-Apr-21 17:14pm    
You should check .BytesToRead and also do a straight read. Set a breakpoint and check the result (text / hex).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900