FreeBasic communication with Arduino logic error?

For issues with communication ports, protocols, etc.
Dr_D
Posts: 2391
Joined: May 27, 2005 4:59
Contact:

Re: FreeBasic communication with Arduino logic error?

Postby Dr_D » Aug 18, 2019 0:38

Thanks for the image hosting site... I have no idea why imgur didn't work. .. probably something to do with money. lol
Anyway, I will post what I have, but I'm not out of ideas just yet.
I'm still learning... maybe I missed something simple.
If I can, I'd like to find it myself.
If not, I'll ask again... but hopefully, my next post will be have full working code, on both ends, and a simple diagram of the circuit. :)
D.J.Peters
Posts: 7852
Joined: May 28, 2005 3:28

Re: FreeBasic communication with Arduino logic error?

Postby D.J.Peters » Aug 18, 2019 5:18

dasyar
Posts: 363
Joined: Dec 04, 2008 15:31

Re: FreeBasic communication with Arduino logic error?

Postby dasyar » Aug 18, 2019 12:33

WOW! This turning out to be one great tutorial on serial programming.

I have been using PySerial for all of my serial comm programs, but I think that I will start at the beginning of this thread, and start giving the code examples a better look. Now , if you were to squeeze in some examples of how to implement some threading, that works in a meaningful way, that would cover a lot of serial comm programming.

Thanks for the great effort that is put into this.
Dr_D
Posts: 2391
Joined: May 27, 2005 4:59
Contact:

Re: FreeBasic communication with Arduino logic error?

Postby Dr_D » Aug 18, 2019 19:25

Ok... I know how crazy this image looks, but I'm not sure of another way to do it just yet. lol
Anyway, I removed any communication back from the Arduino, and the lag has pretty much gone away. The code for both sides is below, but the Arduino code hasn't changed much at all... basically just removed any data sent from that side.

Image

FreeBasic code...

Code: Select all

#include "fbgfx.bi"

screenres 640,480,32,,FB.GFX_HIGH_PRIORITY


dim shared DEVICE_COM as string
dim as boolean conOk = false
dim as integer ARD = freefile


