c语言读取串口数据
⑴ c 获取串口号 c 自动获取串口号
用C怎么写获取串口的内容
看驱动程序的接口啊
一般是是open(“口名”)
用C/C++写一扒游个小程序读取串口接收到贺此销的数据
你太幸运了,刚好我有一个,你在禅游vc++6.0下测试一下。
/* serrecv.c */
/* Receives and saves a file over a serial port */
/* Last modified: Septemeber 21, 2005 */
/* [goman89] */
#include
#include
#include
/* Function to print out usage information */
void usage(void);
/* Function to set up the serial port settings with the specified baud rate,
no parity, and one stop bit */
void set_up_serial_port(HANDLE h, long baud);
/* Function to receive and save file from serial port */
void get_file_from_serial_port(HANDLE h, char *file_name, unsigned long file_length);
int main(int argc, char **argv)
{
HANDLE serial_port; /* Handle to the serial port */
long baud_rate = 9600; /* Baud rate */
char port_name[] = "COM1:"; /* Name of serial port */
unsigned long file_size; /* Size of file to receive in bytes */
unsigned long bytes_received; /* Bytes received from serial port */
unsigned long file_name_size; /* Size of file name in bytes */
char file_name[256]; /* Name of file to receive */
/* Check mand line */
if (argc == 3)
{
/* Read in baud rate */
if (argv[1][1] != 'b' || sscanf(argv[2], "%ld", &baud_rate) != 1)
{
usage;
exit(0);
}
}
else if (argc != 1)
{
usage;
exit(0);
}
/* Open up a handle to the serial port */
serial_port = CreateFile(port_name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
/* Make sure port was opened */
if (serial_port == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Error opening port ");
CloseHandle(serial_port);
exit(0);
}
/* Set up the serial port */
set_up_serial_port(serial_port, baud_rate);
/* Receive file name size from serial port */
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. ");
CloseHandle(serial_port);
exit(0);
}
/* Receive file name from serial port */
ReadFile(serial_port, (void *)file_name, file_name_size, &bytes_received, NULL);
if (bytes_received != file_name_size)
{
fprintf(stderr, "Error retrieving file name. ");
CloseHandle(serial_port);
exit(0);
}
/* Append NULL terminator to end of string */
file_name[bytes_received] = '