C# Forms to Arduino Serial Communication

I'm trying to get the entire serial packets from the Arduino. I've written several C# form interfaces to try and get the entire packet of data rather than one byte at a time. I have achieved this however the serial seems to lock up the form because I believe from what the debugger is telling me the serial readline is cross threading the dataIN1.

The Arduino can't talk through a DTU Ebyte so I placed an Atomic Pi with a touch screen which acts as a buffer between the Arduino controller and the Atomic Pi to the EByte Serial DTU. The Arduino is read by the C# program, does some calculations and then the C# program has another port open for the EByte and sends the serial data to a receiver about 1km away which also has an Atomic Pi running Windows 7. I've tried the LoRa modules however they seem to be querky and aren't that reliable. The DTU's i'm using from EByte I've transmitted up to 4km from the rooftop (42 floors) and the land here is flat (UAE).

DTU <> CPU <> MCU

So I want to place a Code block here in the Forum and I apologize if it's not formatted correctly. I'll do my best to ensure it's placed in the forum but not sure how to keep it in it's own code block. I use "**" in other forums with 3 indents. if this is wrong please correct me. Thank you.

 private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
serialPort1.DiscardInBuffer();
dataIN1 = serialPort1.ReadLine();
this.Invoke(new EventHandler(ShowData));
}
private void ShowData(object sender, EventArgs e)
{
string dataIN1 = serialPort1.ReadLine();
tbReceiveData1.Text = dataIN1;
if (dataIN1 == "Close\r")
{
this.Close();
}
if (dataIN1 == "Pin 11\r")
{
MessageBox.Show("Pin 11 Selected!");

}
}

Okay well I give up... Seems no matter what I do I can't get this code in a code block. I've forgotten how already... LOL!

This assumes the serial, baud, parity and stopbits are set in a label and the tbReceiveData1.Text is the receiver of the dataIN1 string. The Arduino Code I've simplified so I can just look at a few inputs from the Pins of the Arduino just to get the C# code to transfer the data to the other serialPort2.

int nbts = 3;
int startpin = 10;
int bts[3];
boolean btgs[3];
int incomingByte = 0;

//End Button Declarations
void setup() {
Serial.begin(9600);
for(int i=0;i<nbts; i++) bts *= i+startpin;*
_ for(int i=0;i<nbts; i++) btgs *= false;*_
 for(int i=0;i<nbts; i++) pinMode(bts*, INPUT_PULLUP);*
_* delay(500);*_
_* Serial.println("Ready for Input when you are!");*_
_*}*_
_*void loop() {*_
_* readBtn();*_
_*}*_
_*void readBtn(){*_
_* for(int i=0;i<nbts;i++){*_
_ if(digitalRead(bts*)==LOW){*_
_ if(!btgs*){*_
_ btgs *= true;*_
_ if(bts*== 12){*_ 
_* Serial.println("Pin 12");*_
_* delay(250);*_
_* }*_
_ else if(bts*== 11){*_
_ btgs *= false;*_
_* Serial.println("Pin 11");*_
_* delay(250);*_
_* }*_
_ else if(bts*== 10){*_ 
_* Serial.println("Close");*_
_ btgs *= false;*_
_* delay(250);*_
_* }*_
_* }*_
_* }*_
_* }*_
_*}*_
_*
*_

When I run the code of the Arduino, it works fine and sends the full packet to the PC. When the C# tbReceivedData1.Text fills with data, it will read the full incoming string and print it to the textbox. But the rest of the form freezes and I can't close it nor can I close the port. It seems to be stuck in a loop but the debugger says the string arg is cross threading. Does anyone have any ideas on what I can do to fix this issue. I've been on it for a couple of days. I have the code able to pick up one byte at a time but it only prints a partial packet and the rest of the packet is lost in the clear buffer arg. Thanks in advance for any advice.

neuralmorph:
I've tried the LoRa modules however they seem to be querky and aren't that reliable.

I suspect most of the rest of the World would disagree with you.

neuralmorph:
I'm trying to get the entire serial packets from the Arduino. I've written several C# form interfaces to try and get the entire packet of data rather than one byte at a time. I have achieved this however the serial seems to lock up the form because I believe from what the debugger is telling me the serial readline is cross threading the dataIN1.

This is very confusing.

In Arduino code Serial.readline() is for receiving data sent TO the Arduino whereas your first sentence mentions packets FROM the Arduino. Which is it?

For receiving data on an Arduino have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

...R