|
Hi I won't bother repeating what Charles and Michael already said, usleep, read, write etc, all userspace. Attached is a conversion of your comp to a userspace format. comp quite happily builds userspace components from C. It builds, but obviously I cannot test it. The initial usleep in the loop may not be required, it can be dispensed with or adjusted as required regards On 01/07/16 15:27, matthew venn wrote:
-- website: http://www.machinekit.io blog: http://blog.machinekit.io github: https://github.com/machinekit --- You received this message because you are subscribed to the Google Groups "Machinekit" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. Visit this group at https://groups.google.com/group/machinekit. For more options, visit https://groups.google.com/d/optout. |
component xbee "xbee serial"; pin in float pos_; pin out float batt_; pin in float scale = 20.0;
function _;
option singleton yes; // makes no sense to have more than one of
these components running
option userspace yes;
license "GPL"; // indicates GPL v2 or later
;;
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <fcntl.h>
int fd;
struct {
char pos;
char cksum;
} tx;
struct {
unsigned short batt;
unsigned char cksum;
} rx;
#define RX_SIZE 3 //computed as 4 but on the atmel it's 3
int serialport_init();
char CRC8(char *data, char len);
void user_mainloop(void)
{
char buffer[50];
char ch;
int ret = serialport_init();
while(ret)
{
usleep(100000);
FOR_ALL_INSTS()
{
tx.pos = pos_ * scale;
char buf[sizeof(tx)];
memcpy(&buf, &tx, sizeof(tx));
tx.cksum = CRC8(buf,sizeof(tx)-1);
memcpy(&buf, &tx, sizeof(tx));
write (fd, buf, sizeof(tx));
usleep (35 * 1000); // sleep enough time for the chars to get sent
char rx_buf[RX_SIZE];
int n = read (fd, rx_buf, RX_SIZE);
//copy buffer to structure
memcpy(&rx, &rx_buf, RX_SIZE);
if(rx.cksum != CRC8(rx_buf,RX_SIZE-1))
batt_ = -1;
else
batt_ = rx.batt;
}
}
close(fd);
exit(0);
}
//######################################################################
int serialport_init()
{
if ((fd = open("/dev/ttyO1", O_RDWR | O_NONBLOCK))<0)
{
printf("UART: Failed to open the file.\n");
return 0;
}
struct termios options; // the termios structure is vital
tcgetattr(fd, &options); // sets the parameters associated with file
// Set up the communications options:
// 57600 baud, 8-bit, enable receiver, no modem control lines
options.c_cflag = B57600 | CS8 | CREAD | CLOCAL;
tcflush(fd, TCIFLUSH); // discard file information not transmitted
tcsetattr(fd, TCSANOW, &options); // changes occur immmediately
return 1;
}
char CRC8(char *data, char len)
{
char crc = 0x00;
while (len--)
{
char extract = *data++;
char tempI;
for (tempI = 8; tempI; tempI--)
{
char sum = (crc ^ extract) & 0x01;
crc >>= 1;
if(sum)
{
crc ^= 0x8C;
}
extract >>= 1;
}
}
return crc;
}
