On May 23, 9:56 pm, Giles Lean <
giles.l...@pobox.com> wrote:
> > Hi all, I've begun work on a project that handles
> > communication with an industrial computer via a com port and
> > then provides certain network services to communicate with
> > the device.
>
> That takes me back ... *shudder*.
I'm doing almost exactly this same application right now.
>
> > The first step is to get serial communications up and
> > running with the device, and I'm wrestling with my
> > unfamiliarity with how Unix systems deal with serial port
> > configuration and also with how to implement this with Go.
>
> As well as running stty (crude, but if it works, go for it,
> and the previous response had some good advice about adding
> 'parenb' to your list of parameters. (Mostly, for varying
> values of "mostly", serial comms have used 8 bits, no parity,
> and one stop bit, but I'm sure you know what your industrial
> hardware wants.)
>
Try GNU screen or socat if you want also. Either of those can do
serial handling, and socat is ridiculously flexible. I've tunneled
UDP via TCP over ssh tunnels before to make SNMP work through a
firewall over really long distances with socat :-).
> A second (and more difficult, but in the end possibly cleaner)
> approach would be via cgo and using tcgetattr() and
> tcsetattr().
I don't think this is cleaner than asking another small purposeful
program to do the work for you.
For one you're doing something someone already made perfectly good
tool to do (socat and screen). Chances are, this software is used a
lot more than what you're about to write, and may have fewer defects.
Just ask yourself, "Do I want to be solving the problem of serial
communications that might already be solved for me, or should I just
get to work on the meat of my problems?"
If where you work is anything like where I work, the time to solution
is more important than doing it all yourself.
>
> Myself, having done (a long long time ago) comms programming
> in C would write C code that takes a file descriptor and
> twiddles it to the state you want, and then call that function
> from Go. (And an equivalent reverse operation to put things
> back when you're done, if that matters.)
>
> You could also call tcgetattr() and tcsetattr() via cgo, but I
> think doing as much in C as possible would be easier. cgo is
> (frankly, and no offence to those who've worked on it)
> immature and I hit its limits most times I try to use it.
> It's handling of complex #include files is particularly weak
> (which isn't a surprise: that's the hardest part of what it's
> doing). I tend to declare only what I want to use, and not
> include the system include files where I can avoid it.
>
> Oh -- I'd also use devicePath in the stty command, not hard
> code it. ;-)
>
> I suppose a third solution would be to talk to the device in
> C, and have Go talk to the C program via a pipe or socket.
This is closer to what I was thinking too, but those programs already
exist :-). Go look at socat.
http://www.dest-unreach.org/socat/. The
whole point of socat is solving plumbing problems, and you have a
plumbing problem :-)
You might find that, for some reason, this is too slow, but I suspect
you're not at the point where optimization is needed, especially with
serial communications involved.
Dave
>
> Good luck,
>
> Giles