[]C/C++读取串口接收到的数据程序
原文地址:C/C++读取串口接收到的数据程序
作者:半岛鱼
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
void usage(void);
void set_up_serial_port(HANDLE h, long baud);
void get_file_from_serial_port(HANDLE h, char *file_name, unsigned long file_length);
int main(int argc, char **argv)
{
HANDLE serial_port;
long baud_rate = 9600;
char port_name[] = "COM1:";
unsigned long file_size;
unsigned long bytes_received;
unsigned long file_name_size;
char file_name[256];
if (argc == 3)
{
if (argv[1][1] != 'b' || sscanf(argv[2], "%ld", &baud_rate) != 1)
{
usage();
exit(0);
}
}
else if (argc != 1)
{
usage();
exit(0);
}
serial_port = CreateFile(port_name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (serial_port == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Error opening portn");
CloseHandle(serial_port);
exit(0);
}
set_up_serial_port(serial_port, baud_rate);
ReadFile(serial_port, (void *)&file_name_size, sizeof(unsigned long), &bytes_received, NULL);
if (bytes_received != sizeof(unsigned long))
{
fprintf(stderr, "Error getting file name size.n");
CloseHandle(serial_port);
exit(0);
}
ReadFile(serial_port, (void *)file_name, file_name_size, &bytes_received, NULL);
if (bytes_received != file_name_size)
{
fprintf(stderr, "Error retrieving file name.n");
CloseHandle(serial_port);
exit(0);
}
file_name[bytes_received] = ' ';
ReadFile(serial_port, (void *)&file_size, sizeof(unsigned long), &bytes_received, NULL);
if (bytes_received != sizeof(unsigned long))
{
fprintf(stderr, "Error getting file size.n");
CloseHandle(serial_port);
exit(0);
}
get_file_from_serial_port(serial_port, file_name, file_size);
printf("n%lu bytes successfully received and saved as %sn", file_size, file_name);
CloseHandle(serial_port);
return 0;
}
void usage(void)
{
fprintf(stderr, "Usage:n");
fprintf(stderr, "tserrecv [-b baud rate]n");
fprintf(stderr, "tDefault baud rate is 9600n");
fprintf(stderr, "tSupported baud rates: 1200, 2400, 4800, 9600, 14400, 19200n");
return;
}
void set_up_serial_port(HANDLE h, long baud)
{
DCB properties;
GetCommState(h, &properties);
switch(baud)
{
case 1200:
properties.BaudRate = CBR_1200;
break;
case 2400:
properties.BaudRate = CBR_2400;
break;
case 4800:
properties.BaudRate = CBR_4800;
break;
case 9600:
properties.BaudRate = CBR_9600;
break;
case 14400:
properties.BaudRate = CBR_14400;
break;
case 19200:
properties.BaudRate = CBR_19200;
break;
case 38400:
properties.BaudRate = CBR_38400;
break;
default:
fprintf(stderr, "Invalid baud rate: %ldn", baud);
usage();
exit(0);
break;
}
properties.Parity = NOPARITY;
properties.ByteSize = 8;
properties.StopBits = ONESTOPBIT;
SetCommState(h, &properties);
return;
}
void get_file_from_serial_port(HANDLE h, char *file_name, unsigned long file_length)
{
FILE *data_file;
unsigned long bytes_left = file_length;
unsigned long bytes_received_total = 0;
unsigned long bytes_to_receive;
unsigned long bytes_received;
char buffer[200];
data_file = fopen(file_name, "wb");
if (data_file == NULL)
{
fprintf(stderr, "Could not create file %sn", file_name);
CloseHandle(h);
exit(0);
}
while (1)
{
if (bytes_left == 0)
{
break;
}
else if (bytes_left < 200)
{
bytes_to_receive = bytes_left;
}
else
{
bytes_to_receive = 200;
}
ReadFile(h, (void *)buffer, bytes_to_receive, &bytes_received, NULL);
if (bytes_received != bytes_to_receive)
{
fprintf(stderr, "Error reading file.n");
CloseHandle(h);
exit(0);
}
fwrite((void *)buffer, 1, bytes_received, data_file);
bytes_left -= bytes_received;
bytes_received_total += bytes_received;
printf("r%5lu bytes received.", bytes_received_total);
}
fclose(data_file);
return;
}