Serial Port comunication witch arduino

Just starting out? Need help? Post your questions and find answers here.
xxfreshman
User
User
Posts: 17
Joined: Mon Jun 13, 2016 8:22 pm

Serial Port comunication witch arduino

Post by xxfreshman »

Hi people, i dont know anymore what to do, i programmed a arduino with some commands, if i send with 9600 baud a "a" it steps a motor left and gives a "o" as ok, with putty everything works fine, with purebasic i can send but recive is not working, it starts that AviableSerialInput recognises nothing there, what is your idea?

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
Port$ = "COM6"
CompilerElse
Port$ = "/dev/ttyS5"
CompilerEndIf

*buffer = AllocateMemory(128)
Puffer=Asc("a")
RPuffer.a=0

If OpenSerialPort(0, Port$, 9600, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 1, 10)
Debug "Successfully connected"
a=0
Repeat
a=a+1
b=0
Ergebnis = WriteSerialPortString(0, "a", #PB_Ascii)
;Debug "t"
Repeat
If AvailableSerialPortInput(0)
Ergebnis = ReadSerialPortData(0, @RPuffer, 1)
;Debug RPuffer
Debug "t"
If Chr(RPuffer)="o"
b=1
EndIf
EndIf
Until b=1
Debug "t"
;Delay(500)


Until a>100

Else
MessageRequester("Error", "Can't open the serial port: "+Port$)
EndIf
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Serial Port comunication witch arduino

Post by infratec »

Can you try this:

Code: Select all

EnableExplicit

Define Port$, a.i, Ergebnis.i, RPuffer.a, b.i

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  Port$ = "COM6"
CompilerElse
  Port$ = "/dev/ttyS5"
CompilerEndIf


If OpenSerialPort(0, Port$, 9600, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 128, 128)
  Debug "Successfully connected"
  
  Repeat
    a + 1
    RPuffer = 'a'
    If WriteSerialPortData(0, @RPuffer, 1) = 1
      Debug "t"
      
      Repeat
        b = 100
        If AvailableSerialPortInput(0)
          If ReadSerialPortData(0, @RPuffer, 1) = 1
            ;Debug RPuffer
            Debug "r: " + Chr(RPuffer)
            If RPuffer = 'o'
              Debug "ok"
              Break
            EndIf
          EndIf
        Else
          Delay(10)
          b - 1
        EndIf
      Until b = 0
      If b = 0
        Debug "r timeout"
      EndIf
      
    EndIf
    
    Delay(500)
    
  Until a = 10
  
Else
  MessageRequester("Error", "Can't open the serial port: " + Port$)
EndIf
xxfreshman
User
User
Posts: 17
Joined: Mon Jun 13, 2016 8:22 pm

Re: Serial Port comunication witch arduino

Post by xxfreshman »

Its still only

Successfully connected
t


damn
xxfreshman
User
User
Posts: 17
Joined: Mon Jun 13, 2016 8:22 pm

Re: Serial Port comunication witch arduino

Post by xxfreshman »

Maybe a prb with newer Windows than ver 7???
xxfreshman
User
User
Posts: 17
Joined: Mon Jun 13, 2016 8:22 pm

Re: Serial Port comunication witch arduino

Post by xxfreshman »

No its not working sorr<, my fail, was only the restart from arduino :-(
Last edited by xxfreshman on Tue Oct 15, 2019 9:51 pm, edited 1 time in total.
TassyJim
Enthusiast
Enthusiast
Posts: 183
Joined: Sun Jun 16, 2013 6:27 am
Location: Tasmania (Australia)

Re: Serial Port comunication witch arduino

Post by TassyJim »

Do you need to send #CR$ or #CRLF$ after the 't' command?

Jim
xxfreshman
User
User
Posts: 17
Joined: Mon Jun 13, 2016 8:22 pm

Re: Serial Port comunication witch arduino

Post by xxfreshman »

no i need only to send a or b c d e not more, in putty i see the o as answer, and it works perfekt i changed allready to xon xoff, but still nothing.
Last edited by xxfreshman on Tue Oct 15, 2019 10:04 pm, edited 1 time in total.
xxfreshman
User
User
Posts: 17
Joined: Mon Jun 13, 2016 8:22 pm

Re: Serial Port comunication witch arduino

Post by xxfreshman »

if i send endles a "a" it works but i need to get the "o" back this says me that the step motor was fine moving, and reciving this is not working.
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Serial Port comunication witch arduino

Post by infratec »

Please post a complete debug output of my code.
TassyJim
Enthusiast
Enthusiast
Posts: 183
Joined: Sun Jun 16, 2013 6:27 am
Location: Tasmania (Australia)

Re: Serial Port comunication witch arduino

Post by TassyJim »

I didn't have an Arduino handy but, using something similar, infratec's code works as expected
Successfully connected
t
r: o
ok
t
r: o
ok
t
r: o
ok
t
r: o
ok
t
r: o
ok
t
r: o
ok
t
r: o
ok
t
r: o
ok
t
r: o
ok
t
r: o
ok
My BASIC code on the device:

Code: Select all

  DO
    k$ = INKEY$
    IF k$ = "a" THEN
      'pause 100
      PRINT "o";
    ENDIF
    PAUSE 10
  LOOP
I tried different delays and with/without CR in the reply and everything still worked.

Jim
xxfreshman
User
User
Posts: 17
Joined: Mon Jun 13, 2016 8:22 pm

Re: Serial Port comunication witch arduino

Post by xxfreshman »

Arduino code


#include <Stepper.h>

const int stepsPerRevolution = 4;

Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);

int ledPinb = LED_BUILTIN;
int ledPin = 3;
int fadeValue = 0;
void setup() {
pinMode(ledPinb, OUTPUT);
digitalWrite(ledPinb, LOW);

// Set the speed to 5 rpm:
myStepper.setSpeed(2000);

Serial.begin(9600);

}

void loop() {
if (Serial.available()) {
char received = (char)Serial.read();
//Serial.print(received);

if (received == 'a') {
digitalWrite(ledPinb, HIGH);
Serial.print("o");
myStepper.step(stepsPerRevolution);
//delay(500);
} else if (received == 'b') {
digitalWrite(ledPinb, LOW);
Serial.print("o");
myStepper.step(-stepsPerRevolution);
//delay(500);
}
if (received == 'c') {
if (fadeValue < 100) {
Serial.print("o");
fadeValue=fadeValue+1;
analogWrite(ledPin, fadeValue);
}

//delay(500);
} else if (received == 'd') {
if (fadeValue > 0) {
Serial.print("o");
fadeValue=fadeValue-1;
analogWrite(ledPin, fadeValue);
}
//delay(500);
}

}

}


AS I TOLD WITH PUTTY ITS WORKING FINE






NOT WITH PB.
xxfreshman
User
User
Posts: 17
Joined: Mon Jun 13, 2016 8:22 pm

Re: Serial Port comunication witch arduino

Post by xxfreshman »

could it that there is a bug that no reciving is possible?
xxfreshman
User
User
Posts: 17
Joined: Mon Jun 13, 2016 8:22 pm

Re: Serial Port comunication witch arduino

Post by xxfreshman »

and again if i open a putty connection to this arduino code, i can set a "a" i get a "o" so why damn i get nothing in pb?
TassyJim
Enthusiast
Enthusiast
Posts: 183
Joined: Sun Jun 16, 2013 6:27 am
Location: Tasmania (Australia)

Re: Serial Port comunication witch arduino

Post by TassyJim »

I am using PB V5.71 on Widows 10
I tried both 32 and 64 bit versions.
I loaded your Arduino code onto a nano clone.

infratec's code runs as expected.
Your original code works but nowhere were you indicating that the "o" has been received.
I added one line to do this,

Code: Select all

Debug "t"
        If Chr(RPuffer)="o"
          Debug "o"
          b=1
        EndIf
and your code successfully prints "o"
Successfully connected
t
o
t
t
o
t
t
o
I have run out of ideas.
Jim
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Serial Port comunication witch arduino

Post by infratec »

To see a bit more at the output:

Code: Select all

#include <Stepper.h>

const int stepsPerRevolution = 4;

Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);

int ledPinb = LED_BUILTIN;
int ledPin = 3;
int fadeValue = 0;

void setup() {
 pinMode(ledPinb, OUTPUT);
 digitalWrite(ledPinb, LOW);
 
 // Set the speed to 5 rpm:
 myStepper.setSpeed(2000);
 
 Serial.begin(9600);
}

void loop() {

 char Rcv;

 if (Serial.available()) {
  
  Rcv = (char) Serial.read();

  switch(Rcv) {
   case 'a':
    digitalWrite(ledPinb, HIGH);
    Serial.print("l");
    myStepper.step(stepsPerRevolution);
    break;
   case 'b':
    digitalWrite(ledPinb, LOW);
    Serial.print("r");
    myStepper.step(-stepsPerRevolution);
    break;
   case 'c':
    if (fadeValue < 100) {
     Serial.print("u");
     fadeValue++;
     analogWrite(ledPin, fadeValue);
    } else {
     Serial.print("t");
    }
    break;
   case 'd':
    if (fadeValue > 0) {
     Serial.print("d");
     fadeValue--;
     analogWrite(ledPin, fadeValue);
    } else {
     Serial.print("b");
    }
    break;
   default:
    Serial.print("-");
  }
 }
}
Not tested or compiled.
Post Reply