FreeBasic and Arduino communication

For issues with communication ports, protocols, etc.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Post by BasicCoder2 »

Ideally I need a complete example not snippets of code that I can't complete.

Essentially a FreeBasic version of the Python program shown here,

http://www.seeedstudio.com/recipe/166-b ... r-usb.html

.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FreeBasic and Arduino communication

Post by MrSwiss »

@BasicCoder2,

See the latest (reworked) version of FBterm: FBterm (Terminal)
In the source you'll find all the necessary routines ...
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Post by BasicCoder2 »

speedfixer wrote:A couple of years ago - I used this as my learning base for talking to an Arduino.
Thank you I will see what I can make of your example.

At the moment I have an Arduino brain and a pc+k8055 brain. An option I am looking at is to communicate to the Arduino via the K8055. When the parallel port disappeared from the modern computer I resorted to using photo diodes and the computer screen to output data and the buttons on the usb joystick to input data for at least they were supported by FreeBASIC.

.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Post by BasicCoder2 »

MrSwiss wrote:
In the source you'll find all the necessary routines ...
Have just been looking over your FBTerm example using this simple Arduino example:

Code: Select all

void setup(){
  Serial.begin(9600);
}

void loop(){
 Serial.println("Hello world");
delay (1000); }
And it works as expected.

Do you (or anyone else?) have an Arduino code example that will receive a number so I can test the sendnum() routine?
.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FreeBasic and Arduino communication

Post by MrSwiss »

@BasicCoder2,

Arduino Code:

Code: Select all

int inByte = 0;    // incoming serial byte

void setup() {
  // setup code 19200 Baud!
  Serial.begin(19200);
}

void loop() {
  // connection???
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
    // MrSwiss extension: switch (in C) = select case (in FreeBASIC)
    // Action depends on received Byte (Number/?)
    switch (inByte) {
	  case 0:
        Serial.println("Got HEX: 0");
        break;
      case 1:
        Serial.println("Got HEX: 1");
        break;
      case 2:
        Serial.println("Got HEX: 2");
        break;
      case 3:
        Serial.println("Got HEX: 3");
        break;
      case 4:
        Serial.println("Got HEX: 4");
        break;
      case 5:
        Serial.println("Got HEX: 5");
        break;
      case 6:
        Serial.println("Got HEX: 6");
        break;
      case 7:
        Serial.println("Got HEX: 7");
        break;
      case 8:
        Serial.println("Got HEX: 8");
        break;
      case 9:
        Serial.println("Got HEX: 9");
        break;
      case 10:
        Serial.println("Got HEX: A");
        break;
      case 11:
        Serial.println("Got HEX: B");
        break;
      case 12:
        Serial.println("Got HEX: C");
        break;
      case 13:
        Serial.println("Got HEX: D");
        break;
      case 14:
        Serial.println("Got HEX: E");
        break;
      case 15:
        Serial.println("Got HEX: F");
        break;
      case 16:
        Serial.println("Got HEX: 10");
        break;
	    case 48:
        Serial.println("Got ASCII 0");
        break;
      case 49:
        Serial.println("Got ASCII 1");
        break;
      case 50:
        Serial.println("Got ASCII 2");
        break;
      case 51:
        Serial.println("Got ASCII 3");
        break;
      case 52:
        Serial.println("Got ASCII 4");
        break;
      case 53:
        Serial.println("Got ASCII 5");
        break;
      case 54:
        Serial.println("Got ASCII 6");
        break;
      case 55:
        Serial.println("Got ASCII 7");
        break;
      case 56:
        Serial.println("Got ASCII 8");
        break;
      case 57:
        Serial.println("Got ASCII 9");
        break;
      default:
        Serial.print("ERROR: unknown Command! ");
        Serial.println(inByte);
        break;
    }
  inByte = 0;  // clear inByte
  delay(200); // pause a while
  }
}
Short Explanation:
a) Terminal:
  • whatever you type gets sent straight away to Arduino
    Byte wise, the ASCII-Code of Letter/Number etc.
    NO CR/LF etc. needed
    "0" to "9" ASCII implemented here (48 to 57 DEC)
