I'm trying to communicate on a com port on a single wire UART(Half Duplex). It's a C# Application with 8 bit MCU.
Here, Application is the master and MCU is a slave. On the MCU the RX is disabled while TX is sending.
The requirement is to make the com port disable the read function while I send data from the Application.
To do this, I tried to pull out from the buffer of RX after it transmits the number of bytes it transmits, but the MCU responds too fast and the last bytes of TX get in between with the response of the MCU.
Note: I can't change anything in the MCU
Here is my CODE:
public int serialRX()
{
int b;
if (serialPort.IsOpen && (serialPort.BytesToRead > 0))
{
try
{
b = serialPort.ReadByte();
}
catch (TimeoutException)
{
b = -1;
}
return b;
}
else
{
return -1;
}
}
public void serialTX(byte[] data)
{
if (serialPort.IsOpen)
{
serialPort.Write(data, 0, data.Length);
byte i = 0;
int read = -1;
while (i < data.Length)
{
while (read == -1)
{
read = serialRX();
}
read = -1;
i++;
}
}
}