www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - communicating with a com port

reply "Miguel F. Simoes" <Kobold netcabo.pt> writes:
I have been working on a project that will use language D to create the 
graphical user interface.
The application needs to communicate with a Microchip Picdem Z Board.
The board can be accessed by COM4. How can I communicate with COM4 using D?

I have noticed that Phobos has some functions that can be related with this:
    ubyte inp(uint port_address)
    ushort inpw(uint port_address)
    uint inpl(uint port_address)

    ubyte outp(uint port_address, ubyte value)
    ushort outpw(uint port_address, ushort value)
    uint outpl(uint port_address, uint value)

How should I use them?

Greetings,
Miguel Ferreira Simoes 
Jun 01 2005
next sibling parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Wed, 1 Jun 2005 21:47:40 +0100, Miguel F. Simoes <Kobold netcabo.pt>  
wrote:
 I have been working on a project that will use language D to create the
 graphical user interface.
 The application needs to communicate with a Microchip Picdem Z Board.
 The board can be accessed by COM4. How can I communicate with COM4 using  
 D?

Same way you'd do it in C. AFAIK there is no D library for this yet. [On windows]--- CreateFile - to open port GetCommTimeouts - to get timeouts SetCommTimeouts - to set timeouts GetCommState - to get state/mode SetCommState - to set state/mode CloseHandle - to close port WaitCommEvent - to wait for input/output/etc ReadFile - to read from port WriteFile - to write to port --- [On unix]--- open - to open port tcgetattr - to get state/mode tcsetattr - to set state/mode close - to close port select - to wait for input/output read - to read from port write - to write to port --- [on FreeBSD/OS-X]--- cfsetispeed - to set input baud cfsetospeed - to set output baud ---
 I have noticed that Phobos has some functions that can be related with  
 this:
     ubyte inp(uint port_address)
     ushort inpw(uint port_address)
     uint inpl(uint port_address)

     ubyte outp(uint port_address, ubyte value)
     ushort outpw(uint port_address, ushort value)
     uint outpl(uint port_address, uint value)

 How should I use them?

I'm not sure what there do :) Regan
Jun 01 2005
next sibling parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Thu, 02 Jun 2005 09:16:29 +1200, Regan Heath <regan netwin.co.nz> wrote:
 On Wed, 1 Jun 2005 21:47:40 +0100, Miguel F. Simoes <Kobold netcabo.pt>  
 wrote:
 I have been working on a project that will use language D to create the
 graphical user interface.
 The application needs to communicate with a Microchip Picdem Z Board.
 The board can be accessed by COM4. How can I communicate with COM4  
 using D?

Same way you'd do it in C. AFAIK there is no D library for this yet. [On windows]--- CreateFile - to open port GetCommTimeouts - to get timeouts SetCommTimeouts - to set timeouts GetCommState - to get state/mode SetCommState - to set state/mode CloseHandle - to close port WaitCommEvent - to wait for input/output/etc ReadFile - to read from port WriteFile - to write to port --- [On unix]--- open - to open port tcgetattr - to get state/mode tcsetattr - to set state/mode close - to close port select - to wait for input/output read - to read from port write - to write to port --- [on FreeBSD/OS-X]--- cfsetispeed - to set input baud cfsetospeed - to set output baud ---
 I have noticed that Phobos has some functions that can be related with  
 this:
     ubyte inp(uint port_address)
     ushort inpw(uint port_address)
     uint inpl(uint port_address)

     ubyte outp(uint port_address, ubyte value)
     ushort outpw(uint port_address, ushort value)
     uint outpl(uint port_address, uint value)

 How should I use them?

I'm not sure what there do :)

EDIT: 'there' == 'these', or should I say 'there' is 'these' ;)
Jun 01 2005
parent Brad Beveridge <brad somewhere.net> writes:
Regan Heath wrote:
 On Thu, 02 Jun 2005 09:16:29 +1200, Regan Heath <regan netwin.co.nz> wrote:
 
 On Wed, 1 Jun 2005 21:47:40 +0100, Miguel F. Simoes 
 <Kobold netcabo.pt>  wrote:

 I have been working on a project that will use language D to create the
 graphical user interface.
 The application needs to communicate with a Microchip Picdem Z Board.
 The board can be accessed by COM4. How can I communicate with COM4  
 using D?

Same way you'd do it in C. AFAIK there is no D library for this yet. [On windows]--- CreateFile - to open port GetCommTimeouts - to get timeouts SetCommTimeouts - to set timeouts GetCommState - to get state/mode SetCommState - to set state/mode CloseHandle - to close port WaitCommEvent - to wait for input/output/etc ReadFile - to read from port WriteFile - to write to port --- [On unix]--- open - to open port tcgetattr - to get state/mode tcsetattr - to set state/mode close - to close port select - to wait for input/output read - to read from port write - to write to port --- [on FreeBSD/OS-X]--- cfsetispeed - to set input baud cfsetospeed - to set output baud ---
 I have noticed that Phobos has some functions that can be related 
 with  this:
     ubyte inp(uint port_address)
     ushort inpw(uint port_address)
     uint inpl(uint port_address)

     ubyte outp(uint port_address, ubyte value)
     ushort outpw(uint port_address, ushort value)
     uint outpl(uint port_address, uint value)

 How should I use them?

