Moxa 7112 Programming

This is a discussion on Moxa 7112 Programming within the Networking/Device Communication forums, part of the General Programming Boards category; Hey guys, just wondering if anyone here has any experience programming Moxa devices? Basically I have a Debian box which ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    12

    Moxa 7112 Programming

    Hey guys, just wondering if anyone here has any experience programming Moxa devices? Basically I have a Debian box which runs my little serial program fine (all it does is ouput to a serial port) however when I tried it on the Moxa I can't get it to work at all! It compiles fine and I'm sure I'm using the correct ports.

    Here's a snipplet of the code:

    Code:
    portno = 0x98200000;      // Use ttyS0
    ioperm(portno, 1, 1);         // Allow read and write access
    
    // Main program
    
    for (i=0; i<outdatalen; i++)           // oudatalen is the length of the outgoing data string
    {
         outb(outdata[i], portno);
         usleep(waittime);
    }
    
    // Rest of the program
    Having looked around I have found several ways of doing this, however this way just seemed the simplest! I tested it by outputting to a Windows laptop (Hyperterminal) and it works perfectly fine. I just can't get the ports on the Moxa to do anything at all!

    Thanks for any help,

    pgcrooks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > portno = 0x98200000; // Use ttyS0
    > ioperm(portno, 1, 1); // Allow read and write access
    How about using open / read / write / close / ioctl ?
    In other words, the standard API for accessing devices.

    You're making vast assumptions that your h/w architecture resembles an x86 PC.

    Forget anything you ever read about programming ports if you learnt it from looking at DOS code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    Salem that may explain it... Well I have now rewritten it using this guide which as far as I can tell should work on any POSIX architecture but still have no luck. Now the little moxa just outputs to the screen (through Telnet) and the serial ports are still not doing anything. If you are insanely bored, I have attached the source for the program. The interesting bits are at the start and in the "Recombine Data String" sections. It's probably horribly coded but I'm pretty new to programming, and it's my first attempt at anything serial related!

    Thanks
    Attached Files Attached Files

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    How come your 5 lines grew to a 9KB file?

    Create a simple test program which just outputs a short message, and get that working first.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    Ok, sorry was rushing to get out of work so just put the entire program on here! Here's a shortened version with just the output code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <asm/io.h>
    #include <moxadevice.h>
    #include <termios.h>
    #include <fcntl.h>
    #include <errno.h>
    
    int n, fd;
    
    int open_port(fd)
    {
    	fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
    	if (fd < 0)
    	{
    		perror("open_port: /dev/ttyS0 - ");
    	}
    	else
    	{
    		fcntl(fd, F_SETFL, 0);
    	}
    	return (fd);
    }
    
    
    int main(int argc, char* argv[])
    
    {
            open_port();
    	n = write(fd, "test", 4);	     // Output the data to fd for the length of the string
    	if (n < 0)
    	{
    		printf("Write failed!");
    	}	
    	return(0);
    }
    Ok I think that's it... It compiles ok but I don't get output or any error messages. I'm not sure if I need to set the options for the port, if I just leave it like I did will it just output as default? eg 19200kbs, no flow control...

    Thanks
    Last edited by pgcrooks; 09-19-2008 at 03:42 AM.

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    Ok I think I messed up the function call in that, so I messed around and have tried it this way, also with no luck. It outputs "35", which I guess means it opened the port ok and sent 5 characters to the port?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <asm/io.h>
    #include <moxadevice.h>
    #include <termios.h>
    #include <fcntl.h>
    #include <errno.h>
    
    int n, fd;
    char jazz[5];
    
    int main()
    {
    	fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
    	printf("&#37;d", fd);
    	if (fd < 0)
    	{
    		perror("open_port: /dev/ttyS0 - ");
    	}
    	else
    	{
    		fcntl(fd, F_SETFL, 0);
    	}
    	strcpy(jazz, "test");
    	n = write(fd, &jazz, sizeof(jazz));
    	printf("%d", n);
    	if (n < 0)
    	{
    		printf("Write failed!");
    	}	
    	return(0);
    }
    Thanks
    abachler on Linux
    ...obtusely written by a bunch of drunken retarded gorillas on crack.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > n = write(fd, &jazz, sizeof(jazz));
    Drop the &

    strlen would have been better, then you don't send a \0 (maybe that doesn't matter).

    > It outputs "35", which I guess means it opened the port ok and sent 5 characters to the port?
    Yep, sounds good.

    > I'm not sure if I need to set the options for the port, if I just leave it like I did will it just output as default? eg 19200kbs, no flow control...
    I've no idea what the default port config on your machine is.
    Try an ioctl() call to get the current config, and print it out.

    It's definitely worth messing with the port config at the other end (different baud rates etc) to see if anything at all is being sent.

    Another thing, you could close the port explicitly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21