Quantcast
Channel: Xojo Programming Forum - Latest topics
Viewing all articles
Browse latest Browse all 3699

Serial communication with Arduino

$
0
0

My ultimate goal here is to establish a serial link between an Arduino and a raspberry pi 4, using Linux code created from XoJo. As a first step, I attempted to get serial comms working between my mac m1 running xojo and an arduino. Unfortunately I have not had luck getting it working. Xojo is able to connect to the Arduino serial port and recieves the first transmission “0,0,0” from arduino and then nothing is heard or sent either way…

Arduino code is a modified serial comms example:

int firstSensor = 0; // first analog sensor
int secondSensor = 0; // second analog sensor
int thirdSensor = 0; // digital sensor
int inByte = 0; // incoming serial byte

void setup() {
// start serial port at 9600 bps and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

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:
Serial.println(“getting ready to read”);
if (Serial.available() > 0) {
Serial.read();
Serial.println(“This is a response”);

}
delay (1500);
}

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

not sure how to show xojo code here. The serialConnection “data received” event does this:

Var data As String
data = Me.LookAhead(Encodings.ASCII)
If data.IndexOf(EndOfLine.Windows) > -1 Then
TextArea1.AddText (Me.ReadAll(Encodings.ASCII)+chr(10)+chr(13))
End If

Sending serial data is done like this:

SerialConnection1.Write (TextField1.Text)
SerialConnection1.Flush

Any help is appreciated!

Matt

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 3699

Trending Articles