I'm not sure what there do :)

EDIT: 'there' == 'these', or should I say 'there' is 'these' ;)

they are pretty much x86 specific & you could use them to access IO space on the mother board. Ie, outb(0x37F) writes to the parallel port (I think - very hazy here) Brad
Jun 01 2005
prev sibling parent "Miguel F. Simoes" <Kobold netcabo.pt> writes:
Thanks a lot.
It is my first time communicating with a com port.

Miguel
Jun 01 2005
prev sibling parent reply Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Miguel F. Simoes wrote:
 I have been working on a project that will use language D to create the 
 graphical user interface.
 The application needs to communicate with a Microchip Picdem Z Board.
 The board can be accessed by COM4. How can I communicate with COM4 using D?

In the dark C++ days I used to have a *little* COM library working on Linux... Attaching its sources here, maybe you'll find some use for them :) -- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Jun 01 2005
next sibling parent "Regan Heath" <regan netwin.co.nz> writes:
On Wed, 01 Jun 2005 23:28:39 +0200, Tom S  
<h3r3tic remove.mat.uni.torun.pl> wrote:
 Miguel F. Simoes wrote:
 I have been working on a project that will use language D to create the
 graphical user interface.
 The application needs to communicate with a Microchip Picdem Z Board.
 The board can be accessed by COM4. How can I communicate with COM4  
 using D?

In the dark C++ days I used to have a *little* COM library working on Linux... Attaching its sources here, maybe you'll find some use for them :)

Cool. I'm not 100% certain, but I believe on FreeBSD and OSX you get trouble with this line: options.c_cflag = CBAUD | B9600 | CS8 | CREAD | CLOCAL; as it does not expect the baud there (IIRC it's a number not a flag on freeBSD), instead you must use cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); only. Of course, linux is fine with it either way. Regan
Jun 01 2005
prev sibling parent "Miguel F. Simoes" <Kobold netcabo.pt> writes:
Very cool.
I will have a depper look at it.

Thanks!

"Tom S" <h3r3tic remove.mat.uni.torun.pl> escreveu na mensagem 
news:d7l9a8$2j1g$1 digitaldaemon.com...
 Miguel F. Simoes wrote:
 I have been working on a project that will use language D to create the
 graphical user interface.
 The application needs to communicate with a Microchip Picdem Z Board.
 The board can be accessed by COM4. How can I communicate with COM4 using 
 D?

In the dark C++ days I used to have a *little* COM library working on Linux... Attaching its sources here, maybe you'll find some use for them :) -- Tomasz Stachowiak /+ a.k.a. h3r3tic +/

--------------------------------------------------------------------------------
 typedef unsigned char byte;


 class Comm
 {
 public:
    enum CommStatus {
        COMM_ERR  = 0,
        COMM_OK   = 1,
        COMM_NULL = 2
    };

    int fd;

    CommStatus open();
    CommStatus send(const byte *const data, const unsigned len);
    CommStatus recvbyte(byte *const data);
    CommStatus recv(byte* data, int size);

     Comm() : fd(-1) {}
    ~Comm() { if (fd != -1) close(fd); fd = -1; }
 };

--------------------------------------------------------------------------------
 #include <cstdlib>

 #include <cstdio>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <termios.h>

 #include "rs232.h"


 typedef unsigned char byte;

    Comm::CommStatus Comm::open()
    {
        fd = ::open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
        if (-1 == fd) return COMM_ERR;

        struct termios options;
        tcgetattr(fd, &options);

        tcflush(fd, TCIFLUSH);
        options.c_cflag = CBAUD | B9600 | CS8 | CREAD | CLOCAL;
        options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
        options.c_iflag &= ~(INPCK | ISTRIP);
        options.c_iflag &= ~(IXON | IXOFF | IXANY);
        options.c_oflag &= ~OPOST;

        cfsetispeed(&options, B9600);
        cfsetospeed(&options, B9600);

        tcsetattr(fd, TCSANOW, &options);

        return COMM_OK;
    }

    Comm::CommStatus Comm::send(const byte *const data, const unsigned len)
    {
        int status = ::write(fd, data, len);

        if (status < 0) return COMM_ERR;
        else return COMM_OK;
    }

    Comm::CommStatus Comm::recvbyte(byte *const data)
    {
        int status = ::read(fd, data, 1);

        if (status < 0 && errno == EAGAIN) return COMM_NULL;
        else if(status < 0) return COMM_ERR;
        return COMM_OK;
    }

    Comm::CommStatus Comm::recv(byte* data, int size)
    {
        CommStatus st = recvbyte(data);
        if (st != COMM_OK) return st;
        if (1 == size--)   return st;

        fcntl(fd, F_SETFL, 0);

        while (size-- > 0)
        {
            st = recvbyte(++data);
            if (st != COMM_OK) break;
        }

        fcntl(fd, F_SETFL, FNDELAY);
        return st;
    }
 

Jun 01 2005