Go Down

Topic: (SOLVED) Arduino + Mono + Reading from serial (the operation has timed out) (Read 2940 times) previous topic - next topic

Snotter

My Development Environment:

- Hardware: Arduino MEGA 2560/UNO (firmwares: arduino-usbserial-mega.hex/arduino-usbserial-uno.hex)
- Hardware: MACBOOK PRO 13i 2011
- Software:  MAC OSX 10.6.7
- Software:  Arduino 0022
- Software:  Monodevelop 2.4.2

Problem sample code: http://arduino.cc/playground/Interfacing/Mono

// SerialTest.cs
//
// The MIT License
//
// Copyright (c) 2008 Matias Korhonen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
using System;
using System.IO.Ports;

class MainClass
{  
  public static void Main(string[] args)
  {
     SerialPort sport = new SerialPort("/dev/tty.usbmodemfa131", 9600);

     if (sport != null)
     {
        if (sport.IsOpen)
           {
              sport.Close();
           }
     }

     Console.WriteLine("Arduino Serial Test");

     sport.Open();

     while(true)
     {      
        Console.WriteLine(sport.ReadLine());
     }
  }

}

Results in this output:

Unhandled Exception: System.TimeoutException: The operation has timed-out.
 at System.IO.Ports.SerialPortStream.Read (System.Byte[] buffer, Int32 offset, Int32 count) [0x00086] in /private/tmp/monobuild/build/BUILD/mono-2.6.7/mcs/class/System/System.IO.Ports/SerialPortStream.cs:151
 at System.IO.Ports.SerialPort.read_byte () [0x00007] in /private/tmp/monobuild/build/BUILD/mono-2.6.7/mcs/class/System/System.IO.Ports/SerialPort.cs:608
 at System.IO.Ports.SerialPort.ReadTo (System.String value) [0x00042] in /private/tmp/monobuild/build/BUILD/mono-2.6.7/mcs/class/System/System.IO.Ports/SerialPort.cs:670
 at System.IO.Ports.SerialPort.ReadLine () [0x00000] in /private/tmp/monobuild/build/BUILD/mono-2.6.7/mcs/class/System/System.IO.Ports/SerialPort.cs:653
 at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:ReadLine ()
 at ArduinoSerial001.MainClass.iThread_Daemon_Start () [0x00020] in /Users/bartjanssen/Projects/Monodevelop/Arduino-Serial-001/Arduino-Serial-001/Main.cs:68
The application was terminated by a signal: SIGHUP

Question:

What could be wrong and be the reason for the time out ?

PaulS

Quote
What could be wrong and be the reason for the time out ?

Read the data coming in on the serial port, or generate a time out exception if no data arrives within a reasonable time. Hmm, now why would a time out exception be being generated.

Code: [Select]
      SerialPort sport = new SerialPort("/dev/tty.usbmodemfa131", 9600);
This doesn't look like any port name I'm familiar with.

Snotter

FYI:

/dev/tty.usbmodemfa131 is the device name on MAC OSX , in linux it's ttyACM0/1/... , in windows it's COM3/4/...

Snotter

... adding a timeout exception results in an endless loop of time-out after time-out.

The only way i'm able to read from the serial device is by using "screen /dev/tty.usbmodemfa131" in a terminal ...

What could "screen" be doing different in handling the serial communication connection ... ?

Snotter

SOLUTION FOUND:

Apparantly when you connect an Arduino UNO/MEGA to a MAC system you get two devices loaded, in my case /dev/tty.usbmodemfd121 and /dev/cu.usbmodemfd121 ...

All the time and effort I put in above was tested with /dev/tty.usbmodemfd121 and with no luck ...
(I use /dev/tty.usbmodemfd121 as a reference in the Arduino IDE 0022 to upload and read from the serial)

Changing to /dev/cu.usbmodemfd121 in the code DOES WORK  and makes reading from the serial possible !!!

I couldn't find anything about this in the manuals , it might be worth adding imo.

Woot!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy