C语言可以通过调用操作系统提供的串口API实现串口通信。常见的操作系统有Windows和Linux,在这两个操作系统中实现串口通信的方式略有不同。
在Windows系统中,可以使用Win32 API中的CreateFile函数来打开串口端口,使用ReadFile和WriteFile函数来读写串口数据。示例代码如下:
#include <windows.h>
#include <stdio.h>
int main()
{
HANDLE hComm;
DCB dcb = { 0 };
COMMTIMEOUTS timeouts = { 0 };
DWORD bytesRead, bytesWritten;
char serialPort[] = "\\\\.\\COM1";
hComm = CreateFile(serialPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hComm == INVALID_HANDLE_VALUE)
{
printf("Failed to open serial port.\n");
return 1;
}
dcb.DCBlength = sizeof(DCB);
if (!GetCommState(hComm, &dcb))
{
printf("Failed to get serial port settings.\n");
CloseHandle(hComm);
return 1;
}
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
if (!SetCommState(hComm, &dcb))
{
printf("Failed to set serial port settings.\n");
CloseHandle(hComm);
return 1;
}
timeouts.ReadIntervalTimeout = 10;
timeouts.ReadTotalTimeoutConstant = 100;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 100;
timeouts.WriteTotalTimeoutMultiplier = 10;
if (!SetCommTimeouts(hComm, &timeouts))
{
printf("Failed to set serial port timeouts.\n");
CloseHandle(hComm);
return 1;
}
char data[] = "Hello, serial port!";
if (!WriteFile(hComm, data, sizeof(data), &bytesWritten, NULL))
{
printf("Failed to write data to serial port.\n");
CloseHandle(hComm);
return 1;
}
char buffer[256];
if (!ReadFile(hComm, buffer, sizeof(buffer), &bytesRead, NULL))
{
printf("Failed to read data from serial port.\n");
CloseHandle(hComm);
return 1;
}
printf("Received %d bytes: %s\n", bytesRead, buffer);
CloseHandle(hComm);
return 0;
}
在Linux系统中,可以使用标准的串口API函数来读写串口数据,例如open、tcgetattr、tcsetattr、read和write等函数。示例代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
int main()
{
int fd;
struct termios options;
char serialPort[] = "/dev/ttyS0";
fd = open(serialPort, O_RDWR | O_NOCTTY);
if (fd == -1)
{
printf("Failed to open serial port.\n");
return 1;
}
tcgetattr(fd, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= CS8 | CLOCAL | CREAD;
options.c_iflag |= IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 1;
tcsetattr(fd, TCSANOW, &options);
char data[] = "Hello, serial port!";
int bytesWritten = write(fd, data, sizeof(data));
if (bytesWritten == -1)
{
printf("Failed to write data to serial port.\n");
close(fd);
return 1;
}
char buffer[256];
int bytesRead = read(fd, buffer, sizeof(buffer));
if (bytesRead == -1)
{
printf("Failed to read data from serial port.\n");
close(fd);
return 1;
}
printf("Received %d bytes: %s\n", bytesRead, buffer);
close(fd);
return 0;
}
```C语言可以通过调用操作系统提供的串口API实现串口通信。常见的操作系统有Windows和Linux,在这两个操作系统中实现串口通信的方式略有不同。
在Windows系统中,可以使用Win32 API中的CreateFile函数来打开串口端口,使用ReadFile和WriteFile函数来读写串口数据。示例代码如下:
```c
#include <windows.h>
#include <stdio.h>
int main()
{
HANDLE hComm;
DCB dcb = { 0 };
COMMTIMEOUTS timeouts = { 0 };
DWORD bytesRead, bytesWritten;
char serialPort[] = "\\\\.\\COM1";
hComm = CreateFile(serialPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hComm == INVALID_HANDLE_VALUE)
{
printf("Failed to open serial port.\n");
return 1;
}
dcb.DCBlength = sizeof(DCB);
if (!GetCommState(hComm, &dcb))
{
printf("Failed to get serial port settings.\n");
CloseHandle(hComm);
return 1;
}
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
if (!SetCommState(hComm, &dcb))
{
printf("Failed to set serial port settings.\n");
CloseHandle(hComm);
return 1;
}
timeouts.ReadIntervalTimeout = 10;
timeouts.ReadTotalTimeoutConstant = 100;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 100;
timeouts.WriteTotalTimeoutMultiplier = 10;
if (!SetCommTimeouts(hComm, &timeouts))
{
printf("Failed to set serial port timeouts.\n");
CloseHandle(hComm);
return 1;
}
char data[] = "Hello, serial port!";
if (!WriteFile(hComm, data, sizeof(data), &bytesWritten, NULL))
{
printf("Failed to write data to serial port.\n");
CloseHandle(hComm);
return 1;
}
char buffer[256];
if (!ReadFile(hComm, buffer, sizeof(buffer), &bytesRead, NULL))
{
printf("Failed to read data from serial port.\n");
CloseHandle(hComm);
return 1;
}
printf("Received %d bytes: %s\n", bytesRead, buffer);
CloseHandle(hComm);
return 0;
}
在Linux系统中,可以使用标准的串口API函数来读写串口数据,例如open、tcgetattr、tcsetattr、read和write等函数。示例代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
int main()
{
int fd;
struct termios options;
char serialPort[] = "/dev/ttyS0";
fd = open(serialPort, O_RDWR | O_NOCTTY);
if (fd == -1)
{
printf("Failed to open serial port.\n");
return 1;
}
tcgetattr(fd, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= CS8 | CLOCAL | CREAD;
options.c_iflag |= IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 1;
tcsetattr(fd, TCSANOW, &options);
char data[] = "Hello, serial port!";
int bytesWritten = write(fd, data, sizeof(data));
if (bytesWritten == -1)
{
printf("Failed to write data to serial port.\n");
close(fd);
return 1;
}
char buffer[256];
int bytesRead = read(fd, buffer, sizeof(buffer));
if (bytesRead == -1)
{
printf("Failed to read data from serial port.\n");
close(fd);
return 1;
}
printf("Received %d bytes: %s\n", bytesRead, buffer);
close(fd);
return 0;
}