b) Send Number:
  • requires an [Enter] after the Number
    these are the Decimal equivalents of HEX (0 to 255 = 00 to FF)
    0 to 10 (HEX) implemented here (0 to 16 DEC)
All else returns Error ...
Last edited by MrSwiss on Feb 23, 2016 15:40, edited 1 time in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FreeBasic and Arduino communication

Post by MrSwiss »

@BasicCoder2,

The Differences between the K8055 and the Arduino are in short:
  • K8055: is a "stupid" Device, e.g. it does exactly, what you tell it to do.
    (From the Program running on your PC, Arduino, or other controlling Device.)
    Arduino: is an "intelligent" Device, e.g. it does "it's own thing" (whatever YOU program it to do!).
    (It can run autonomously of a controlling System/Device, e.g. Data Logging etc.)
So, in Order to "get" anything out of your Arduino, you'll have to learn first, how to program it!

This was the Reason that I wrote in an earlier Post, to concentrate on Arduino first and only later on,
with the FB Side of Things.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: FreeBasic and Arduino communication

Post by D.J.Peters »

It's right the arduino IDE used the serial port (com/tty/usb) for uploading and reseting the arduino device.
That means the integrated serial monitor (if opened before) will lose the connection to the serial port and must reinit the port after an upload/device reset.
May be i'm wrong but you can take a look at the serial monitor of the arduino IDE it's open source.

Joshy
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: FreeBasic and Arduino communication

Post by D.J.Peters »

I found it here: SerialMonitor.java

The serail monitor open and close the port normal but it add an event listner "onSerialRateChange".

So if the IDE closed and reopens the port for it's own usage (upload/reset)
after that it needs only to trigger an onSerialRateChange event and a serial monitor (if any running) will reopen the port automatically.

NOTE: a sleep is important between a close and open
close #hFile
sleep 500
hFile = FreeFile()
open myPortInitString for ... as #hFile

Joshy

Code: Select all

onSerialRateChange(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        String wholeString = (String) serialRates.getSelectedItem();
        String rateString = wholeString.substring(0, wholeString.indexOf(' '));
        serialRate = Integer.parseInt(rateString);
        PreferencesData.set("serial.debug_rate", rateString);
        try {
          close();
          Thread.sleep(100); // Wait for serial port to properly close
          open();
        } catch (InterruptedException e) {
          // noop
        } catch (Exception e) {
          System.err.println(e);
        }
      }
    });
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Post by BasicCoder2 »

MrSwiss wrote:So, in Order to "get" anything out of your Arduino, you'll have to learn first, how to program it!.
Which is made easier and faster with simple examples.

I can program in C but am not up to speed with the serial port exchange protocols used by FreeBASIC or C.

I started programming a long time ago as a self taught hobby. I have no formal training in computer science which is the reason I am using FreeBASIC. This is why some "explanations" are too terse for me to follow as they assume knowledge I don't have. I do have a good grasp of low level programming. Get rid of the OS and direct access to the hardware and I would have no trouble writing code in Assembler to exchange data between two computers as I have done that before. I could make up my own protocols which other people would then have to learn if they wanted to use my code. Instead of that I have to learn to use their code which has nothing to do with actually understanding of the fundamentals of programming.

Yes I understand the difference between the K8055 and the Arduino. I can program stand alone programs for the Arduino and add my own circuits to its i/o pins. I am just not sure how to connect to a FreeBASIC program yet.
.
Last edited by BasicCoder2 on Feb 23, 2016 19:41, edited 1 time in total.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Post by BasicCoder2 »

D.J.Peters wrote:I found it here.
Unfortunately I don't understand Java (or Python) sufficiently enough to translate it to FreeBASIC version.

Perhaps it was a mistake to ever convert to FreeBASIC. Instead I should have spend the time learning a better supported language like Java, c# or maybe now Python would be the best language for a hobbyist. I only used FreeBASIC because I could leverage off my knowledge of QBASIC. However I made my choice and do not have the time or as I age the memory to learn another language.
.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FreeBasic and Arduino communication

