RS232 communication

Hi everyone, I’m a young developer and today I started working with Xojo. I’m trying to create a simple RS232 communication program with my hardware. Right now, seeing the video on YouTube, I can read what comes to me on the serial. But now, I would also like to forward messages in hex to my device. How can I do?

instead of using serial.read, you use serial.write …
code snippets in the language reference, and full source code in the examples/communication/serial folder of Xojo.

Hi @Jean-Yves Pochez , I see this in Xojo documents, but being new in Xojo I need more details. I would like you to see my interface so as to tell you what I would like to do. Can I send you a message in private?

@Jean-Yves Pochez , How Can I send you a photo of my example?

@Jean-Yves Pochez I was able to send a command to my hardware with the string you posted to me. My last two questions:

  1. How can I send messages in HEX?
  2. When I want to read the data on my device, I don’t see everything, but a part, why?

using the decodehex method, sending the resulting string to the serial device.

Dim s As String s = DecodeHex("61") //returns "a"

post your photo to some online service like imgur.com
then copy the bblink url imgur gives you and paste it here in your reply

depends on many many things…

Thanks @Jean-Yves Pochez, this is my part of program:
https://i.imgur.com/e3S588g.png

In those little squares I’m going to write values in hex to send them to my device. How can I do?

You have to add the filename extension to make your image show:

Anyway, are you sure you want to send hex data? In many cases where an API documentation writes hex data it simply means to send binary values for which the ChrB command exists. For a hex value of &h11 that would simply mean to send a ChrB(17) (or ChrB(&h11)) to the serial.

In most cases this is because you are reading data while the device is still replying. Most devices use a separator, often EndOfline, or some other Byte combination to depict the end of a message. So what you should be doing in the DataAvailable event is to use LookAhead to see if the separator exists in the serial’s data buffer, read only that much, process this message and return to see if another separator exists. Else exit the event handler and leave the buffer untouched.

Hi @Ulrich Bogun , thanks for your answer. Regarding point 1, I have to be able to send to my hardware values from 01 to 3E in hexadecimal. For the second point, however, it is not very clear to me. Could you give me an example? As I said, I am new and have no experience in the field

Let’s say you have a method ProcessCommand in your app. Then this could be the content of your Serial DataAvailable event when the device uses Chr(13)+Chr(10) as a separator:

Dim pos As Integer Do pos = Me.LookAhead(Encodings.ASCII).InStr(EndOfLine.Windows) If pos > 0 Then Dim command As String = Me.Read(pos+1, Encodings.ASCII).Trim // Pos +1 to get chr(13)+chr(10), modify if just 1 separator byte. ProcessCommand(command) End If Loop Until Pos = 0

This way, if the serial did not finish receiving one device reply, it will ignore the content and return the next time the event fires. Otherwise it will process every reply that came in in between.

If I used the code that you attacched here, I have an error with ProcessCommand(item not exist). Why?

Because that method does not exist in your app. It was just a placeholder. You should use your own method that processes the device reply here.
(You can of course do the processing just at that place, just a personal preference for short and clean code.)

To do something simple and short? I didn’t understand what I have to call the method.

[code]//if InStr(me.LookAhead(Encodings.ASCII), chr(13)+chr(10)) > 0 then
//TextArea1.text = TextArea1.Text + Me.Readall(Encodings.ASCII)
//end if

Dim pos As Integer
Do
pos = Me.LookAhead(Encodings.ASCII).InStr(EndOfLine.Windows)
If pos > 0 Then
TextArea1.text = Me.Read(pos+1, Encodings.ASCII).Trim
//Dim command As String = Me.Read(pos+1, Encodings.ASCII).Trim
//processCommand(command)
End If
Loop Until Pos = 0[/code]

This is what I used and the new one