Main Site Documentation

rs232.serialPort Write Bytes?


#1

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


#2

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

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


#3

Thank you James I will have a go :slight_smile:


#4

Other things that may help:

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

http://msdn.microsoft.com/en-us/library/dd170258.aspx

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


#5

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


#6

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


#7

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

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


#8

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


#9

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


#10

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


#11

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


#12

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


#13

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


#14

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