Post by MrSwiss »

BasicCoder2 wrote:I do have a good grasp of low level programming.
Nobody is stopping you, if you prefer to program your Arduino in Assembly ...
BasicCoder2 wrote:I could make up my own protocols which other people would then have to learn if they wanted to use my code.
This is exactly what you have to do on the Arduino: define a Communication Protocol,
which you are later on using, to interact with it from your FB-Program.

To ask for Arduino Code on this Forum is the wrong approach, ask for this in Arduino-Forums.
Here we are after all, FreeBASIC programmers first ...
Arduino/Forum
There you'll find plenty Examples too. A lot of them are far more complex than my simple Stuff.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: FreeBasic and Arduino communication

Post by caseih »

I'm sorry you're so frustrated. FreeBasic is just fine as a language, so don't feel bad about focusing on it. If you know FB and C, then you can make FB talk to Arduino and vice-versa. At a high level, the serial port is no different than the normal text-mode console you work with on FB. You print to it and you can input from it. If you makes sure on the other end to always put in line-feeds, then FB's input routines can work with lines of input just like your local console.

It might help if you spelled out precisely what you want to do. A serial communication protocol can be as complicated or as simple as necessary. Some serial protocols are so simple you can open up a terminal and talk to them with a keyboard. For one project my needs were somewhat complicated, so I used an existing protocol for transferring units of memory back and forth. But for most projects, a simple line-based (CRLF) protocol is sufficient. For example a GPS unit may talk with serial to another device and spit out NMEA lines of text, so many per second. Nothing special is needed to read this text from the serial port.

You say you need a complete example. What kind of example would you like? I could probably crank out a simple arduino sketch and a corresponding FB program that talks with it.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Post by BasicCoder2 »

MrSwiss wrote:Nobody is stopping you, if you prefer to program your Arduino in Assembly ...
Spare time to spend on my interest is stopping me. There is a balance to be found between doing something yourself or learning how to use someone else's solution. I converted to C back in my MSDOS days for practical reasons.
... what you have to do on the Arduino: define a Communication Protocol,
which you are later on using, to interact with it from your FB-Program.
But I would just use the serial object. That is not the problem. I have the Arduino code for that from the internet. A nice beginner example is sending a number from the pc to the arduino and it using that to control a servo motor or led flash rate. The return example is usually from a analog input transducer to say log the temperature.
To ask for Arduino Code on this Forum is the wrong approach, ask for this in Arduino-Forums.
Here we are after all, FreeBASIC programmers first ...
But I don't need the Arduino code I need the FreeBASIC code and I will not get that from the Arduino forum. The example code for the pc side is given in Python (or c#) that uses a serial library for which there is no FB equivalent.

By the way thank you for the FreeBASIC code you did provide and perhaps with some study when I have the time I will figure out the FB side of the problem.
.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Post by BasicCoder2 »

caseih wrote:You say you need a complete example. What kind of example would you like? I could probably crank out a simple arduino sketch and a corresponding FB program that talks with it.
http://www.meccanismocomplesso.org/en/a ... -actuator/

Essentially it would amount to using an arduino board like you use a k8055 except you can write your own commands to tell the arduino board what you want it to do and what data you want it to read. Thus the arduino needs to be able to wait for a command (say a number for each command) which it then carries out. That command may include data and it may require data to be read and returned.

Time is the only issue as I know how to do all the electronics and c programming.
.
Last edited by BasicCoder2 on Feb 24, 2016 4:22, edited 1 time in total.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: FreeBasic and Arduino communication

Post by caseih »

Yes that's totally possible. But if I were to implement all that I don't think it would help you. We have to start much simpler. Sounds like you need to master basic serial communication first, then you can build on that. i feel like your frustrations are partly stemming from being over-ambitious, at least at this early stage.

I'm unclear as to whether your problem is with Arduino or FB. I suspect both. I'll try to work out a simple call and response arduino code that can work with FB.

In the meantime, have you looked at the official arduino docs on serial communication?
Post Reply