Apologies JR1050
I tried to split your last post into a new thread and succeeded but when I tried to remove it from the original thread it deleted both!
You asked if an Arduino Mega 2560 was able to be used as an I/O board for Linuxcnc
OK I have written you an arduino sketch and a hal component that will send and receive respectively, the values at 32 pins on the arduino mega.
The arduino pins used are 14-49, which I think are all digital and I set them to input
You will need to download AVR and Arduino 1.0.1, possibly install the -dev package for linuxcnc and learn how to use comp to create the component ( comp --install compname).
I could send you a compiled component, but that is the easy bit. I can't program your arduino board for you, so there is no shortcut to going to the arduino site and reading.
I have just tested it printing to stderr, you will need to connect the arduino pins to something to test them and connect the component pins bit-0 to 31 to something in hal to do anything.
Comment out the 3 test lines when using for real.
You just get mostly 0 but some 1's in the test print, but the pins are in an unknown floating state unconnected.
//arduinosend.ino
#include <SoftwareSerial.h>
#include "Wire.h"
void setup()
{
Serial.begin(9600);
for(int x = 14; x < 50; x++)pinMode(x, INPUT);
}
void loop()
{
int x, y, z;
uint8_t buff[5];
buff[4] = '\0';
z = 14;
for(x = 0; x < 4; x++) // 4 packets 8 bits
for(y = 0; y < 8 ; y++) bitWrite(buff[x], y, digitalRead(z++));
Serial.write(buff, 4);
delay(1000);
}//serialreceive.comp
component serialreceive "Receiving 32 bits data from arduino";
pin out bit bit-##[32] = false "Output bits";
option singleton yes;
option userspace yes;
author "ArcEye arceye@mgware.co.uk";
license "GPL";
;;
#include <stdio.h> /* Standard input/output definitions */
#include <stdlib.h>
#include <stdint.h> /* Standard types */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <sys/ioctl.h>
#define BAUDRATE B9600
#define DEVICE "/dev/ttyACM0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
int serialport_init();
struct termios toptions;; // port setup
void user_mainloop(void)
{
int c, x, y, z;
char ch;
int fd = serialport_init();
while (fd != -1)
{
usleep(1000000);
FOR_ALL_INSTS()
{
z = 0;
for(x = 0; x < 4; x++) // 4 packets 8 bits
{
read(fd,&ch,1);
for(y = 0; y < 8 ; y++) bit(z++) = bitRead(ch, y);
}
// test - to show that bits were written
// comment out next 3 lines when using for real
for(x = 0; x < 32; x++)
fprintf(stderr,"%d",bit(x));
fprintf(stderr,"\n");
}
}
close(fd);
exit(0);
}
//######################################################################
int serialport_init()
{
int fd;
fd = open(DEVICE, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
perror("init_serialport: Unable to open port ");
return -1;
}
if (tcgetattr(fd, &toptions) < 0)
{
perror("init_serialport: Couldn't get term attributes");
return -1;
}
speed_t brate = BAUDRATE;
cfsetispeed(&toptions, brate);
cfsetospeed(&toptions, brate);
// 8N1
toptions.c_cflag &= ~PARENB;
toptions.c_cflag &= ~CSTOPB;
toptions.c_cflag &= ~CSIZE;
toptions.c_cflag |= CS8;
// no flow control
toptions.c_cflag &= ~CRTSCTS;
toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
toptions.c_oflag &= ~OPOST; // make raw
toptions.c_cc[VMIN] = 0;
toptions.c_cc[VTIME] = 20;
if( tcsetattr(fd, TCSANOW, &toptions) < 0)
{
perror("init_serialport: Couldn't set term attributes");
return -1;
}
return fd;
}
regards