- Working with COM Ports in C
- 07 May 2011 07:32:31 pm
- Last edited by _player1537 on 07 May 2011 09:25:45 pm; edited 1 time in total
I am wanting to write a C program to work with my eZ430 Chronos watch, which uses a COM port. I got some help the other night from Jonimus, Tari, and (sorry) my brain forgets who else. Anyways, I remember Kerm saying that he already had code that did most of this (working with them in general), so I figured I would post here so he would post it For what it's worth, here is the code that I was able to get, but it doesn't work right now.
Code:
Most of the data was taken from various websites, I'll try to find them all:
http://chemicaloliver.net/arduino/graphing-ti-ez430-chronos-watch-data-in-linux/ (Copy paste)
http://pastebin.com/fde255fd (from 1)
http://www.easysw.com/~mike/serial/serial.html (Which I switched from, but most of the code was taken from examples there)
Edit: chemicaLOLiver got caught in the filter
Code:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
FILE* OpenPort( void );
int main(int argc, char* argv[]) {
char startAccessPoint[] = { 'B', 0xFF, 0x07, 0x03 };
char accDataRequest[] = { 'B', 0xFF, 0x08, 0x07, 0x00, 0x00, 0x00, 0x00 };
char recvBuf[10];
int length;
struct termios options;
FILE* ser = OpenPort();
int fd = fileno(ser);
printf("Port opened\n");
fwrite(startAccessPoint, 1, 4, ser); // Start access point
printf("Access point started\n");
// Set options
tcgetattr(fd, &options);
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
printf("Settings... set\n");
while( 1 ) {
printf("in while(1) loop\n");
fwrite(accDataRequest, 1, 8, ser); // Request data
length = fread(recvBuf, 1, 7, ser);
printf("x:%d y:%d z:%d\n",recvBuf[2], recvBuf[1], recvBuf[0]); // x:128 y:64 z:200
printf("\tdatatype:%d\n",recvBuf[3]); // datatype:2
}
fclose(ser);
return 0;
}
FILE* OpenPort( void ) {
FILE* fp; // File descriptor
fp = fopen("/dev/ttyACM0", "r+b");
if( fp < 0 ) {
perror("Could not open port");
} else {
//fcntl(fp, F_SETFL, 0);
}
return fp;
}
http://chemicaloliver.net/arduino/graphing-ti-ez430-chronos-watch-data-in-linux/ (Copy paste)
http://pastebin.com/fde255fd (from 1)
http://www.easysw.com/~mike/serial/serial.html (Which I switched from, but most of the code was taken from examples there)
Edit: chemicaLOLiver got caught in the filter