[ SOLVED ] C# win10 serial terminal MISSING FIRST BYTE !

so, after 3 days of hard programming, reading and trying all sorts of software and serial port codes, I finally found two lines of code on the internet and after copying it into the serial terminal connection function, everything fell into place and the strings as and other communication and data went smoothly.

at first I couldn't even understand what this code meant for the correct operation of communication through the serial port to the terminal, but later it turned out that there was also a lot of sense in these two lines of code that started the whole process as it should.

first let me clarify what these two lines are and what they do in the code of the hardware serial port connection function of the computer which is wired to the hardware serial port of the raspberry pico or arduino or other processor systems.

these two lines of code allow the hardware serial port to receive and transmit information. these are DTR / Data Terminal Ready/
/link here >> https://en.wikipedia.org/wiki/Data_Terminal_Ready/
RTS /Request To Send/
/link is here >>> Difference between RTS/CTS and DTR/DSR flow control - GeeksforGeeks

IBM link >>> https://www.ibm.com/docs/it/aix/7.2?topic=communication-flow-control

so, serial ports are as communication interface as there are computers on this planet. the older and more experienced know that all sorts of machines and systems have been controlled through serial ports over the years of the computer industry. there is an awful lot of information about this on the Internet, you can read until retirement.

these two commands given in the connect function enable the hardware serial port to receive data to the program that connects to it to receive the data. the terminal program's connect button sets both hardware serial port flags to one so that when you press the connect button, the hardware serial port will tell the opposite serial port that it is ready to accept data from it.

exactly these two flags in C# visual studio are disabled by default and you have to manually put them as code on the corresponding serial port to say that you are ready to receive information and transmit as well.

because the raspberry pico is connected through the usb port which is a direct serial port to the RP2040 processor when these flags are not set there is no communication !!!!!

for that, in the code of the connect button function, you put these two lines BEFORE THE CONNECT FUNCTION !!! so that the flags can be set to unity and when the hardware serial port /computer USB cable/ is connected to the raspberry pico USB port, the flags will be raised and the receive and transmit will go as it should.

there is no change in the event function /interruption when receiving data/ through the serial port. these two lines are placed in the CONNECT button function !!!

here's some code for an example:

private void button2_Click(object sender, EventArgs e)
         { // CONNECT SERIAL PORT
             serialPort1.PortName = comboBox1.SelectedItem.ToString();
             try
             {
                 if (!(serialPort1.IsOpen))
                 {
                     serialPort1.RtsEnable = true; // IMPORTANT RECEIVE FROM USB ARDUINO/PICO !!!!
                     serialPort1.DtrEnable = true; // IMPORTANT RECEIVE FROM USB ARDUINO/PICO !!!!
                    // paste these two lines before serialPort1.Open(); // !!!! IMPORTANT
serialPort1.Open();
                     richTextBox1.AppendText(serialPort1.PortName.ToString() + " is CONNECTED" + "\n");
                 }
                 button2.Text = "CONNECTED";
                 button2.BackColor = Color.LightGreen;
                 button1.Enabled = false;
                 button2.Enabled = false; button3.Enabled = true;
                 button14.Enabled = true; button15.Enabled = true;
                 button16.Enabled = true;
                 comboBox2.Enabled = true;

                 // IMPORTANT BUTTON STATUS AUTO CLICK AFTER SELECT BOARD NUMBER
                // button15.PerformClick();

             }
             catch (Exception ex)
             {
                 //MessageBox.Show("Error opening/writing to serial port::" + ex.Message, "Error!");
                 richTextBox1.AppendText("\nserial port already open >> " + ex.Message + "\n\n");
                 button3.Enabled = false;
             }
         }

as I showed in the previous pictures, there was no communication on KOM3, unlike other com ports and other programs, now everything is OK !!! :slight_smile: after 3 days of struggle and sleepless nights, everything is now OK

in the C# documentation these two lines of code on the Microsoft Help site by default these two flags are disabled. why that is i have no idea.

here are links:

true to enable Data Terminal Ready (DTR); otherwise, false. The default is false.

true to enable Request to Transmit (RTS); otherwise, false. The default is false.

once again the code you need to put in order to be OK with serial communication terminal programs in C# programming for Windows 10:

serialPort1.RtsEnable = true; // <<<<< IMPORTANT RECEIVE FROM USB ARDUINO/PICO !!!!
serialPort1.DtrEnable = true; // <<<<<< IMPORTANT RECEIVE FROM USB ARDUINO/PICO !!!!
 // paste these two lines before serialPort1.Open(); // !!!! IMPORTANT
serialPort1.Open();
 richTextBox1.AppendText(serialPort1.PortName.ToString() + " is CONNECTED" + "\n");

and everything works perfectly. :slight_smile: thanks to the colleague for the code he gave me in his comment.

it turns out there's a lot to know about serial ports before you even get started. one can always learn something as long as one wants to :slight_smile:

if i have some new data about serial communication will let all you know. here is a picture from ok read/write terminal on COM3

1 Like