Realbasic Serial Example

 admin  
Active6 years, 2 months ago

Xojo is a development tool for creating powerful, native applications for desktop, web, iOS, and Raspberry Pi. Xojo offers Xojo Cloud for easy, secure, maintenance-free web app hosting. Mac & Apple Devices - EveryMac.com's Ultimate Mac Lookup. Lookup Mac, iPod, iPhone, iPad, Apple Watch, Apple TV and other Apple devices by Apple Order Number, Model Number, EMC Number, Model Identifier, and Serial Number as well as Intel processor number to check its specs and other details.

Realbasic

I am writing a realbasic console application which polls several serial ports for data and saves the results to a database.

My initial idea was to open the port, read the data and then close it again, The problem is, that opening a serial port can take up to 4 seconds and I may need to read from up to 8 ports, so opening and closing the port is not practical for each cycle.

it's highly possible that the serial device disconnected from an opened port, and of course this is going to cause problems.

So is it possible to detect whether a port is open and alive so that I can leave the port open and only close and re-open it when I detect that the connected device has gone away.

The serial port is a bluetooth serial port, it is talking to a bluetooth radio which in turn talks to a microcontroller. I have start and end characters which I listen for and all of this works fine, until the bluetooth device goes out of range and effectively disconnects leaving the serial port still open.

I can of course close the port and try and open it again, but instead I would like to detect whether the device is still connected to the bluetooth serial port.

Also, calling serial.close() on a serial port on OSX causes a momentary freeze (desktop cursor freezes) , I suspect 100% cpu usage, this does not happen on windows and I therefore want to minimize the number of port opens and closes that I have to do as I am polling data from around 10 bluetooth devices once a minute.

As it takes up to 4 seconds to open a serial port, the best solution would be to use a direct HID connection to the bluetooth radio instead of a SPP serial connection, however it appears that nobody has ever connected realbasic to a bluetooth HID device before and so there's no info or help on it.

Paul Lefebvre
5,2423 gold badges23 silver badges35 bronze badges
crankshaft

Realbasic Tutorial

crankshaftRealbasic
1,1213 gold badges23 silver badges54 bronze badges

2 Answers

Serial

It's been a few years since I did Serial programming in RB, so I don't recall everything.

The Serial class has a 'LineStateChanged' event. Have you checked if that gets invoked once the BT conn is lost or reconnected?

If that doesn't work, you could try using the low level BSD/POSIX functions to open the port and use ioctl() calls to figure out its state. I don't have any examples for this, though. And I'm not even sure that this is the right way to do it. It probably comes down to learning what a C program would do, and translate that to RB.

About the bad performance: That's usually due to RB's limited control over its event management: It doesn't see that it needs to poll the serial port more often, and thus only checks on it rarely as long as it has no other reasons to ask for more idle time from the OS.The trick is to run a Timer with a high frequency (e.g. once every 10ms), and then invoke the serial port's Poll function from the Timer's Action event.

Thomas TempelmannThomas Tempelmann

Realbasic Software

6,3323 gold badges52 silver badges99 bronze badges

Your request is kind of odd because typically what you do is connect to the serial port and then wait to receive any data sent to that port. Let me put it another way. The serial port does not store any data waiting to disgorge for you when you connect. You're either listening or your not.

You have to connect to the port and listen for data. Once you receive DataAvailable events you can start processing data but be aware that not all data in the stream may have been received and processed before the event occurrs. You generally need to determine what the End of Message is. Sometimes this is a Carraige Return and sometimes not. Depends on the system.

More information on DataAvailable at http://docs.xojo.com/index.php/Serial.DataAvailable

Paul Lefebvre
5,2423 gold badges23 silver badges35 bronze badges
BKeeney SoftwareBKeeney Software

Not the answer you're looking for? Browse other questions tagged realbasicxojo or ask your own question.

   Coments are closed