Show uart.c syntax highlighted
/*
*
* Copyright (c) 2003 The Regents of the University of California. All
* rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "uart.h"
#ifndef __WIN32__ /*Linux & IPAQ platform*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
COMM_HANDLE open_comm_port(char port[], u16 baud_rate, u8 byte_size, u8 parity, u8 stop_bits)
{
COMM_HANDLE fd;
struct termios t;
if((fd = open(port, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0) return -1;
tcgetattr(fd, &t);
t.c_cflag = CS8 | CREAD | HUPCL | CLOCAL; /*data bits = 8*/
t.c_iflag = IGNBRK | IGNPAR;
t.c_oflag = 0;
t.c_lflag = 0;
t.c_cc[VMIN ] = 1;
t.c_cc[VTIME] = 0;
/*baud rate = 9600*/
/*
cfsetispeed(&t, B9600);
cfsetospeed(&t, B9600);
*/
/*baud rate = 19200*/
/*
cfsetispeed(&t, B19200);
cfsetospeed(&t, B19200);
*/
/*baud rate = 38400*/
cfsetispeed(&t, B38400);
cfsetospeed(&t, B38400);
/*baud rate = 115200*/
/*
cfsetispeed(&t, B115200);
cfsetospeed(&t, B115200);
*/
tcsetattr(fd, TCSANOW, &t);
return fd;
}
u8 close_comm_port(COMM_HANDLE h_port)
{
if(close(h_port)<0) return 0;
return 1;
}
u8 get_byte(COMM_HANDLE h_port, u8* value)
{
if(read(h_port,value,1)<0) return 0;
return 1;
}
u8 get_bytes(COMM_HANDLE h_port, u8* value, int len)
{
if(read(h_port,value,len)<0) return 0;
return 1;
}
u8 write_buffer(COMM_HANDLE h_port, u8 buffer[], u32 byte_count)
{
if((byte_count == 0) || (buffer == NULL)) return 0;
if(write(h_port,buffer,byte_count)<0) return 0;
return 1;
}
u32 bytes_available(COMM_HANDLE h_port)
{
u32 bytes;
ioctl(h_port,FIONREAD,&bytes);
return bytes;
}
u8 write_string(COMM_HANDLE h_port, char str[])
{
if(write(h_port,str, strlen(str))<0) return 0;
return 1;
}
#endif /*not defined __WIN32__*/
#ifdef __WIN32__ /* win32 platform */
#include <windows.h>
COMM_HANDLE open_comm_port(char port[], u16 baud_rate, u8 byte_size, u8 parity, u8 stop_bits)
{
COMMTIMEOUTS m_TimeOuts;
DCB m_dcb; // a DCB is a windows structure used for configuring the port
HANDLE m_hCom;
m_hCom = CreateFile(port,
GENERIC_READ | GENERIC_WRITE,
0, /* comm devices must be opened w/exclusive-access */
NULL, /* no security attrs */
OPEN_EXISTING, /* comm devices must use OPEN_EXISTING */
0, /* not overlapped I/O */
NULL /* hTemplate must be NULL for comm devices */
);
/* CreateFile will fail if the port is already open, or if the com port does not exist. */
if(m_hCom == INVALID_HANDLE_VALUE){
return NULL;
}
/* Now get the DCB properties of the port */
if(!GetCommState(m_hCom,&m_dcb))
{
/* something is hay wire, close the port and return */
CloseHandle(m_hCom);
return NULL;
}
/* SET THE dcb port properties*/
m_dcb.BaudRate = baud_rate;
m_dcb.ByteSize = byte_size;
m_dcb.Parity = parity;
m_dcb.StopBits = stop_bits;
/* now we can set the properties of the port with our settings. */
if(!SetCommState(m_hCom,&m_dcb))
{
/* something is hay wire, close the port and return */
CloseHandle(m_hCom);
return NULL;
}
/* set the intial size of the transmit and receive queues. These are
not exposed to outside clients of the class either. Perhaps they should be?
I set the receive buffer to 32k, and the transmit buffer to 9k (a default). */
if(!SetupComm(m_hCom, 1024*32, 1024*9))
{
/* something is hay wire, close the port and return */
CloseHandle(m_hCom);
return NULL;
}
/* These values are just default values that I determined empirically.
Adjust as necessary. I don't expose these to the outside because
most people aren't sure how they work (uhhh, like me included). */
m_TimeOuts.ReadIntervalTimeout = 15;
m_TimeOuts.ReadTotalTimeoutMultiplier = 1;
m_TimeOuts.ReadTotalTimeoutConstant = 250;
m_TimeOuts.WriteTotalTimeoutMultiplier = 1;
m_TimeOuts.WriteTotalTimeoutConstant = 250;
if(!SetCommTimeouts(m_hCom, &m_TimeOuts))
{
/* something is hay wire, close the port and return */
CloseHandle(m_hCom);
return NULL;
}
/* if we made it to here then success */
return m_hCom;
}
u8 close_comm_port(COMM_HANDLE h_port)
{
/*CloseHandle is non-zero on success*/
if(CloseHandle(h_port) != 0) return 1;
return 0;
}
u8 get_byte(COMM_HANDLE h_port, u8* value)
{
DWORD dummy;
if(!ReadFile(h_port,value,1,&dummy,NULL))return 0;
return 1;
}
u8 write_buffer(COMM_HANDLE h_port, u8 buffer[], u32 byte_count)
{
DWORD dummy;
if((byte_count == 0) || (buffer == NULL)) return 0;
if(!WriteFile(h_port,buffer,byte_count,&dummy,NULL)) return 0;
return 1;
}
u32 bytes_available(COMM_HANDLE h_port)
{
COMSTAT comstat;
DWORD dummy;
if(!ClearCommError(h_port, &dummy, &comstat)) return 0;
return comstat.cbInQue;
}
u8 write_string(COMM_HANDLE h_port, char str[])
{
DWORD dummy;
if(!WriteFile(h_port,str, strlen(str),&dummy,NULL)) return 0;
return 1;
}
#endif
See more files for this project here