0
votes

This is the situation:

We have 2 arduino connected by transceivers, Arduino A is sending a number 1, 2 or 3 and the Arduino B is just receiving the data by A to be read by a web application in visual studio.

Here is our code in our Arduino (where Bin1 is declared as equal to 1 for initial value):

void loop() {

rx_data = mySerial.read();

if(rx_data==1)
{
   bin1="1";
}
if(rx_data==2)
{
   bin1="2";
}
if(rx_data==3)
{
   bin1="3";
}

Serial.println(bin1);
}

Here is our code in our Visual Studio web app in reading the output of the serial in the COM:

private string binStatus = " ";
private static SerialPort _sensorPort;

private static void OpenSerialPort()
{
    _sensorPort = new SerialPort
    {
       BaudRate = 9600,
       PortName = "COM7",
       DtrEnable = true,
       RtsEnable = true
    };
}

private static void CloseSerialPort()
{
   _sensorPort.Close();
}

This is the part when the programs starts in Visual Studio:

protected void DoPostBack(object sender, EventArgs e)
{
   OpenSerialPort();
   binStatus = _sensorPort.ReadLine();
   CloseSerialPort();
}

Here is the case:

Arduino A sends 3 to the Arduino B. And checking via serial monitor in arduino program, it is correct. It reads the 3 sent by Arduino A. Now, the Visual Studio program gets the serial printed by the Arduino program via the accessing the COM7 where it will be placed in the binStatus string. the value that visual studio reads is the one that was declared which is the 1 it is like the arduino did not perform the void loop operation, it just printed the declaration.

We've been stuck for more than 24 hours and we cannot solve our problem. Serial Monitor prints it right but the visual studio program reads it wrong.

1
"the one that was declared which is the 1" is not a clear problem description. - Hans Passant
ok i got that. can you confirm what arduino is reading from serial port? - Ccr

1 Answers

0
votes

Remember, serial communication sends data in form of characters.

So that means, your logic if(rx_data==1) is wrong because you are expecting the value in integer form which is never going to be true.

try this

if(rx_data=='1')

I hope this helps.