Comparing serial send to serial receive

Hello all,
I am REALLY new to XOJO, but I have been programming microcontollers in C for a few years so I do recognise some of the language.

Heres my project/app. I want to send the numbers 0 to 9 inclusive in ASCII to the serial port 100 times for a total payload of 1000 bytes plus a carriage return. I have a device at the end of the connection that will save those 1000 bytes and send them back to the project/app. I want to capture those bytes(all1000) and sense the carriage return, and compare them to the transmitted bytes and let me know if there is no match.

Basically a table comparison of sorts.

For example, if I send 1 2 3 4 5 6 7 8 9 0 and the remote sends back 1 2 4 5 7 8 9 0
Teh app should count the number of received bytes and issue a FAIL indicator, and the number of bytes received. I would also like to see the received bytes in a text area.

For you XOJO pros this is probably a piece of cake, but for this noob, not so much

Jim

Hi Jim,

all the info I am talking about here, and probably all the information you will find anywhere on this forum or the internet will only work with Xojo 19r1.1 or OLDER.

please excuse me if you have already tried some of the notes below, but other new users may also find the information useful.

in order to do any of this you need to first do many other things:-

search the computer for ports
select a port
configure baud, stop bits etc
open it and check if the system allowed that port to open correctly
if that is ok then you will need a routine to send the data, a routine to receive the data and a routine to parse the data and display it on screen.

this may sound a bit daunting at first sight, but in reality its a group of separate tiny project that all can be tested individually until they are all doing what you want and then they will work together.

please do not fall into the trap of trying to make the whole project work from the beginning, any part of it not working will make you think something is bad that is actually ok.

I use serial ports in many many projects for many years and they are all based on a code base which is actually quite simple, they are all based on the EXAMPLE project supplied with Xojo:-

start a NEW PROJECT, go to the EXAMPLES folder, open COMMUNICATIONS, open SERIAL, load ‘Serial Port Bar Code Reader’

this is obviously not going to work with your project directly, but it has all the requirements I stated above which will allow you to start the project with working parts you can then modify for your needs, there are many ways to do what you require so use this as a real start point to get a bit of experience with the IDE and debugger.

if I were doing the same I think I would fist try to see how the serial port searches the computer for the serial ports and displays them to you on screen, then try to connect to it.
that functionality will be the same in any serial application, so once mastered you have a good step forward to sending data.

final bit of advice at this stage, do not try to connect to your hardware project until you know if the send and receive functions you create can actually send and receive data, I would recommend using a loop back connection on a serial port cable by connecting pins 3 and 2 together with a bit of wire.
then you can use a terminal program, I use CoolTerm, http://freeware.the-meiers.org , which was written in Xojo and is superb, to read the data you send and send data from it to you code to be sure you are actually getting valid results, basically using the terminal to simulate your real hardware until you have validated code in your project.

serial data can be most difficult to debug if you are expecting a result to happen when actually the data is not what you expect as I am sure you have seen in embedded stuff.

the serial data you see in the terminal may not actually be what you expect as the system will present you with the ASCII representation of the data, and a can of worms might then be found! do all test using ASCII (decimal 32 to 127 characters) before thinking about HEX or you might go mad.

EDIT, I forgot to say that the loop back would need 2 serial port adaptors, on for the Xojo project and 1 for the terminal, pin 2 and 3 are the RX TX data pins so when sending from one part of the project it will need the pins one way and sending the other way they would need swapping about, also connect pin 5 for ground!

Mark

Thanks Mark, but I realised that I neglected to mention that I have already done the tutorials so I am familiar with using the serial port class and such.

What I am looking for is how to send the 1000 bytes plus >CR< and receive back whatever comes in to the RX pin into a buffer then compare the RX buffer to the Transmit.

In this case, since I am only repeating 0 - 9 inclusive I do not need to have two buffers.

In a microcontroller and C this is a breeze with a couple of FOR loops. The RX side is also pretty straightforward with allocating 1001 bytes in SRAM to hold the incoming serial stream.

Then one final FOR loop to compare the RX buffer to the sequence.

And as I put in my OP, I am using ASCII. YES I will simply short 2/3 together for the preliminary testing.

So I need to figure out with a little help how to do teh send routine, and how to allocate a ‘buffer’ of sorts for the RX data

Thanks!
JIm

