-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSerialSend.c
More file actions
105 lines (96 loc) · 2.7 KB
/
SerialSend.c
File metadata and controls
105 lines (96 loc) · 2.7 KB
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
//
// SerialSend.c - This program sends text via serial port
// Written by Ted Burke - last updated 6-12-2011
//
// Command line arguments are used to specify the text
// to send and the serial port to use. The baud rate used
// is currently always 38400 baud. The text cannot contain
// any spaces.
//
// argv[1] = device name
// argv[2] = text to send
//
// To compile with MinGW:
//
// gcc -o SerialSend.exe SerialSend.c
//
// To compile with cl, the Microsoft compiler:
//
// cl SerialSend.c
//
// To run (this example sends the characters "S365" via COM1):
//
// SerialSend COM1 S356
//
// References:
//
// Robertson Bayer, "Windows Serial Port Programming", March 30, 2008
// (I used code from Bayer's serial port tutorial as my starting point)
//
#include <windows.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
// Declare variables and structures
HANDLE hSerial;
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};
DWORD dwBytesWritten = 0;
// Parse command line arguments
if (argc < 2)
{
fprintf(stderr, "Usage:\n\n\tSerialSend DEVICE_NAME TEXT_TO_SEND\n");
return 1;
}
// Open the specified serial port (first command line argument)
hSerial = CreateFile(argv[1], GENERIC_READ|GENERIC_WRITE, 0, 0,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hSerial==INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Error: serial port %s could not be opened\n", argv[1]);
return 1;
}
// Set device parameters (38400 baud, 1 start bit, 1 stop bit, no parity)
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (GetCommState(hSerial, &dcbSerialParams) == 0)
{
fprintf(stderr, "Error getting device state\n");
CloseHandle(hSerial);
return 1;
}
dcbSerialParams.BaudRate = CBR_38400;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if(SetCommState(hSerial, &dcbSerialParams) == 0)
{
fprintf(stderr, "Error setting device parameters\n");
CloseHandle(hSerial);
return 1;
}
// Set COM port timeout settings
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if(SetCommTimeouts(hSerial, &timeouts) == 0)
{
fprintf(stderr, "Error setting timeouts\n");
CloseHandle(hSerial);
return 1;
}
// Send specified text (second command line argument; cannot contain spaces)
if(!WriteFile(hSerial, argv[2], strlen(argv[1]), &dwBytesWritten, NULL))
{
fprintf(stderr, "Error writing text to %s\n", argv[1]);
}
// Close serial port
if (CloseHandle(hSerial) == 0)
{
fprintf(stderr, "Error closing serial device %s\n", argv[1]);
return 1;
}
// exit normally
return 0;
}