Using serial data

i got an cobra 2 with RS232. serial
i got it connected with this code :

// This is the function that will be called when data is available
//  on the serial port.
// This function will not be called again if the data isn't read in.
void serialPort_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
    // Create a buffer that is the length of the data that is available
    // the BytesToRead variable tells us how many bytes are waiting in a buffer.
    byte[] readData = new byte[rs232.serialPort.BytesToRead];
    // Now we read the data into the buffer we just created, starting at the 
    //  first byte.
    rs232.serialPort.Read(readData, 0, readData.Length);
    // Use the data that you just read however you need to

now it says :
// Use the data that you just read however you need to
im quite nooby and i cant figure out how to write these bytes to an string. i want to print the string on the char display i got.
can anyone help me out?

this is what i made :

    string printdata = Convert.ToBase64String(readData);

but i get nothing but blanks.
or im sending my data worng. im trying to send data using my PC COMPORT1.
using “hyperserialport.”


Hi Rayvh, welcome to the forum !

Step 1 is always to inspect your data as it comes in - use breakpoints and inspect the readData array’s contents. That will really tell you whether you have the data or not ! For simple stuff you can always iterate through that loop and print the individual characters too.

 rs232.serialPort.Read(readData, 0, readData.Length);
Debug.Print(new string(Encoding.UTF8.GetChars(readData)));

And as Brett mentioned - Welcome.

how exactly do i do this ? :-[

and i tryed this :

;  rs232.serialPort.Read(readData, 0, readData.Length);
            char_Display.PrintString(new string(Encoding.UTF8.GetChars(readData)));
            Debug.Print(new string(Encoding.UTF8.GetChars(readData))); 

but it says encoding doesnt exist in the current content

edit: found it.

using System.Text;


i recive

on everything it gets

breakpoints are F9 in Visual Studio. Select the line you want to stop on, hit F9, then hit F5 to deploy and run. When your app hits that code, VS allows you to then step through or into further code (F10/F11) as well as look at local variable values etc by hovering over the object with your mouse.

i get this null as result… (see image)
i try to send using my computers COM1 port on defauld baud rate ect ect.
im sending “123456” or “test”

It seems that you’re are not receiving anything.

Just print BytesToRead to see if there’s data on com port.

how :confused:
i cant seem to get it working.


can you explain ?

did you try to send “12345\r\n”

yes, makes everywhere

do you use a null-modem cable (crossed Signal lines)?

not that i know… im using the PC com1 out. to an extension cable to the Rs232.

you should find out whether your cable crosses the pin from TX and RX. If you don’t use such a cable it cannot work

Can you confirm what mainboard you are using and what connected modules you have? Are you using an RS232 module? Are you also using a USB-to-RS232 cable? Either way, I’d suggest you buy yourself a few CP2102 TTL modules like

As @ RoSchmi says, using an RS232 cable means you will need to know whether it’s a crossover or not. With a CP2102, you get to control that by swapping cables around easily.

Also, it’s a REALLY BAD idea to try to write input you get from a serial port directly to your LCD, because you will get data in dribs and drabs, not in nice consecutive strings. So I would try a much simpler test regime first, and later figure out how you’re going to make the data into strings yourself and write that to the screen. So here’s some code snippets.

In ProgramStarted:

            MySerial.Initialize(19200, GT.Interfaces.Serial.SerialParity.None, GT.Interfaces.Serial.SerialStopBits.One, 8, GT.Interfaces.Serial.HardwareFlowControl.NotRequired);
            MySerial.serialPort.DataReceived += serialPort_DataReceived;

            Debug.Print(" MySerial.serialPort = " + MySerial.serialPort.ToString());
            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");

(I’ve named my RS232 module as “MySerial” )

Then your datarecieved handler:

        void serialPort_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
            int B2R = sender.BytesToRead;
            int myChar;
            Debug.Print("Data to read: "+ B2R);
            for (int i = 0; i < B2R; i++)
                myChar = sender.ReadByte();
                Debug.Print("Next: " + myChar);

Note: all untested, but I think it should work (I’ll test it later tonight, 10+ hours away)

From this, put a breakpoint on the MyChar = sender.ReadByte() line and watch the value of the data you read in. Write down their hex values if you need.

Then, make sure you’re connected to the CORRECT COM port on the PC side with your terminal program. Check device manager to confirm. Open the terminal program, make sure you select the correct port and set it up to match the speed and send parameters in the Initialize method above, and then connect. Send it lots of data (not just a few, to make sure it doesn’t get buffered) and if everything works, the breakpoint should be hit. Step through the code at that point and tell us the results.

im reciving stuff if i use your code on boud rate of 38400.
seems the cable is okay. now i can start translating the data into something more usefull
im getting al kinds of numbers, how do i translate these into usefull text?

edit: ive translated it into ascii charachters. thanks!

i kinda wanted to recreate the program in visual basic. but i cant figure out how to
"call/start" the serialPort_DataReceived method.

i wanted to make it in visual basic because its easyer to edit.
if i send : "Hallo"
the code gets :

instead of “Hallo” as in 1 part.

this is my C# code so far.

 void serialPort_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
            int B2R = sender.BytesToRead; 
            int myChar;
            var printstring = "";
            //Debug.Print("Data to read: "+ B2R);        
            for (int i = 0; i < B2R; i++)   
                myChar = sender.ReadByte();
                char converted = (char)myChar;
              //  Debug.Print("Next: " + converted);
                printstring = printstring + converted;

Over Serial each Byte is sent on it’s own. It’s not loke complete telegrams like in TCP.
So you need your own flow control.
When using ASCII commands you could send a at the end of each message or may be a .
Then on the receiver side you read byte by byte and append it to a StringBuilder until you get the ‘End-Byte’.
If you use a binary format instead of ASCII I would recommend to send the telegram (message) length as the 1st byte. The receiver reads 1 byte to get the length, and then reads the remaining number of bytes.

to split the incoming data after each CRLF sequence, I used a nice class SerialBuffer.cs which once was posted from andrejk on Codeshare. You can get the class e.g. from my last Codeshare project:
the class is used:

SerialBuffer mySerialBuffer = new SerialBuffer(256);
void serialPort_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
            byte[] myBuffer = new byte[rs232.serialPort.BytesToRead];
            rs232.serialPort.Read(myBuffer, 0, myBuffer.Length);
                    mySerialBuffer.LoadSerial(myBuffer, 0, myBuffer.Length);
                   string dataLine = ;
    while ((dataLine = mySerialBuffer.ReadLine()) != null)

yes, it’s very important that you never assume that your data will come in all at once. The event handler doesn’t wait for anything (and don’t make it !) you should just get your data from the serial port, buffer it internally, and let the handler exit. You then manipulate the returned data and print what you need when you need.

i cant figure it out…

i putted the code where i had my old " serialPort_DataReceived" ::slight_smile:

edit1: figured out the namespace thing
edit2: nothing to print?