Greetings fellow Linux user!
Here are two test programs that work (for me anyway, 2.6.32-21-generic #32-Ubuntu SMP Fri Apr 16 08:10:02 UTC 2010 i686 GNU/Linux)
The first one is a Python script which uses pyserial.
The second one is a C translation of the Python script.
(The C program is five times longer...)
See also quarqd/src/main.c, posted in the ANT+ achievers forum.
EDIT: This forum is a nightmare. Did the file attach this time? I will paste them in as text...
Regards,
Mark
.(JavaScript must be enabled to view this email address)
markrages@gmail
[file name=test_ant.gz size=1333]
http://www.thisisant.com/images/fbfiles/files/test_ant.gz[/file]
test_ant.py:
#!/usr/bin/python
import serial, sys, time
sp=serial.Serial(sys.argv[1], baudrate=57600)
sp.setDTR(0); # remove from reset
time.sleep(0.5); # AP1 modules need time after reset before they hear serial
# AP2 modules have a startup message, wait for that instead
def send_message(msg):
message=[0xa4, len(msg)-1] + msg
message.append(reduce(lambda x,y: x^y, message)) # checksum
sp.write(''.join([chr(c) for c in message]))
ANT_Request_Message = 0x4d
ANT_Capabilities = 0x54
send_message([ANT_Request_Message, 0, ANT_Capabilities])
sp.setTimeout(1)
print " ".join(["%02x"%ord(c) for c in sp.read(1000)])
test_ant.c:
#include <stdio.h>
#include <termios.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
int ant_fd;
int setdtr (int on)
{
int controlbits = TIOCM_DTR;
if(on)
return(ioctl(ant_fd, TIOCMBIS, &controlbits));
else
return(ioctl(ant_fd, TIOCMBIC, &controlbits));
}
void init_tty(char *dev) {
struct termios tty_conf;
ant_fd = open(dev, O_RDWR | O_NOCTTY );
if(ant_fd < 0) {
fprintf(stderr,"Error opening %s \\n", dev);
perror(dev);
exit(ant_fd);
}
tcgetattr(ant_fd, &tty_conf);
cfsetspeed(&tty_conf, B57600);
cfmakeraw(&tty_conf);
// Set the new options for the port...
tcsetattr(ant_fd, TCSANOW, &tty_conf);
setdtr(0);
}
char readchar( void ) {
char c;
read(ant_fd, &c, 1);
return c;
}
char chksum=0;
void writechar(char c) {
write(ant_fd, &c, 1);
chksum ^= c;
}
void sendchk( void ) {
writechar(chksum);
// now chksum is zero
}
void send_message(char *msg, int len) {
int i;
writechar(0xa4);
writechar(len);
for (i=0; i<len+1; i++) writechar(msg[i]);
sendchk();
}
void receive_message(char * buffer, int *len) {
int i;
do {
*buffer=readchar();
} while ((char)0xa4 != *buffer);
*(++buffer)=readchar(); // len
*len=1+1+1+*buffer+1; // sync,id,len,data,chksum
for (i=1+*buffer; i>0; i--) {
*(++buffer)=readchar(); // id,data
}
*(++buffer)=readchar(); // chksum
}
int main(int argc, char *argv[]) {
char buffer[100];
char *bp;
int len;
if (argc<2) {
fprintf(stderr, "Use like this: %s /dev/ttyUSBx\\n", argv[0]);
exit(-1);
}
init_tty(argv[1]);
sleep(1);
send_message("\\x4d\\x00\\x54", 2);
receive_message(buffer, &len);
bp=buffer;
while (--len) {
printf("%02X ",(unsigned char)*(bp++));
}
printf("\\n");
return 0;
}