SERIAL RS232

Hello,
I am beginner with XOJO and i would like to communicate with RS232 / SERIAL COM PORT to send receive characters or hex. I tried XOJO Examples, but there are no examples of how to send and receive data using tx and Rx buffer. Does somebody has got performed it, and can explain or give example how to ?
Thanks a lots in advance,
ol

Sure, the basic premise is

You can drop a serial port control onto your window from the library. Make sure it’s properties are set to match the device you are connected to. Make sure your wiring is correct. At the minimum you need Serial in, serial out AND Ground. Serial in from one device should connect to serial out of the other. Google DB9 connections for info.

You need to get a list of com ports that are available on the machine and assign one of those com ports to the serial control. I use something like this to load them into a menu.

[code] ‘’’’ Get a list of available COM ports available

For i as Integer = 0 to System.SerialPortCount - 1
ScrnComPort.AddRow( System.SerialPort( i ).Name )
next i
[/code]

The port is then opened. Use serial.open.

You use serial.write to send data out to the serial port and com port.

Anything received comes in through the receive buffer. When data is received into the buffer the DataReceived event for the serial port is fired. Any code to read the data needs to be in this event, or called from this event. Use serial.read and serial.readall to do this. Be aware that it is not well documented how much dataor how much time between data sets is required to fire this event. Therefore you may need to do multiple reads to get what you expect.

Always do error checking.

This applies for Windows, I know nothing of that “other” brand.

Many thanks for your answer Joseph, I will try it today.
I already have checked cable and port open, then i will perform a test on receive event.
@

This is 100% incorrect. You can call Serial.Read or Serial.ReadAll from ANYWHERE. It does NOT have to be in the DataAvailable event. This is generally the most convenient place to have it because you know that when the data is there you generally need to do something with it.

But I’ve had cases where I need to execute code in a synchronous fashion where I write one thing wait and then parse the response and based on that, execute other code, etc. and the asynchronous nature of the DataAvailable even makes that difficult. So I’ve done stuff like:

Serial1.Write(SomeCommand)

Do
   Serial1.Poll
Loop Until Serial1.LookAhead.Instr(Some_Response_I_Want) <> 0

Dim ReadString as String = Serial1.ReadAll

Most of the time though, my reads ARE in the DataAvailable event.

The other thing you can do is call LookAhead to examine the contents of the buffer w/o emptying the buffer.

Yes, you are correct, but for a beginner it is probably not the best practice. The next question would be, I am doing a serial read and only getting partial data or not any data because he is not waiting adequate time for the data to arrive. I was merely explaining the purpose of the dataavailable event, but should have been more clear. I’ve never had the need to read the serial port if there is no data available.

This is an interesting approach. I do it differently, avoiding the do loop. I create a string (or what ever your working with). In the data available event I do a ReadAll and append it to the whatever is already existing in the string (perhaps data from the prior read). I then search the string for what I am looking for, parsing any message I need. If I find what I am looking for I do some action or execute some other code. I then modify the string to maintain any extra data or bytes I may need for the next read (beginning bytes of next message).

Doesn’t the do event lock up your interface? What if the device is not connected and never sends data?

[quote=139897:@Joseph Evert]Yes, you are correct, but for a beginner it is probably not the best practice. The next question would be, I am doing a serial read and only getting partial data or not any data because he is not waiting adequate time for the data to arrive. I was merely explaining the purpose of the dataavailable event, but should have been more clear. I’ve never had the need to read the serial port if there is no data available.
[/quote]

Correct. But it’s also good to advise new people accurately. I get what you are trying to do though.

[quote]
This is an interesting approach. I do it differently, avoiding the do loop. I create a string (or what ever your working with). In the data available event I do a ReadAll and append it to the whatever is already existing in the string (perhaps data from the prior read). I then search the string for what I am looking for, parsing any message I need. If I find what I am looking for I do some action or execute some other code. I then modify the string to maintain any extra data or bytes I may need for the next read (beginning bytes of next message).

Doesn’t the do event lock up your interface? What if the device is not connected and never sends data?[/quote]

No, it doesn’t. The call to the Poll method ensures this doesn’t happen. I do have a timeout that I look for as well. In the case of what I am doing, I know I am going to get data back.

But sometimes you have to get the data sequentially and work sequentially as that is what is being done to a physical device. I tried doing it running entirely event driven but it was driving me crazy trying to get everything into the right sequence of knowing when to respond, what to do next, etc. Lookahead works very well when trying to see if what you want is in the buffer.

It does block the UI, but you just probably haven’t noticed it because it has been fast enough response. Serial.Poll does not keep the UI responsive.

Well, true, but yes, it’s a short enough time. Sometimes in threads if I am accessing the serial port (or TCPport) I have slept the thread for a few milliseconds at a time while looping.

I’ve never had any issue with working sequential data with the DataAvailable event, as that is the nature of almost all bi-directional serial communications. Are you saying the data is getting scrambled in some way when it is read?

Based on your description what you are doing is not “synchronous” communication by definition. When we read and write to the serial buffers are we not reading and writing to a UART (Universal ASYNCHRONOUS Receive/Transmitter)? Xojo documentation says writing is done asynchronously… Are you using the term synchronous and sequentially interchangeably?

[quote=139938:@Joseph Evert]I’ve never had any issue with working sequential data with the DataAvailable event, as that is the nature of almost all bi-directional serial communications. Are you saying the data is getting scrambled in some way when it is read?
[/quote]

That’s not what I said. I said I need to do things in a specific order. Event driven programming is not necessarily sequential.

[quote]
Based on your description what you are doing is not “synchronous” communication by definition. When we read and write to the serial buffers are we not reading and writing to a UART (Universal ASYNCHRONOUS Receive/Transmitter)? Xojo documentation says writing is done asynchronously… Are you using the term synchronous and sequentially interchangeably?[/quote]

Of course it’s asyncrhronous. But I need it to function in a synchronous manner. I have certain commands to send and then based on the responses perform specific actions in order. When you sit down at a terminal and start typing a command, you are really working synchronously. You wait for the device to respond and then send the next thing, etc.

In a truly asynchronous environment, you could send all your writes while waiting for the responses to come back and then handle all those responses independently. I can’t do that as what I write at one point is dependent on what is received back from the device from the previous write. So it absolutely is synchronous.

But I really don’t need or want to defend what I am doing.

Nor do I, and my intention was not to offend you or require you to defend what you were doing, I was just trying to understand it.

You and I are doing the same exact thing as my communication protocol requires that the communication is in a sequence > The PC (or device) sends a command and waits for something specific back, based on what it receives does something else and responds, etc. etc. - the devices talk to each other. One device is not just blasting stuff out in a stream.

All I am saying is that I am doing this in the same way using the built in events and having things that are required to be synchronized in order does not preclude someone from using the DataAvailable event. It works perfectly.

I would love to be able to see how you do this then because I’ve not figured out a good way to do it that meets my needs. Would you be willing to share some code with me? Send me a private message if you do.

I’m always willing to find a better way…