FreeBasic and Arduino communication

For issues with communication ports, protocols, etc.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: FreeBasic and Arduino communication

Post by caseih »

By the way, BasicCoder2, I was having problems with either handshaking or flow control when I was trying to transmit to this serial device from FB. It's a 3-wire affair with just gnd, TX, and RX (I'm using a special USB to TTL serial adapter, not RS232). I solved the problem with this incantation:

Code: Select all

open com "/dev/ttyUSB0:38400,n,8,1,BIN,CS0,CD0,TB0,RS" as sp
This seems to disable enough of the hardware flow control stuff that it worked. Technically i could have used ASCII mode as well, as the protocol is all line-based, terminating in CR/LF. But since my ultimate goal is to transfer the code to an arduino processor eventually, I wanted to have it work on the byte-level rather than using things like FB's line input or input routines. If you have to work over just three wire serial, you may need to do something similar.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Post by BasicCoder2 »

caseih wrote:I solved the problem with this incantation: ...
Shades of Harry Potter!
It has been the magical incantations of the OPEN COM of FreeBASIC that I was unfamiliar with as I have never used them before. All my previous interfacing was done via parallel ports. However when the direct access to such ports vanished along with the ports themselves the language you used had to be able to navigate the multitasking operating system of a modern computer and read/write to a USB port. With FreeBasic that is the OPEN COM statement.

I guess an advantage of using a serial to to RS232 or parallel port adapter is you could program everything in FreeBASIC.
... my ultimate goal is to transfer the code to an arduino processor eventually ...
So why not start there to begin with?

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

Re: FreeBasic and Arduino communication

Post by MrSwiss »

BasicCoder2 wrote:I find the need for a delay less than robust although I see it used that way in other example code.
There isn't anything "less than robust", on the Contrary it helps to make things more "robust".

Many People tend to forget, that even Electronics, being really fast, takes Time to do its job!
It may be in the Range of a couple of Nano-Seconds, but still Time ...
Software also needs Time to run, on a far larger Scale, a couple of Milli-Seconds, perhaps.
Electro-Mechanical Devices, such as a Servo, can be even slower than Software. This being
the Reason to add a Software Delay, to prevent the Software to 'overtake' the Hardware's
Capabilities.

So much Philosophy for Today ...
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: FreeBasic and Arduino communication

Post by caseih »

BasicCoder2 wrote:So why not start there to begin with?.
It's faster and more convenient to develop on the computer first. I had to reverse-engineer the protocol after all, and having to recompile and upload after every change is a bit slow. So prototype in FB (because serial interfacing is easy there) in order to get it right, then translate to Arduino later. Ordinarily I'd prototype in Python, but I thought I'd use FB this time, and was pleased with the results. FB's old-fashioned locate and print served my purposes very well: I could simulate my little 2-line display in a window using normal FB screen output commands. In Python I'd have to use ncurses and I didn't want to have to learn that just yet.

In case you're curious what I'm trying to do is develop a remote display for an instrument. The instrument communicates with it's internal display via a simple serial protocol. I will have an arduino and a 2-line LCD screen that will connect over a cord, allowing me to have a remote display that functions the same as the internal one, but more conveniently located! The display is actually two-way. The buttons on the display send a command string back to the instrument. I will duplicate that as well. The only caveat is that while a TX line can go to two display units okay, the RX line coming back from display can only take one input at a time for reasons of signalling and collisions. So I will either have a switch (physical or automatic with transistor logic) to physically change the instrument's RX between the two display units, or maybe down the road make a little mini arduino demultiplexer that listens to both displays and sends the data on down the single line to the instrument. One wrinkle to deal with is that the serial lines are 3.3v and Arduino is 5v, which it uses even on it's serial lines. So I'll need a logic level converter.

Anyway, I'm pleased with my progress and your queries here on the forum inspired me to get it working.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Post by BasicCoder2 »

MrSwiss wrote:
BasicCoder2 wrote:I find the need for a delay less than robust although I see it used that way in other example code.
There isn't anything "less than robust", on the Contrary it helps to make things more "robust".
Instead of sleeping for the maximum time for the Arduino to do its thing it is better if the FreeBASIC program can get on with other things until the Arduino tells it it has completed its task.

Code: Select all

do

  if arduino ready for next command
     send command
  end if
  
  do other stuff
  
 loop
 
.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FreeBasic and Arduino communication

Post by MrSwiss »

BasicCoder2 wrote:Instead of sleeping for the maximum time for the Arduino to do its thing it is better if the FreeBASIC program can get on with other things until the Arduino tells it it has completed its task.

Code: Select all

do

  if arduino ready for next command
     send command
  end if
 
  do other stuff
 
 loop
Yes, agreed, if there is something else, to do (which, in my Example, is NOT the Case!).

