rs232.serialPort Write Bytes?

Hello Everyone,

I have trying to find how I can send bytes with the rs232.serialPort.
For me it gives writeLine and Write strings.

Thanks,
Lucas

@ lssilva - rs232.serialPort.Write accepts the following params:

byte[] data
string data
byte[] data, int offset, int length

Thank you James I will have a go :slight_smile:

Other things that may help:

https://www.ghielectronics.com/docs/15/uart

https://www.ghielectronics.com/docs/275

Thank you Jeff,
The links are very usefull. I have tried some of the sample code. but whenever I call

rs232.serialPort.Read

The programs crash and stop.

I know the serial device is sending a response but I can’t read it.

Any experience?

Thanks,
Lucas

By the way the source code is:

 data)
        {
            byte[] rx_data = new byte[10];
            for (int i = 0; i < 1; i++) 
            {
                if (!rs232.serialPort.IsOpen)
                {
                    printText("Port is closed");
                }
                else
                {
                    String byteStr = ByteToHexBitFiddle(data);
                    printText(byteStr);
                    rs232.serialPort.Write(data);
                    Thread.Sleep(500);
                     
                    int read_count = rs232.serialPort.Read(rx_data, 0, rx_data.Length);
                    if (read_count == 0)
                    {
                        printText("0 to read ");
                    }
                    else
                    {
                        printText("something to read ");
                    }
       
                }
            }
        }



As soon as it reaches int read_count = rs232.serialPort.Read(rx_data, 0, rx_data.Length); the program freez. I have tried to replace it with a read byte only. My hardware is set with a loopback so theoretically I should receive everything I send.

I have a Cerbot with a Display_n18 and rs232

Thanks,
Lucas

Hi,
please format your code by click on the 101010 Icon in the line above the Emoticons.

{code=cs]
::
::
{/code]

Thanks, Formatted.
Can you see any hidden bugs there?

@ lssilva - Is the behavior any different if you subscribe to and attempt the read in the event:

Hi,
to stay in your code you could try:


if (rs232.serialPort.bytesToRead == 0}
{
printText("0 to read ");

}
Else
{
    int read_count = rs232.serialPort.Read(rx_data, 0, rx_data.Length);
    printText("something to read ");


}

you can have a look on this link to see how it was solved by others

https://www.ghielectronics.com/community/forum/topic?id=14927

Thanks for the replies.

@ RoSchmi
I tried the modified code and it did not work and I checked the code in the link and it seems to be based on DataReceived which doesn’t work for me.

@ James
I have tried both


 rs232.serialPort.DataReceived += new GT.Interfaces.Serial.DataReceivedEventHandler(serialPort_DataReceived);
            rs232.serialPort.LineReceived += new GT.Interfaces.Serial.LineReceivedEventHandler(serialPort_LineReceived);

But the methods are never called.

I managed to get Ethernet and all other modules working just the reading in RS232 left. I will check the wiring again.

Thanks

:-[ :’( :wall:
I had soldered in the wrong place :wall: Now it works fine ??? :-[

@ lssilva - We all make mistakes like this, don’t worry too much. Thank you, though, for keeping us updated!

We ALL have been there !!! So no worries :wink: