6

for testing purpose I'm trying to write a simple program that connect to a virtual serial port created with socat.

I create the serial port with this command:

socat -d -d PTY,b9600 PTY,link=ttyVS1,b9600

getting this output:

2011/11/08 18:26:31 socat[32708] N PTY is /dev/pts/1
2011/11/08 18:26:31 socat[32708] N PTY is /dev/pts/2
2011/11/08 18:26:31 socat[32708] N starting data transfer loop with FDs [3,3] and [5,5]

When I try to connect in this way:

System.IO.Ports.SerialPort _port;
_port = new SerialPort("/dev/pts/1", 9600);
_port.Open();

I get a "filename unknown" System.IO.IOException.

Am I missing to set DataBits, StopBits or other parameters? How can I discover the properties sett by socat? Or what is wrong with this code?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
gsscoder
  • 3,088
  • 4
  • 33
  • 49

1 Answers1

1

What is wrong here is for sure the name of the device. Can you check at /dev that /dev/pts/1 exists? Maybe it has another name like /dev/pts1?

Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207
  • I will check and I'll post here... For now I can say that I've done another test with Windows using com0com (Virtual Serial Port software). I was able to run two instance of the same software (connecting without baudrate, just the port) and exchange data! I want to do the same with Ubuntu Linux (and aslo with Mac OS X)... – gsscoder Nov 08 '11 at 18:53
  • On linux the problemi is that, also if both /dev/pts/1 (and 2) exists, when I call Serial.GetPortNames() I can't see /dev/pts/* listed. Probably the Mono runtime acts like .NET runtime being not able to connect to port not named COM* (for linux could be /dev/tty*, I don't know!). Sounds correct? (Can I symlink resolve the problem?) – gsscoder Nov 08 '11 at 19:01
  • What do you have when calling GetPortNames()? – Ignacio Soler Garcia Nov 08 '11 at 21:11
  • /dev/ttyS0 /dev/ttyS1 /dev/ttyS10 /dev/ttyS11 /dev/ttyS12 /dev/ttyS13 /dev/ttyS14 /dev/ttyS15 /dev/ttyS16 /dev/ttyS17 /dev/ttyS18 /dev/ttyS19 /dev/ttyS2 /dev/ttyS20 /dev/ttyS21 /dev/ttyS22 /dev/ttyS23 /dev/ttyS24 /dev/ttyS25 /dev/ttyS26 /dev/ttyS27 /dev/ttyS28 /dev/ttyS29 /dev/ttyS3 /dev/ttyS30 /dev/ttyS31 /dev/ttyS4 /dev/ttyS5 /dev/ttyS6 /dev/ttyS7 /dev/ttyS8 /dev/ttyS9 – gsscoder Nov 09 '11 at 17:21
  • 1
    Ok, so we have 2 things to check. Or the Mono implementation of the serial port only support ports starting with /dev/ttySx or there is a bug at the method (mmm ... I don't know how the code would be able to know that /dev/pts/1 is a serial port anyway ...). Can you change the port that socat creates to a tty name? or link? or something like that? You get the idea ... I'm not a Linux expert ... – Ignacio Soler Garcia Nov 09 '11 at 18:11
  • I've tried with a symbolink link (sudo ln -s /dev/pts/1 /dev/ttyS20 after renameing /dev/ttyS20.old) for both devices and I get the same exception. Any other idea? Thanks again. – gsscoder Nov 10 '11 at 06:20
  • Why not... I'll post here results! Thank you for your time. – gsscoder Nov 10 '11 at 17:33
  • Upvoted! I'm diving into mono/mcs/class/System/System.IO.Ports source directory. SerialPort.Open (on SerialPort.cs) consumes an internal class called SerialPortStream. This last type use an extern call defined as:[DllImport ("MonoPosixHelper", SetLastError = true)] static extern int open_serial (string portName);. Now we should go deep on this API! – gsscoder Nov 11 '11 at 06:51
  • Put your neoprene swimming suit ;) Good luck! – Ignacio Soler Garcia Nov 11 '11 at 08:57
  • Finally surfaced! The code we care lives in this place of the source tree: **mono/support/serial.c**. The `open_serial(char*)` is defined here and performs a simple call to a system **open()**, like this: `open (devfile, O_RDWR | O_NOCTTY | O_NONBLOCK)`. open_serial() doesn't check the format of devfile parameter. Maybe that the **O_NOCTTY** flag enforce a device file and **/dev/pts/1** _created by socat_ is not recognized as device file. So the problem comes back to **socat**: how to run socat in a way that will allow the creation of valid device file? – gsscoder Nov 12 '11 at 08:53
  • I've started a small lib with inside some code for access serial data, [https://github.com/gsscoder/libfoundation](https://github.com/gsscoder/libfoundation). Serial mock device included, for only for Windows (using **com0com**). – gsscoder Nov 14 '11 at 19:38
  • Did you ever find a fix for this? I'm running into it as well. – RobotCaleb Apr 04 '12 at 05:56