Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
56 elmo 1
/* First version by Bjoern Biesenbach <bjoern@bjoern-b.de> */
2
#include <string.h>
3
#include <unistd.h>
4
#include <fcntl.h>
5
#include <errno.h>
6
#include <termios.h>
7
#include <stdlib.h>
8
 
9
#include "serial.h"
10
#include "main.h"
11
 
12
static int fd; // File descriptor for serial port
13
 
14
int open_port(char *port)
15
{
16
        int fds; /* File descriptor for the port */
17
 
18
        fds = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
19
        if (fds == -1)
20
        {
21
                /*
22
                * Could not open the port.
23
                */
24
                perror("open_port: Unable to open serial port!");
25
                exit(1);
26
        }
27
        else
28
        fcntl(fds, F_SETFL, 0);
29
 
30
        return (fds);
31
}
32
void AddCRC(unsigned int wieviele)
33
{
34
 unsigned int tmpCRC = 0,i;
35
 for(i = 0; i < wieviele;i++)
36
  {
37
   tmpCRC += TxBuffer[i];
38
  }
39
   tmpCRC %= 4096;
40
   TxBuffer[i++] = '=' + tmpCRC / 64;
41
   TxBuffer[i++] = '=' + tmpCRC % 64;
42
   TxBuffer[i++] = '\r';
43
}
44
 
45
 
46
 
47
void sende()
48
{
49
        unsigned char tmp_tx;
50
        int i=0;
51
        while(TxBuffer[i] !='\r' && i<150)
52
        {
53
                write(fd,&TxBuffer[i],1);
54
                i++;
55
        }
56
        write(fd,"\r",1);
57
}
58
 
59
void SendOutData(unsigned char cmd,unsigned char modul, unsigned char *buffer, int len)
60
{
61
 unsigned int pt = 0,i;
62
 
63
 TxBuffer[pt++] = '#';           // Startzeichen
64
 TxBuffer[pt++] = modul+'a';     // Adresse (a=0; b=1,...)
65
 TxBuffer[pt++] = cmd;           // Commando
66
 if(len)
67
 for(i = 0;i < len; i+=3)
68
 {
69
    TxBuffer[pt++] = '=' + (buffer[i] >> 2);
70
    TxBuffer[pt++] = '=' + (((buffer[i] & 0x03) << 4) | ((buffer[i+1] & 0xf0) >> 4));
71
    TxBuffer[pt++] = '=' + ((unsigned char) ((buffer[i+1] & 0x0f) << 2) | ((buffer[i+2] & 0xc0) >> 6));
72
    TxBuffer[pt++] = '=' + ((unsigned char) buffer[i+2] & 0x3f);
73
  }
74
 AddCRC(pt);
75
 sende();
76
}
77
 
78
void initSerial(char *port)
79
{
80
        struct termios options;
81
        fd=open_port(port);
82
        tcgetattr(fd, &options);
83
        cfsetispeed(&options, B57600);
84
        cfsetospeed(&options, B57600);
85
        options.c_cflag |= (CLOCAL | CREAD);
86
        tcsetattr(fd, TCSANOW, &options);
87
        fcntl(fd, F_SETFL, FNDELAY);
88
}