File: io_win32.c

package info (click to toggle)
avrp 1.0beta3-6
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 204 kB
  • ctags: 194
  • sloc: ansic: 2,463; makefile: 40; perl: 6; sh: 5
file content (143 lines) | stat: -rwxr-xr-x 3,832 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/***********************************************************************
 *  avrp - Atmel AVR programming software to use with Atmel's
 *         serial-port programmers.
 *  Copyright (C) 1997-1998 Jon Anders Haugum
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; see the file COPYING.  If not, write to
 *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 *
 *
 *  Author of avrp can be reached at:
 *     email: jonah@colargol.tihlde.hist.no
 *     www: http://www.colargol.tihlde.hist.no/~jonah/el/avrp.html
 *     Postal address: Jon Anders Haugum
 *                     �vre M�llenbergsgt 52
 *                     7014 Trondheim
 */

/*
 * Serial port routines using win32 API (Windows 95/NT)
 */

#include <stdio.h>
#include <windows.h>

#include "avrp.h"

/*
 * Structure private to serial port functions
 */

struct Ser
   {
   char *Name;           // Name of serial port
   HANDLE Comm;          // Filehandle for serial port
   int comm_open;        // Set to True when port is open
   COMMTIMEOUTS cto_old; // Storage for old timeout values
   };

/*
 * Open the serial port
 */

void *OpenSer(char *SerPort)
   {
   struct Ser *Ser = NULL;
   DCB dcb;
   COMMTIMEOUTS cto_new = {0};

   if(Ser = (struct Ser *)malloc(sizeof(struct Ser)))
      {
      Ser->Name = SerPort;
      Ser->comm_open = False;
      Ser->Comm = CreateFile(SerPort,  
                             GENERIC_READ | GENERIC_WRITE, 
                             0, 
                             0, 
                             OPEN_EXISTING,
                             0,
                             0);
      if(Ser->Comm != INVALID_HANDLE_VALUE)
         {
         Ser->comm_open = True;
         GetCommTimeouts(Ser->Comm, &Ser->cto_old);
         cto_new.ReadTotalTimeoutConstant = 300; // milliseconds
         SetCommTimeouts(Ser->Comm, &cto_new);
         FillMemory(&dcb, sizeof(dcb), 0);
         dcb.DCBlength = sizeof(dcb);
         if(BuildCommDCB("19200,n,8,1", &dcb))
            {
            if(SetCommState(Ser->Comm, &dcb))
               return(Ser);
            else
               printf("Error: SetCommState() failed\n");
            }
         else
            printf("Error: BuildCommDCB() failed\n");
         }
      else
         printf("Error: %s: No such port\n", SerPort);
      CloseSer(Ser);
      }
   else
      printf("Error: Unable to allocate memory\n");
   return(NULL);
   }

/*
 * Close the serial port
 */

void CloseSer(void *Ser)
   {
   if(((struct Ser *)Ser)->comm_open)
      {
      SetCommTimeouts(((struct Ser *)Ser)->Comm, &((struct Ser *)Ser)->cto_old);
      CloseHandle(((struct Ser *)Ser)->Comm);
      }
   free(Ser);
   }

/*
 * Write a byte to serial port
 */

void SerWriteByte(void *Ser, unsigned char Byte)
   {
   int ret;

   WriteFile(((struct Ser *)Ser)->Comm, &Byte, 1, &ret, NULL);
   }

/*
 * Read data from serial port
 *
 * INPUTS
 *   Ser   - Pointer to serial structure
 *   Buff  - Pointer to buffer
 *   Count - Number of bytes to read
 * 
 * RESULT
 *    Actual number of bytes read 
 */

int SerReadData(void *Ser, unsigned char *Buff, int Count)
   {
   int ret;

   ReadFile(((struct Ser *)Ser)->Comm, Buff, Count, &ret, NULL);
   return(ret);
   }