FreeBasic and Arduino communication

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

FreeBasic and Arduino communication

Postby BasicCoder2 » Jan 12, 2016 8:14

Is it possible to write a sketch to communicate with a PC program written in FreeBASIC as you can with the K8055 board?
For example so a FreeBasic program can receive data gathered by a Arduino program/hardware say temperature readings and display them on the computer screen. Also for a FreeBasic program to send a request for a certain action by the Arduino.
.
MrSwiss
Posts: 2020
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FreeBasic and Arduino communication

Postby MrSwiss » Jan 12, 2016 10:51

Yes, it is definitely possible!

However, the Problem is mainly the Sketch, which has to implement some sort of
PROTOCOL, which is thereafter used for 2-Way Communication:
e.g. the Commands received (from external Prog., not necessarily FB) and how the
ARDUINO is reacting upon such Command ...

Example:
ext. Prog. sends "c" or "C" --> ARDU
ARDU understands "collect, then send collected Data"
ARDU executes "whatever is asked for" and then waits for the next REQUEST/COMMAND ...

[edit]Since ARDUINO provides a VCP-Driver (virtual COM Port) from a FB Perspective, the
usual COM-Port related Commands should do the Trick.[/edit]
speedfixer
Posts: 221
Joined: Nov 28, 2012 1:27
Location: California

Re: FreeBasic and Arduino communication

Postby speedfixer » Jan 12, 2016 17:12

The arduino can operate just like a remote processor:

you program it - it does what you ask

Various arduino software packages make it trivial to write a program that will:

exchange info with the computer on demand, timed in a loop, or dump info on a threshold programmed into the device.

All can be available in the same program running at one time: there is enough memory on most arduinos to do this and more.

Those and the many other small processor 'kits' or design boards - for many different processors - make working on what would be used as a dedicated processor cheap, simple and fun.

I forgot to mention:
libraries are available for communication, device control (temp sensors, for example), even full terminals, so much more - free
BasicCoder2
Posts: 2874
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Postby BasicCoder2 » Jan 12, 2016 19:49

Here is a simple example from the Arduino manual.

Code: Select all

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

void loop()
{
    Serial.println(analogRead(0))
    delay(1000);
}


I am not sure if the serial object refers to the same usb connection used to load a sketch into the arduino or some other kind of connection?

I was kind of hoping someone had some FreeBASIC code for reading and writing data via the usb port between the pc and the arduino.

At a pinch I could communicate via a K8055 board by connecting some of the i/o pins of the K8055 board and the i/o pins of the Arduino board and writing a protocol in FreeBasic for the pc and a protocol in Sketch for the Arduino.

Of course in ye olden days I would have simply used the parallel port to connect to two of the Arduino's pins.

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

Re: FreeBasic and Arduino communication

Postby MrSwiss » Jan 12, 2016 20:37

BasicCoder2 wrote:I am not sure if the serial object refers to the same usb connection used to load a sketch into the arduino or some other kind of connection?
They are one and the same: e.g. if you are using COM8 to download the Sketch, then the whole PC <--> Arduino Communication runs over COM8. As I've stated above: via VCP Interface ... of the Driver.

Sorry I've NO Implementation in FB, but I've done it with "Graphic Programming" (ProfiLab-Expert).
The IMO best Start in Arduino Environment is: Examples\Communication\SerialCallResponseASCII - Sketch
BasicCoder2
Posts: 2874
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Postby BasicCoder2 » Jan 12, 2016 21:59

Just "discovered" the Arduino serial monitor to receive and print the above "hello world" example.

So I guess I have to figure out how to read/write to com ports from a FreeBASIC program to do the same thing.

Strange thing happened when I plugged in a new Arduino board. It assigned to it COM9 insisting that COM8 was being used and thus failed to load the Sketch. When I plugged in my previous Arduino board (same model type) it accepted it and worked fine. Somehow the two boards are being taken as two different boards even when only one is plugged in at any given time.
.
MrSwiss
Posts: 2020
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FreeBasic and Arduino communication

Postby MrSwiss » Jan 13, 2016 9:50

BasicCoder2 wrote:Just "discovered" the Arduino serial monitor to receive and print the above "hello world" example.
Yep, this is useful for Debugging of the Sketch running on the Arduino, e.g. checking whether it's Output is "as expected".
BasicCoder2 wrote:Strange thing happened when I plugged in a new Arduino board. It assigned to it COM9 insisting that COM8 was ...
I think this is "on Purpose" by checking not only the Board Type, but probably also it's Serial No. in Order to prevent "loading of Sketch" to the "wrong" Board.

You might want to run more than just one of them, from a single PC. To do so, a defined "Separation Mechanism" is needed. Other Devices such as LabJack's configure a Board ID [0 to 127] in Order to achieve this ... since USB is a Bus-System (unlike the Original RS232 Device, Point to Point).
BasicCoder2
Posts: 2874
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Postby BasicCoder2 » Feb 10, 2016 1:13

Found this and it works ok but was unable to figure out how to reverse the process so commands could be sent from a FreeBASIC program to the Arduino.

viewtopic.php?t=13062
Last edited by BasicCoder2 on Feb 23, 2016 5:40, edited 1 time in total.
MrSwiss
Posts: 2020
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FreeBasic and Arduino communication

Postby MrSwiss » Feb 10, 2016 12:38

@BasicCoder2,
you seem to be far to much "focused" on FB, at the moment. Have a look at Arduino first!

Like I've tried to explain before, you've first to have a Arduino-Sketch, that is able to handle
2-way Communication (the one you're referring to, is only "sending"!). Examples are found:
arduino.cc as a Base, as well as in the Arduino IDE under Examples.

As a Starter, a (by me) modified version of above Example:

Code: Select all

// choped down/extended: Serial Call and Response in ASCII, © 2016 by MrSwiss
// Port Speed = 115200 bps
// only the Arduino code part, Wiring Code and Comments removed!

int Analog0 = 0;    // first analog sensor
int Analog1 = 0;    // second analog sensor
int Digital2 = 0;   // digital sensor
int inByte = 0;     // incoming serial byte

void setup() {
  // start serial port at 115200 bps
  Serial.begin(115200);
  pinMode(2, INPUT);   // digital sensor is on digital pin 2
  establishContact();  // send a byte to establish contact until receiver responds
}

void loop() {
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
   // MrSwiss extension: switch (in C) = select case (in FreeBASIC)
   // Action depends on received Byte (Command), read Comments below ...
   switch (inByte) {
      // accepts "C" and "c"
      case 67:
      case 99:
        // GetData;
        Analog0 = analogRead(A0);
        Analog1 = analogRead(A1);
        Digital2 = map(digitalRead(2), 0, 1, 0, 255);
        Serial.print(Analog0);
        Serial.print(",");
        Serial.print(Analog1);
        Serial.print(",");
        Serial.println(Digital2);
        break;
      // accepts "A" and "a"
      case 65:
      case 97:
        Serial.println("command ERROR");
        break;
      default:
        // if nothing else matches, do the default (is optional)
        Serial.println("unknown ERROR");
        break;
    }           
  }
}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.println("0,0,0");   // send an initial string
    delay(500);
  }
}

Above is Arduino-Code (in Arduino-C). For further advice on Arduino programming contact:
http://www.arduino.cc - Forums

How it works, in short:
    - Arduino seeks connection, by sending "0,0,0" (String) // from: establishContact() / void Function() in C
    - if it doesn't get anything in, it goes to "Sleep" (after reaching a timeout)
    - if it gets a Byte in, it ends "establishContact()" and starts to run it's MAIN-LOOP
    - this waits for a Byte (Command)
    - then executes on: cC, aA, everything else (default)
above is easy to see if you are using Serial-Monitor (of Arduino IDE).

Only once this is established to function properly, you can start with the FB-side of things ...
BasicCoder2
Posts: 2874
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Postby BasicCoder2 » Feb 10, 2016 20:41

MrSwiss wrote:@BasicCoder2,
you seem to be far to much "focused" on FB, at the moment. Have a look at Arduino first!


I do know how handshaking works having done it often on the older machines via the parallel port.

Today it must be all done using other people's code and they have to tell you how to use their code.

The details of how other people have implemented it is hidden in the Serial object and the connection protocol of the OPEN COM statement.

As I say to those who ask me how to do something on the computer I reply how would I know I didn't write the code.

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

Re: FreeBasic and Arduino communication

Postby MrSwiss » Feb 10, 2016 21:09

I do know how handshaking works having done it often on the older machines via the parallel port.
Today it must be all done using other people's code and they have to tell you how to use their code.

I don't understand what you're complaining about. Even in "the old days" you where using "other peoples code".
Example: the BIOS Programmers code to get your Parallel Port to work ...
BasicCoder2
Posts: 2874
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Postby BasicCoder2 » Feb 10, 2016 23:05

@MrSwiss,

Actually I programmed the graphics hardware myself. Back in ye olden days the hardware specifications were made available and not encapsulated in other people's code. With the C64 you could program the chips yourself. With the Amiga you could also turn off the operating system and program its graphic and sound chip directly. I could set a pixel on the old Amiga 7Hz machine at the same speed as I moved the mouse thus no need for a line routine to join the points.

It was not a complaint just a statement of fact. In order to do something you need the information to do it in a form and level commensurate with your particular skill level. I haven't had the time let alone the motivation to learn all the convoluted complexity that now overlays the hardware. Yes I understand it can make programming faster even if the code itself runs slower. Thanks to faster machines the overhead isn't an issue.

With regards to the Arduino I could just program a microchip in machine code instead but using a C compiler and other people's code makes it faster and easier once you learn how their code works at the user level. Soldering up your own interface circuits also takes time so it is faster to learn how to use someone else's hardware.
.
caseih
Posts: 1034
Joined: Feb 26, 2007 5:32