in Xojo a string can hold just about any binary data so you dont really need to “allocate” buffer like you would in C

are you transmitting the bytes with ASCII code 0 - 9 or the bytes that are the digits 0-9 (ascii 48-57) ?

I know the differences with embedded (I use PIC and interrupts for all my RX stuff and its extremely reliable), but there are significant differences in the PC and how it handles the data.

the main difference is the use of an EVENT based system in Xojo.
there are no guarantees at any point that what you send from the embedded device actually gets presented to you in the DataAvailable event of the serial port in one lump as you may hope.

in fact its just as likely you get 1 byte or all of it, it depends on everything else going on in the computer.

basically you can not depend on the receive event as an arbiter of packets sent, its not designed to be that beast.

there are several ways to tackle it, one might be (to make it more reliable) is to send much smaller packets which each have some form of end character and then the last one has a different character to indicate the end.

sending 1000 characters and expecting them all to end up completely correct in the buffer is a bit optimistic if you ask me, without some message management (you are doing something similar to my current project which is a PIC and Pi).

as I mentioned in my initial post, trying to do your complete project immediately is likely to end in some frustration.
a smaller test with say 20 bytes is likely to be more useful as initial testing.

also you do not mention baud rate, in my experience any testing above 19200 is likely to introduce unexpected issues, make sure the code is good in the computer before going higher, not all USB serial adaptors work as well as you might hope and longer than a couple of meters cable is asking for trouble with single ended hardware.

next option is what to do in the DataAvailable event, for testing purposes I would do this in the event, but in reality the code in any event should be as small as possible and call a method to do any processing.

the main options are ReadAll and LookAhead.

ReadAll will empty the current contents of the Serial buffer into your own string property, MySerialRXData as string, for example:-
in the DataAvailable event:-

MySerialRXData=me.ReadAll

you do not need to tell the string how big it needs to be, it will grow up to ram limit I think.

when you have the data in MySerialRXData you will need to see if the last character is as you put it, you will need to specify what is by using Chr(10) or Chr(13) depending what you send for >CR<:-

this would be done using the string functions Right or Mid to look at the last character in the buffer and compare it with Chr(10) etc.

you can then show the results in a textfield:-

mytextfield.text= MySerialRXData

the second option, and the one I might prefer in the scenario described in this test, is to use LookAhead.

LookAhead will read the serial buffer data and allow it to be tested but does not empty the contents from the receive buffer.

this is similar code I use in my own routines with changes to the names:-

If right(me.LookAhead,1)=Chr(10) then
//do something with the data received.
MySerialRXData=me.ReadAll
mytextfield.text= MySerialRXData
end

all of this is just a version of the barcode example.

@Norman Palardy

0 - 9 ascii is 30 - 39.

Ahhh Good to know!

@Mike Carlton
I will re-read your reply and ponder this. Data is going to be at up to 115.2k. I suppose I could do all the heavy lifting in an AVR and then use a seconds serial port on the AVR to talk to XOJO, but then I could simply use a terminal at that point, wasting the $99 I spent on a license.

[quote=457005:@Jim Marquardt]@Norman Palardy

0 - 9 ascii is 30 - 39.
[/quote]
in hex - I gave decimal :stuck_out_tongue:

the serial port speed is not an issue in Xojo, the issues are external, such as the USB adaptor, length of cable, speed of the computer running the code and largely external electrical noise, is it absolutely necessary to run at 115200?

you could have done all the development without buying the licence, it will run in the IDE exactly the same you just could not build the application.
but even so, there are a million other apps you can make now! so never a waste of money.

At the end of the day, almost all the projects I have designed and developed in the last 20 years are serial based using embedded PIC devices with a Mac or win based computer at the end to process the data, I never used them at anything above 57600 and when it needs reliability it uses an RS485 transport layer to eliminate all the noise issues, the last few years its been exclusively had Xojo at the computer end and its proved to be perfectly able to do any project.

so that is all I can offer to answer your original question, its not a piece of cake, just another problem with its own set of rules which are completely different to the embedded world, both of these worlds are as familiar to me as each other, and if you were to ask originally if its possible to reliably send 1000 bytes of data, without a message protocol, at 115200 baud over a cable with RS232, to a PC using Xojo then the answer would be 'its unlikely to be reliable at that speed due to limitations in the RS232 hardware with ANY compiler for the PC"