ReactOS 0.4.15-dev-7953-g1f49173
sertest.c
Go to the documentation of this file.
1#include <windows.h>
2#include <stdio.h>
3
4#include <tchar.h>
5
6#define BUFSIZE 128
7#define MAX_PORTNAME_LEN 20
8#define APP_VERSION_STR "0.01"
9
10int main(int argc, char *argv[])
11{
12 CHAR txBuffer[BUFSIZE];
13 CHAR rxBuffer[BUFSIZE];
14 DWORD dwBaud = 9600;
15 DWORD dwNumWritten;
16 DWORD dwErrors;
17 DCB dcb;
18 BOOL bResult;
19 HANDLE hPort;
20 int i;
21 int nPortNum = 1;
22
23 TCHAR szPortName[MAX_PORTNAME_LEN];
24
25 if (argc > 1) {
26 //sscanf(argv[1], "%d", &dwBaud);
27 sscanf(argv[1], "%d", &nPortNum);
28 }
29 sprintf(szPortName, _T("COM%d"), nPortNum);
30
31 printf("Serial Port Test Application Version %s\n", APP_VERSION_STR);
32 printf("Attempting to open serial port %d - %s\n", nPortNum, szPortName);
33 hPort = CreateFile(szPortName,
35 0, // exclusive
36 NULL, // sec attr
38 0, // no attributes
39 NULL); // no template
40
41 if (hPort == (HANDLE)-1) {
42 printf("ERROR: CreateFile() failed with result: %lx\n", (DWORD)hPort);
43 return 1;
44 }
45 printf("CreateFile() returned: %lx\n", (DWORD)hPort);
46
47 printf("Fiddling with DTR and RTS control lines...\n");
48 for (i = 0; i < 100; i++) {
49 bResult = EscapeCommFunction(hPort, SETDTR);
50 if (!bResult) {
51 printf("WARNING: EscapeCommFunction(SETDTR) failed: %lx\n", (DWORD)bResult);
52 }
53 bResult = EscapeCommFunction(hPort, SETRTS);
54 if (!bResult) {
55 printf("WARNING: EscapeCommFunction(SETRTS) failed: %lx\n", (DWORD)bResult);
56 }
57 Sleep(500);
58/*
59#define CLRDTR (6)
60#define CLRRTS (4)
61#define SETDTR (5)
62#define SETRTS (3)
63#define SETXOFF (1)
64#define SETXON (2)
65#define SETBREAK (8)
66#define CLRBREAK (9)
67 */
68 bResult = EscapeCommFunction(hPort, CLRDTR);
69 if (!bResult) {
70 printf("WARNING: EscapeCommFunction(CLRDTR) failed: %lx\n", (DWORD)bResult);
71 }
72 bResult = EscapeCommFunction(hPort, CLRRTS);
73 if (!bResult) {
74 printf("WARNING: EscapeCommFunction(CLRRTS) failed: %lx\n", (DWORD)bResult);
75 }
76 }
77 printf("Getting the default line characteristics...\n");
78 dcb.DCBlength = sizeof(DCB);
79 if (!GetCommState(hPort, &dcb)) {
80 printf("ERROR: failed to get the dcb: %ld\n", GetLastError());
81 return 2;
82 }
83 printf("Setting the line characteristics to 9600,8,N,1\n");
84 dcb.BaudRate = dwBaud;
85 dcb.ByteSize = 8;
86 dcb.Parity = NOPARITY;
87 dcb.StopBits = ONESTOPBIT;
88
89 bResult = SetCommState(hPort, &dcb);
90 if (!bResult) {
91 printf("ERROR: failed to set the comm state: %lx\n", (DWORD)bResult);
92 return 3;
93 }
94 for (i = 0; i < BUFSIZE; i++) {
95 txBuffer[i] = (CHAR)i;
96 //printf(" %d ", txBuffer[i]);
97 rxBuffer[i] = 0xFF;
98 }
99 printf("\n");
100 printf("Writting transmit buffer to the serial port\n");
101 bResult = WriteFile(hPort, txBuffer, BUFSIZE, &dwNumWritten, NULL);
102 if (!bResult) {
103 printf("ERROR: failed to write to the serial port: %lx\n", (DWORD)bResult);
104 return 4;
105 }
106 printf("WriteFile() returned: %lx, byteswritten: %lx\n", (DWORD)bResult, dwNumWritten);
107#if 0
108 printf("Attempting to read %d bytes from the serial port\n", BUFSIZE);
109 bResult = ReadFile(hPort, rxBuffer, BUFSIZE, &dwNumRead, NULL);
110 if (!bResult) {
111 printf("ERROR: failed to read from the serial port: %lx\n", (DWORD)bResult);
112 return 5;
113 }
114 printf("ReadFile() returned: %lx, bytesread: %lx\n", (DWORD)bResult, dwNumRead);
115 for (i = 0; i < BUFSIZE; i++) {
116 printf(" %d ",rxBuffer[i]);
117 }
118#endif
119 printf("Attempting to close the serial port\n");
120 bResult = ClearCommError(hPort, &dwErrors, NULL);
121 printf("ClearCommError returned: %lx, dwErrors: %lx\n", (DWORD)bResult, dwErrors);
122 bResult = CloseHandle(hPort);
123 if (!bResult) {
124 printf("ERROR: failed to close the serial port: %lx\n", (DWORD)bResult);
125 return 6;
126 }
127 printf("Finished\n");
128 return 0;
129}
static int argc
Definition: ServiceArgs.c:12
#define CHAR(Char)
#define NULL
Definition: types.h:112
#define CloseHandle
Definition: compat.h:739
#define OPEN_EXISTING
Definition: compat.h:775
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define GENERIC_READ
Definition: compat.h:135
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
BOOL WINAPI SetCommState(HANDLE handle, LPDCB lpdcb)
Definition: comm.c:807
BOOL WINAPI GetCommState(HANDLE handle, LPDCB lpdcb)
Definition: comm.c:902
BOOL WINAPI EscapeCommFunction(HANDLE handle, DWORD func)
Definition: comm.c:602
BOOL WINAPI ClearCommError(HANDLE handle, LPDWORD errors, LPCOMSTAT lpStat)
Definition: comm.c:663
int main()
Definition: test.c:6
FCB DCB
Definition: fatstruc.h:1184
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
#define printf
Definition: freeldr.h:97
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_Check_return_ _CRTIMP int __cdecl sscanf(_In_z_ const char *_Src, _In_z_ _Scanf_format_string_ const char *_Format,...)
#define sprintf(buf, format,...)
Definition: sprintf.c:55
#define argv
Definition: mplay32.c:18
#define GENERIC_WRITE
Definition: nt_native.h:90
#define MAX_PORTNAME_LEN
Definition: sertest.c:7
#define BUFSIZE
Definition: sertest.c:6
#define APP_VERSION_STR
Definition: sertest.c:8
Definition: cdstruc.h:902
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
#define _T(x)
Definition: vfdio.h:22
#define CLRRTS
Definition: winbase.h:225
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define ONESTOPBIT
Definition: winbase.h:458
#define CLRDTR
Definition: winbase.h:224
#define SETDTR
Definition: winbase.h:226
#define NOPARITY
Definition: winbase.h:453
#define CreateFile
Definition: winbase.h:3749
#define SETRTS
Definition: winbase.h:227
char TCHAR
Definition: xmlstorage.h:189
char CHAR
Definition: xmlstorage.h:175