这个程序用Dev C++可以不
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
int analysis(char *buff);
int OpenDev(char *Dev);
void set_speed(int fd, int speed);
int set_Parity(int fd,int databits,int stopbits,int parity);
int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400, 1200, 300, };
int OpenDev(char *Dev)
{
int fd = open(Dev,O_RDWR | O_NOCTTY | O_NONBLOCK);
if(-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
{
printf("Open com success!!!!!!!!!!!");
return fd;
}
}
void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
?tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
?{
if (speed == name_arr[i])
{
#include<stdio.h>
int main()
{
HANDLE hComm;
hComm = CreateFile(“\\\\.\\COM24”, //port name
GENERIC_READ | GENERIC_WRITE, //Read/Write
0, // No Sharing
NULL, // No Security
OPEN_EXISTING,// Open existing port only
0, // Non Overlapped I/O
NULL); // Null for Comm Devices
if (hComm == INVALID_HANDLE_VALUE)
printf(“Error in opening serial port”);
else
printf(“opening serial port successful”);
CloseHandle(hComm);//Closing the Serial Port
return 0;
}
//正确操作方法
hComm = CreateFile(“COM1”, // for COM1—COM9 only
GENERIC_READ | GENERIC_WRITE, //Read/Write
0, // No Sharing
NULL, // No Security
OPEN_EXISTING, // Open existing port only
0, // Non Overlapped I/O
NULL);本回答被提问者采纳