However, in your proposed way, the Arduino isn't called at a 'timed' Interval any longer. You'd need addi-
tional Timing-Code to restore that Behaviour, e.g, measure the Time "do other stuff" needs to run and
then subtract that Time from the Sleep Time. It isn't always as simple, as it might look, at "first Sight".

This is the Main-Reason it is called a Example, and NOT a Application (it highlights a certain Point only,
leaving other Considerations by the Side, for ease of Readability/Understanding).
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: FreeBasic and Arduino communication

Post by D.J.Peters »

@BasicCoder2 This is why I wrote the event driven COM port stuff.

You don't have to polling for any incoming data.

Code: Select all

while no_new_data()
  sleep 20
wend
If your Arduino sends any data a callback are called automatical.

Code: Select all

sub ArduinoCB(pBuffer as ubyte ptr,size as integer)
  ' do anything with the incomming data
end sub
Joshy
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Post by BasicCoder2 »

MrSwiss wrote:
BasicCoder2 wrote:Instead of sleeping for the maximum time for the Arduino to do its thing it is better if the FreeBASIC program can get on with other things until the Arduino tells it it has completed its task.
This is the Main-Reason it is called a Example, and NOT a Application (it highlights a certain Point only,
leaving other Considerations by the Side, for ease of Readability/Understanding).
Indeed and thank for your examples for they seem to be sufficient at this stage for me to set up a simple two way communication between a FreeBASIC program and the Arduino. The servo example is working without a hitch although my robot project doesn't use servo motors instead I am using ordinary dc motors with encoders to control how many times they rotate any direction.
.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Post by BasicCoder2 »

D.J.Peters wrote:@BasicCoder2 This is why I wrote the event driven COM port stuff.
It is a lot of code and at this stage I am unsure how to use it. As you know I am not an advanced programmer like you and tend to think the way I did back in my MSDOS days. This is the reason I use FreeBASIC instead of being a top gun c++ programmer :)
.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Post by BasicCoder2 »

caseih wrote:In case you're curious what I'm trying to do is develop a remote display for an instrument.
Your description of what you are trying to do isn't all that clear to me. I read you as saying you want a remote keyboard and display for an electrical instrument that already has a keyboard and display? It doesn't seem to me that the code would be very complex and thus would be easy enough to develop up front using the Arduino board alone. I am not even sure where the serial connection comes into it? Does the current keyboard and display have a serial connection to the instrument?

In my case the PC+FreeBASIC will be the main controller and the Arduino board will serve it as the k8055 has done so up till now.
Anyway, I'm pleased with my progress and your queries here on the forum inspired me to get it working.
If the final project will become a purely Arduino project than other Arduino users might also be interested or helpful on an Arduino forum where you can also post images and the circuits of your project.
.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: FreeBasic and Arduino communication

Post by D.J.Peters »

BasicCoder2 wrote:It is a lot of code and at this stage I am unsure how to use it.
It's really easy (trust me) I ordered a cheap Arduino Uno R3 with MEGA328P chip and will show you how simple it is.

Joshy
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: FreeBasic and Arduino communication

Post by caseih »

BasicCoder2 wrote:Your description of what you are trying to do isn't all that clear to me. I read you as saying you want a remote keyboard and display for an electrical instrument that already has a keyboard and display? It doesn't seem to me that the code would be very complex and thus would be easy enough to develop up front using the Arduino board alone. I am not even sure where the serial connection comes into it? Does the current keyboard and display have a serial connection to the instrument?
Instrument meaning a specialized device that does GPS tracking and recording. The user interface is a 2-line display and a couple of buttons. The main board communicates with the screen and buttons in the same chassis over a serial link. There's a protocol that is used to tell the screen what to display and where. And when buttons are pressed, this information is transmitted back to the main board over the link. Think of the screen and buttons as it's own separate computer, because it actually is. This sort of modularity is very common in the embedded world. One microprocessor may do the main work while another one handles the UI. Another yet may interface with sensors. They all communicate together over busses of different kinds, usually i2c or serial.

And actually if you think about it your computer does something similar. When you press a key on the keyboard a microprocessor determines what key it was, debounces it, and sends a signal using a particular protocol over a particular bus (USB often). Even modern computer monitors have powerful computers in them to render the image coming over HDMI, DVI, or VGA signals, and drive the LCD matrix. Anyway I digress. Sorry to hijack your thread. Clearly our aims are slightly different.

