Unable to read from Serial Port using C# Mono (RaspberryPi) -


i'm attempting write c# library looks @ available usb serial ports on raspberry pi can enumerate, identify , communicate set of arduinos connected pi via usb hub.

i able make work on windows machine (several arduinos connected desktop computer) , have been able make work on pi however, struggling understand how generalize fix.

if attempt run program on pi, able open serial port , send data however, cannot receive arduinos: timeout exceptions. understand mono's implementation of serialport limited , must use serialport.readbyte() instead of readline() , data received events (my solution based on code howtosystemioports). serial port enumeration using method outlined in stack exchange response here.

my timeout set 4 seconds, several orders of magnitude longer expect receive message.

after lot of googling, came across mention of using minicom initialize serial port here, surprise allowed me receive data arduino. biggest drawback need initialize port using minicom , leave process opening each time boot pi. can't seem figure out how make work multiple arduinos.


here have tried far:

  • updated pi firmware , software latest versions
  • attempted use both arduino mega 2560 r3 , arduino uno
  • changed owner of tty* ports (ttyacm0 , ttyusb0 in case) both user , group
  • successfully configured port via minicom, left process running , start program , read/wrote data. manual process seems work 1 arduino @ time
  • successfully run program in windows without fault
  • verified arduinos recognized pi running "dmesg | grep tty"

here hope solve:

  • automatic setup/initialization of arduino serial ports. whether through shell script run before main program or within mono code code below can run intended.

here connection code:

    public bool startarduinocomms()     {         string[] ports = getportnames();         foreach (string port in ports)         {             mlogger.logmessage(prosthesiscore.utility.logger.loggerchannels.arduino, string.format("found serial port {0}", port));         }          bool foundcorrectarduino = false;          var idpacket = new arduinomessagebase();         idpacket.id = arduinomessagevalues.kidentifyvalue;          string jsonoutput = newtonsoft.json.jsonconvert.serializeobject(idpacket);          foreach (string port in ports)         {             serialport serialport = new serialport(port, karduinocommsbaudrate);             serialport.parity = parity.none;             serialport.databits = 8;             serialport.stopbits = stopbits.one;              //only check unopened ports             if (!serialport.isopen)             {                 serialport.open();                  //disable telemtry incase                 var toggle = new { id = arduinomessagevalues.ktelemetryenablevalue, en = false };                 string disabletelem = newtonsoft.json.jsonconvert.serializeobject(toggle);                 serialport.write(disabletelem);                  //discard built data                 serialport.discardinbuffer();                 serialport.write(jsonoutput);                 serialport.readtimeout = kidtimeoutmilliseconds;                  string response = string.empty;                 (int = 0; < knumretries; ++i)                 {                     try                     {                         //this guaranteed timeout if not configured through minicom                         response = readline(serialport);                         break;                     }                     //catch case serial port unavailable. move next port                     catch (timeoutexception)                     {                         continue;                     }                 }                  if (!string.isnullorempty(response))                 {                     //perform response validation                 }                 else                 {                     //got no response                 }                  if (!foundcorrectarduino)                 {                     serialport.close();                 }             }         }          return foundcorrectarduino;     }     /// <summary>     /// https://stackoverflow.com/questions/434494/serial-port-rs232-in-mono-for-multiple-platforms     /// </summary>     /// <returns></returns>     private static string[] getportnames()     {         int p = (int)environment.osversion.platform;         list<string> serial_ports = new list<string>();          // on unix?         if (p == 4 || p == 128 || p == 6)         {             string[] ttys = system.io.directory.getfiles("/dev/", "tty*");             foreach (string dev in ttys)             {                 //arduino megas show ttyacm due different usb<->rs232 chips                 if (dev.startswith("/dev/ttys") || dev.startswith("/dev/ttyusb") || dev.startswith("/dev/ttyacm"))                 {                     serial_ports.add(dev);                 }             }         }         else         {             serial_ports.addrange(serialport.getportnames());         }          return serial_ports.toarray();     } 

have @ stty command. let set/read teminal settings

http://linux.about.com/od/lna_guide/a/gdelna38t01.htm give rundown on it's use. easier call out minicom, , settings stay on device.


Comments