Re: FreeBasic and Arduino communication

Postby caseih » Feb 11, 2016 2:12

This is the first time I've heard Arduino referred to as convoluted complexity with lots of overlays over the hardware! Short of using assembler, this is as low as you can get, level-wise. You are working fairly directly with hardware. And if you want to program arduino in assembly language, you are free to do so.

For me using C is near enough to low level and the hardware is right there, ready to access (directly I might add). How is looking at an example of how to do something is using someone else's code? Learning by example is an important way to do it. I've written plenty of arduino code from scratch. I've also used third-party libraries when appropriate.

If everything were done in assembler you'd still have the same problem which is you need to know more about arduino architecture and programming before you can just interface with it from FreeBasic. Arduino is not complex, and it's not hard at all to get into it. You can start with 4 lines of code in the Arduino IDE. It's dirt-simple. I think you could learn it if you wanted to.
BasicCoder2
Posts: 2874
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Postby BasicCoder2 » Feb 11, 2016 3:16

caseih wrote:This is the first time I've heard Arduino referred to as convoluted complexity with lots of overlays over the hardware!

I was referring to a modern computer and its multitasking operating system and layers of code placed over the modern priority hardware. It is a natural evolution from the simple to the complex as information accumulates over time as to how to best achieve some outcome.

How is looking at an example of how to do something is using someone else's code?

Not sure you followed my point. I don't have access to the workings of Excel so if someone says how do I do x,y or z I would say read the manual. It will not tell you how it works only how to make use of it. It is like driving a car with no idea of what is under the hood.

I've written plenty of arduino code from scratch.

I have only just started although I have had the board for some time.
Last edited by BasicCoder2 on Feb 23, 2016 5:50, edited 1 time in total.
speedfixer
Posts: 221
Joined: Nov 28, 2012 1:27
Location: California

Re: FreeBasic and Arduino communication

Postby speedfixer » Feb 11, 2016 17:52

A couple of years ago - I used this as my learning base for talking to an Arduino.

Libraries change; FB wasn't 64 bit.

Code: Select all

' ard1.bas

' test talking to arduino

    #include "string.bi"         ' needed for format function
    Dim LineCount As LongInt      ' just so we can see how many lines are read
    Dim chrcount as Long
    Dim inchar As Byte               ' this is our incoming byte of data
    Dim InBuffer As String         ' this is our buffer to collect the bytes
    Dim PortStr as String
    InBuffer=""
    LineCount=0
    chrcount=0


dim as integer filnum
filnum = freefile

    PortStr = "COM12:9600,N,8,1,CD,CS,DS,OP,BIN"


    shell "stty -F /dev/ttyUSB0 speed 9600 -clocal -hupcl"
    sleep 1000
    open com "/dev/ttyUSB0:9600,n,8,1,cs0,cd0,ds0,rs" as #filnum

    sleep 1000, 1

    ' loop until there is a keypress ... any keypress
    while inkey <> "" : wend
do
        If Not(EOF(filnum)) then
            ' get a single byte from the serial port
            Get #filnum,0,inchar,1
            ' characters below ASCII 32 are 'non-printing characters
            ' characters above ASCII 126 are not define (by ASCII)
            ' append any printable character to the string
            If (inchar > 31) and (inchar < 127) Then
                InBuffer = InBuffer + Chr(inchar)
                chrcount=chrcount+1
            else
                ' ignore this character
            End If
            ' Linux/Unix terminate strings with a line feed (ASCII 10)
            ' MACs terminate lines with a carriage return (ASCII 13)
            ' Microsoft and Arduino use carriage return/linefeed (ASCII 13,10)
            ' If we get any of the above then increment the line count and
            ' print the string but only if we have something to print.
            If ((inchar=13) Or (inchar=10)) And  (chrcount >0) Then
                LineCount = LineCount + 1
                Print  Format(LineCount,"000000") +" ("+ Format(chrcount,"000") + "):  " + Inbuffer
                ' clear the buffer so that we can do it again
                InBuffer=""
                chrcount=0
            End If
        End If
        Sleep 25, 1
    if inkey <> "" then exit do
loop
    Close #filnum

    End


Serial speed MUST be setup correctly.
The Arduino tends to reset on a com start and stop, especially simply grabbing and releasing the PC port. The Arduino program always stays. If you wanted to return and collect data later, it may reset on re-accessing the device.
The Arduino comm lines are really sensitive to extraneous noise. You have to be really careful taking your signals off of and into the Arduino board.
The branching example in the sketch by Mr. Swiss is a good suggestion for managing the Arduino from the FB side. There tends to be more space on the Arduino than you expect after you start playing with it.

david

Return to “Hardware Interfaces / Communication”

Who is online

Users browsing this forum: No registered users and 1 guest