My remote display unit will allow me to place the screen and buttons in a more convenient place while the main unit (with it's own screen and buttons built into the chassis) will be elsewhere. Here's an example of the protocol. It's an ASCII, line-based protocol:

Code: Select all

$PSLID,086,12D,12D,12D,12D,078,100,111,10A,104,10A,111,100,100,002,08F
$PSLID,10F,080,153,081,120,0CA,14E,14F,120,147,150,153,0C0,14F,176,172
$PSLID,172,169,164,165
Each of the three lines here is terminated with a CR/LF. Anyway, see if you can make sense of that (though without seeing the actual screen and what's being displayed it won't make as much sense). The first hex number is telling the display we want to write to row 0, column 6, and then the next 4 numbers tell it to write "----" to the screen. The number after that (078) tells the screen that ASCII character 15 is going to be defined to be a custom bitmap, which the next 8 numbers specify (8 rows of bitmaps for one character), and the 002 specifies the end of the definition. It goes on from there, using a similar pattern.

So it's simple enough , but it took me a while to figure out it. Looking at the numbers in binary helps me identify patterns I can look for. A programmer's calculator is pretty important here to easily convert between hex, decimal, and binary.

Anyway the reason they use a protocol is so that the screen unit can easily detect valid data and ignore stuff it doesn't recognize such as errors or noise in transmission. One thing that tripped me up is that when a character is redefined to a custom bitmap, it changes all existing instances on the screen already to this new bitmap. Lots of trial and error! This is why the PC is logical to work on first, not the Arduino. Debugging is much easier!
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: FreeBasic and Arduino communication

Post by D.J.Peters »

Hello BasicCoder2
I got the cheap Arduino UNO rev. 3 today.
I readed before the cheap CH340G USB TTL chip would neead extra drivers but not in my case.
I plugged the device in Windows 10 64-bit, Linux Slackware 32-bit and Ubuntu 64-bit and it works.

Joshy

Here are a simple Arduino test code.

Code: Select all

// FBArduino01
#define LED 13

void setup() {
  // initialize serial communication at 9600 bits per second
  Serial.begin(9600);
  // wait for serial port to connect.
  while (!Serial) ;

  // make the LED pin as output
  pinMode(LED,OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(LED, HIGH);
  Serial.println("LED on");
  delay(1000);

  digitalWrite(LED, LOW);
  Serial.println("LED off");
  delay(1000);
}
Here are the FreeBASIC RS232 part:

Code: Select all

' fbarduion01.bas
#include "RS232.bas"

#define COMPORT    3 ' <--- !!! define your Arduino COM port here !!!
#define BAUDRATE 9600 ' use the same serial speed as in your arduino sketch

dim shared as RS232 Serial

' NOTE: On all Windows OS's the serial device works in binary mode only.
' So if you waiting for a complete string you have to collect all
' incomming data until chr(10) !"\n" are received.
sub ReceiveStringCB(byval buffer as ubyte ptr, byval size as integer)
  static as string msg
  dim as boolean StringComplete
  dim as integer n
  ' copy chars from buffer to string
  while n<size
    ' it's not a printable char
    if buffer[n]<32 then
      ' it's a new line/string char
      if buffer[n]=asc(!"\n") then
        StringComplete=true 
        exit while
      end if
    else
      msg &= chr(buffer[n])
    end if
    n+=1
  wend
  if StringComplete then
    line (0,12)-step(639,8),0,BF
    draw string (0,12),"arduino says: " & msg
    if instr(msg,"on") then
       circle (320,240),100,2,,,,F
    else
       circle (320,240),100,0,,,,F
    end if
    circle (320,240),100,15
    msg=""
  end if
end sub

' faked arduino Setup() style
sub Setup constructor
  screenres 640,480
  Serial.EventReceive = @ReceiveStringCB
  if Serial.Open(COMPORT,BAUDRATE)=false then
    draw string (0,0),"error: can't open RS232 at COM" & COMPORT & ":"
    beep : sleep : end 1
  else
    draw string (0,0),"press [ESC] to quit ..."
  end if
end sub

dim as boolean quit
while quit=false
  var char = inkey()
  if char=chr(27) then quit=true
  sleep 200
wend

Serial.Close()
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBasic and Arduino communication

Post by BasicCoder2 »

D.J.Peters wrote:I got the cheap Arduino UNO rev. 3 today.
And are you going to use it for any fun projects?

Later next week I will be getting some more sensors for the Arduino and maybe look at a more expensive version with more pins.

Your example works but the problem for me is understanding how to use the object orientated coding.

Also it only receives a message it doesn't send a command like one to change the pulse rate of the LED.

My approach was to have the FB program as the main controller much as I do with the K8055 whereas in your example the Arduino controls a FB subroutine.

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

Re: FreeBasic and Arduino communication

Post by D.J.Peters »

BasicCoder2 wrote:And are you going to use it for any fun projects?
Normaly I used all my MC's with codeblocks and the right GNU toolchain.
Now I tested the Arduino environment and it's slow :-(

I odered an small 2.4" TFT 240x320 pixels and 16 bit colors (565) with a touch screen for 3€ it has a ST7783 display controller.

A simple test inside loop() filling the whole screen in black and white needs 2.5 seconds per frame.

A hour ago I read the ST7483 doc and wrote my own controller interace and I get 3 fps on the cheap 16 MHz UNO R3.

So don't trust the Arduino environment on other side it's the first choice for breginers.

Joshy
Post Reply