-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserial.c
81 lines (65 loc) · 1.87 KB
/
serial.c
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
/*
stm32flash - Open Source ST STM32 flash program for *nix
Copyright (C) 2010 Geoffrey McRae <[email protected]>
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; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include "serial.h"
#include "serial_posix.h"
serial_t *h;
int SerialInt(char *device)
{
speed_t port_baud = B115200; /*115200*/
tcflag_t port_bits = CS8; /*8 data bits*/
tcflag_t port_parity = 0; /*no parity*/
tcflag_t port_stop = 0; /*1 stop bit*/
h = serial_open(device);
if(h == NULL){
printf(" can not open the device.");
return -1;
}
/*serial_it_config(h);*/
if ( serial_setup(h,port_baud,port_bits,port_parity,port_stop)!= 0 ){
printf(" device initialization failed.");
return -1;
}
return 0;
}
int SerialGetChar(char * c)
{
return serial_read(h,c,1);
}
int SerialPutChar(const char c)
{
int cnt;
cnt = serial_write(h,(void *)&c,1);
if(cnt !=1)
printf("write 1 byte failed.\n");
return cnt;
}
int SerialRead(void *buf,size_t nbyte)
{
return serial_read(h,buf,nbyte);
}
int SerialWrite(const void *buf,size_t nbyte)
{
int cnt;
cnt = serial_write(h,(void *)buf,nbyte);
if(cnt !=nbyte)
printf("write %d byte failed.\n",(int)nbyte);
return cnt;
}
void SerialClose()
{
serial_close(h);
}