for i as integer = 1 to 10

   DEVICE_COM = "COM" & str(i)

   DEVICE_COM = "COM3"

   if( open com(DEVICE_COM & ":9600,n,8,1,cs0,ds0,cd0,rs" for binary As #ARD) <> 0 ) then
      print "error connecting to Arduino on " + DEVICE_COM
   else
      conOK = true
      exit for
   end if

next


if conOK then

   print "Arduino connected on " + DEVICE_COM
   sleep 1000,1
else

   print "Failed to connect to Arduino"
   sleep 5000, 1
   end 1

end if


dim as string oTime, aTime, sHour, sMinute, sSecond, sTimeCode, temp


do


   oTime = aTime
   aTime   = time
   sTimeCode = ""

   temp   = bin(val(mid(aTime,1,2)), 6)
   sHour = ""
   for i as integer = len(temp) to 1 step -1
      sHour+=mid(temp,i,1)
   next

   temp = bin(val(mid(aTime,4,2)), 6)
   sMinute = ""
   for i as integer = len(temp) to 1 step -1
      sMinute+=mid(temp,i,1)
   next

   temp = bin(val(mid(aTime,7,2)), 6)
   sSecond = ""
   for i as integer = len(temp) to 1 step -1
      sSecond+=mid(temp,i,1)
   next

   temp = sHour+sMinute+sSecond

   for i as integer = len(temp) to 1 step -1
      sTimeCode+=mid(temp,i,1)
   next

   sTimeCode+=chr(13)

   screenlock

   if oTime<>aTime then
      cls
      print sTimeCode
      put #ARD,,sTimeCode
   end if

   screensync
   screenunlock

   sleep 3, 1

loop until multikey(FB.SC_ESCAPE)



Arduino code...

Code: Select all

/*
   Time Code Message Format:
   hhhhhhmmmmmmssssss<CR>

   hhhhhh = hours in binary text format ('0' or '1' characters only)
   mmmmmm = minutes in binary text format ('0' or '1' characters only)
   ssssss = seconds in binary text format ('0' or '1' characters only)
   <CR>   = end of message character chr(13)

   19 bytes total
*/

const int SER = 12;   // (595 pin 14)serial input line goes to the *first* shift register... daisy chain, so each QH(PIN 9) goes to the next SER, etc, etc...
const int SRCLK = 11; // (595 pin 11)the serial clock line, or the sync pulse, normal state is low... write, high, back to low, etc...
const int SRCLR = 10; // (595 pin 10)clear memory line, normal state is high... drop to low then back to high to clear memory...
const int RCLK = 9;   // (595 pin 12)copy register line, normal state is low... send high then back to low to copy states...
const int OE = 8;     // (595 pin 13)enable output line... send low to enable, high to disable...

const int TOTAL_SHIFT_PINS = 18;

void setup()
{
  pinMode(SER, OUTPUT);
  pinMode(SRCLK, OUTPUT);
  pinMode(SRCLR, OUTPUT);
  pinMode(RCLK, OUTPUT);
  pinMode(OE, OUTPUT);

  Serial.begin( 9600 );
  while ( !Serial )
    delay(1);

  //Serial.print( "Arduino is online - demo 5\n" );
  //Serial.flush();

  SR_clearRegisters();
  SR_copyToStorage();
  SR_turnOutputsOn();
}

void loop()
{
  // preserve inTimeCode[] and inCount between calls,
  // we may not receive the entire message in one loop through
  static uint8_t inTimeCode[19];
  static int inCount = 0;
  int16_t ch = -1;

  // read one char at a time (basically guarantees we never
  // see the whole message in one call to loop()
  ch = Serial.read();

  // valid char? add it to inTimeCode[]
  if ( ch != -1 )
  {
    inTimeCode[inCount] = ch;
    inCount += 1;
  }

  // buffer full, but no CR? it's bad, start over
  if ( (ch != 13) && (inCount == 19) )
  {
    inCount == 0;
    //Serial.write( "bad time code, too long/n" );
    //Serial.write( 13 );
  }

  // got a CR, but buffer not full yet? it's bad, start over
  if ( (ch == 13) && (inCount != 19) )
  {
    inCount == 0;
    //Serial.write( "bad time code, too short/n" );
    //Serial.write( 13 );
  }

  // got a CR and buffer full? maybe it's OK... let's check
  if ( (ch == 13) && (inCount == 19) )
  {
    for ( int i = 0; i < 18; ++i )
    {
      if ( (inTimeCode[i] != '0') && (inTimeCode[i] != '1') )
      {
        // bad data, start over
        inCount == 0;
        //Serial.write( "bad time code, invalid data/n" );
        //Serial.write( 13 );
        break;
      }
    }
    // we already know last char is CR, so no need to check that
  }

  // still have a full buffer?  it has to
  // be good... we threw all the bad data away
  if ( inCount == 19 )
  {
    SR_turnOutputsOff();
    for ( int l = 0; l < 18; ++l )
    {
      if (inTimeCode[l] == 49 )
      {
        SR_shiftDataIn(HIGH);
      }
      else
      {
        SR_shiftDataIn(LOW);
      }

      //Serial.write( inTimeCode[l]);
      //Serial.write( 32 );
    }
    SR_copyToStorage();
    SR_turnOutputsOn();
    //Serial.write( 13 );
    //Serial.flush();
    // done with the valid time code, start over, so we can get the next one
    inCount = 0;
  }
}


void SR_turnOutputsOn()
{
  digitalWrite(OE, LOW);
}

void SR_turnOutputsOff()
{
  digitalWrite(OE, HIGH);
}

void SR_clearRegisters()
{
  digitalWrite(SRCLR, LOW);
  digitalWrite(SRCLR, HIGH);
}

void SR_shiftDataIn(int data)
{
  digitalWrite(SER, data);
  digitalWrite(SRCLK, HIGH);
  digitalWrite(SRCLK, LOW);
}

void SR_copyToStorage()
{
  digitalWrite(RCLK, HIGH);
  digitalWrite(RCLK, LOW);
}
MrSwiss
Posts: 3307
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FreeBasic communication with Arduino logic error?

Postby MrSwiss » Aug 18, 2019 20:26

Dr_D wrote:Anyway, I removed any communication back from the Arduino, and the lag has pretty much gone away.
Have you tried with higher baudrate settings? (I never go below 19200, minimum)
I've run my *very old Arduino* (pre Ono model) at speeds up to (and including) 115200 baud.
The real HW communication is after all USB, the COM port is just virtual (VCP).
Dr_D
Posts: 2391
Joined: May 27, 2005 4:59
Contact:

Re: FreeBasic communication with Arduino logic error?

Postby Dr_D » Aug 18, 2019 21:42

MrSwiss wrote:
Dr_D wrote:Anyway, I removed any communication back from the Arduino, and the lag has pretty much gone away.
Have you tried with higher baudrate settings? (I never go below 19200, minimum)
I've run my *very old Arduino* (pre Ono model) at speeds up to (and including) 115200 baud.
The real HW communication is after all USB, the COM port is just virtual (VCP).


I haven't, but I will. Great idea. :)

Return to “Hardware Interfaces / Communication”

Who is online

Users browsing this forum: MSN [Bot] and 1 guest