6

I need to perform a simple exchange via serial port in my program. I cannot find any working examples or documentation related to serial ports. I need to open a serial port, configure a port (set speed, parity, stopbits, etc), write/read binary data and then close is.

I tried to use https://github.com/japaric/serial.rs, but this library is outdated (it doesn't even compile on Rust 1.0). Even then, this library only provided functionally on how to configure the serial port but not to use it.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
fmvin
  • 591
  • 2
  • 5
  • 9
  • You're going to have to either use the C functions directly via the FFI, or use a library someone else has written, because this is not in the standard library. Is the problem with that library only that it doesn't compile anymore? If so, you could probably update it to compile with your version of Rust. (NB. that library has [a public field](https://github.com/japaric/serial.rs/blob/master/src/serial.rs#L24) containing an (Option of a) [`FileDesc`](http://doc.rust-lang.org/master/native/io/file/struct.FileDesc.html), which can be used to read/write via the traits mentioned on that page.) – huon Aug 31 '14 at 05:49

4 Answers4

6

On UNIX serial port is represented by a character device, whcih can be accessed via ordinary system calls that are used for file I/O. The only addition that you would care about with respect to serial port is ioctl, that's what you will use to set baud rate and other parameters.

errordeveloper
  • 6,716
  • 6
  • 41
  • 54
5

There are 2 solutions depending of OS where code is built. For *nix OS serial.rs library should work fine for rust 0.11.0 build but for supporting rust-0.12.0 an issue was opened and not closed yet.

For Windows stuff (mingw-w64) the serial.rs is not a simple solution because this lib is based on termios calls which are not easily setup for mingw. It comes from point that mingw is built against msvcrt not against glibc (for more information see here). On Windows a simple solution would be to write a wrapper for library like rs232 by teuniz using rust FFI.

  1. Build library rs232 using mingw gcc;

  2. Create a wrapper in rust;

Short example for Windows looks like this:

extern crate libc;
use libc::{c_int,c_uchar,c_uint};
use std::os;
//
#[link(name = "rs232")]
extern {    
    fn RS232_OpenComport(comport_number:c_int, baudrate:c_int) ->c_int; 
    fn RS232_SendByte(comport_number:c_int, byte:c_uchar)->c_int;
    fn RS232_CloseComport(comport_number:c_int);
}
static  COM10:c_int=9;    
fn main() {
    let y=unsafe{RS232_OpenComport(COM10, 115200)};    
    unsafe{
        RS232_SendByte(COM10,101);  
        RS232_SendByte(COM10,100);
    }    
    let cl=unsafe{RS232_CloseComport(COM10)};
}
Community
  • 1
  • 1
fmvin
  • 591
  • 2
  • 5
  • 9
3

If you need the ability to list serial devices (not only connect to them), then you need serialport, which is based on serial.

If you want to use serial, you should know that it was divided into small crates and you should use one or the other depending if you are developing a library or an executable. Check the readme and documentation for extra information.

Both crates are multi-platform and have recent updates, so it's a matter of deciding which API looks better to you.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Emilio
  • 2,526
  • 22
  • 26
0

The serial package works fine.

Sanpi
  • 420
  • 7
  • 18