Serial comms to Arduino

Continuing the discussion from Serial communication with Arduino:

I am back still trying to communicate with an ADRUINO via serial port. Just to be sure, I don’t need any special drivers, etc. to get my MacBook pro M1 to properly talk via the USB port to the Arduino, correct?

I still can’t seem to get XOJO code to talk to the Arduino. SerialConnection1 Data Received event never fires…

Try
  Var data As String = SerialConnection1.ReadAll
  If data <> "" Then
    TextArea1.AddText("Received: " + data)
  End If
Catch error As IOException
  MessageBox("Error reading from serial port: " + error.Message)
End Try

The Arduino (Adafruit METRO RP 2040) code behaves as expected when using the Arduino IDE serial monitor. Here is that code:

void setup() {
  // Initialize serial communication at 9600 bits per second
  Serial.begin(9600);
}

void loop() {
  // Check if data is available from the Raspberry Pi
  if (Serial.available() > 0) {
    // Read the incoming data as a string
    String incomingData = Serial.readStringUntil('\n');

    // Print the received data to the Serial Monitor (optional)
    Serial.print("Received: ");
    Serial.println(incomingData);

    // Send an acknowledgment back to the Raspberry Pi
    Serial.println("Acknowledged");
  }

  // Optional: Other code can be placed here
}

Is there some dead simple test I can use to insure XOJO is hearing the Arduino?

This is a simple program that connects to a serial device, allows you to send a message and prints out what is received.

Tested on Windows with an Arduino Uno.

serialtest.zip (9.9 KB)

[Edited By Author - reduced code block eliminating code not relevant to OPs question]
Where is Xojo Code that shows the setup of the COM port and serial properties for the COM port you’re trying to use (on the Xojo side)?

For example, I load all the available COM ports on application start into a pop up menu. Once the user selects the desired COM port I initialize it using the code below.

I use Xojo to communicate with the Arduino quite regularly.

''' Make sure a row is selectedin the popup menu, if not, bail out
If popComPorts.SelectedRowIndex = -1 Then
  Return 0
End If

''' Obtain the port id from the row tag of the selected row
Dim port As Integer = Val(popComPorts.RowTagAt(popComPorts.SelectedRowIndex))

''' Set the serial port id and baud rate
SerialConnection1.Device = SerialDevice.At(port)
SerialConnection1.Baud = SerialConnection.Baud57600

''' Try to open the serial port, if success return 1, if not return 0
Try
  SerialConnection1.Connect
  SerialCon = 1 '' Set connection flag
  Return 1
Catch error As IOException
  MessageBox("IOException SerialConnection1.Connect")
  rctCon.FillColor = clrRed
  Return 0
End Try

''' Failed; reset all of the serial port flags and restore window status to not connected
SerialCon = 0
rctCon.FillColor = clrRed
Return 0

Based on your Arduino code, you are not sending anything from Arduino unless you receive something, so I suspect all on Xojo side.

yes, my comm port selection is done like the XOJO serial example; a dropdown list populated with all the available serial ports, and a connect button. I think it is connecting, because i don’t get the connection failed message unless the arduino IDE is connected to that serial port.

Matt

UPDATE

I added a led flash on the arduino side when it receives serial data. Now when I send text from the XOJO program the LED flashes. So I know I am connected, and the arduino is getting data from XOJO code. But the response back to XOJO is not getting through somehow…

Just a suggestion. If you can use an Ethernet or WiFi interface you will save yourself a lot of headaches.

Arduino has neither…

Using your Arduino code and my Xojo test program on Windows with an Arduino Uno works (I sent the text ‘hello’):

image

Not sure if you are running into an issue with the Mac or the METRO RP 2040…I don’t have either to try.

If you haven’t already tried it I would suggest using a terminal program on your Mac (or the serial monitor in the Arduino IDE) and testing the communication with the Metro to verify that side is working correctly. Then move on to the Xojo side.

yes Arduino responds as expected in Arduino IDE serial monitor. Seems to be something on the XOJO side. Why would I be able to send to the Arduino from XOJO, but have the response blocked or ignored by XOJO? I put some delays in both sides of the code thinking timing might be the issue, but no change…

Matt

Arduino has both. A mkr1010 has Bluetooth LE, and WiFi. You can get and Ethernet shield for just about any board that does not have an Ethernet port. I have been programming these boards for a long time and you will be much happier with one of the many Ethernet protocols. There are many examples for both XOJO and Arduino.

You can’t have the serial monitor open and connect to the same comm port from another device. You will need to create another serial port on the Arduino, for example you can add a software serial port and use that to send data to the serial monitor (Serial2). Use the USB serial to communicate with your PC.

That’s true but I believe he would get an error/exception when he attempted to connect.

Glancing at information on the METRO RP 2040 - there is a lot going on with that board. I’m guessing the Arduino IDE is ‘hiding’ some of the complexity involved with communicating to the board. Have you tried connecting/communicating to it with just the Mac terminal? If not, I would suggest trying that.

It does on a Windows PC, not sure about a MAC. If he is using the comm port for the serial monitor, then it cannot communicate with another device. It also looks like that board has the standard shield headers so an Ethernet shield would work OK.

Good news but long story…

Well, evidently I order too much stuff. I would have bet that I didn’t have this board but looked in my ‘to play with’ pile and there it was. (Bought it a year ago…)

Relevant information - I see the same behavior as you: I can interact with it successfully via the Serial Monitor in the Arduino IDE. But external to the IDE, it appears to connect but as you stated the DataReceived event is never triggered.

I then tried CoolTerm (written in Xojo by Roger Meier) and it was able to communicate with the board successfully. He has a simpler open source version called OpenSimpleTerm. I downloaded the source to do some testing. It also communicated successfully with the board and after reviewing the source I found adding this line before the ‘Connect’ call makes it work for me:

SerialConnection1.DataTerminalReady = True

I am not an expert on serial communications and have never needed to set that for my projects that communicate with Arduinos and ESP32s - so I’m not sure why that is needed by the Metro board.

Hope that helps.

1 Like

OMG! that line did the trick! Thank you so much!

XOJO can now receive serial data from the Arduino. I also had to change the incoming data encoding to “ASCII” to be able to recognize the CR LF characters

Note that I turned on the “DTR” switch in the serial port inspector first, but that had no effect. The line you suggested was needed.

This stupid little problem has been holding me up for months and now I can move forward with the project. Everyone’s help is greatly appreciated!

Matt

1 Like

I’m glad CoolTerm helped with figuring this out :slight_smile:

Another interesting thing to note is that many micro-controller boards use DTR as means to reset the micro-controller. This includes the Arduinos (e.g. the Nano), where the DTR signal is AC-coupled to the RESET signal. You can simply reset your board by toggling DTR low and then back high. This can come in very handy when the physical reset button is not easily accessible. I believe this is also how the Arduino IDE resets the board before it uploads new firmware.

4 Likes

Thanks a lot, Roger! I have a project combining Raspi and Arduino and usually enjoy I can monitor everything online now with VNC. Except for when the Arduino needs a reset …

Hmmm, I see. Not the usual DTR/DSR use. A Beginner's Guide to the ESP8266

image