#include
#include
int main()
{
HANDLE hComm;
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);
if (hComm == INVALID_HANDLE_VALUE)
printf(“Error in opening serial port”);
else
printf(“opening serial port successful”);
char lpBuffer[] = 0x01;
DWORD dNoOFBytestoWrite; // No of bytes to write into the port
DWORD dNoOfBytesWritten = 0; // No of bytes written to the port
dNoOFBytestoWrite = sizeof(lpBuffer);
Status = WriteFile(hComm, // Handle to the Serial port
lpBuffer, // Data to be written to the port
dNoOFBytestoWrite, //No of bytes to write
&dNoOfBytesWritten, //Bytes written
NULL);
CloseHandle(hComm);//Closing the Serial Port
return 0;
}
简单来说就是根据芯片手册对串口进行驱动
基本过程就是初始化:配置相关寄存器,设置波特率等属性
编写发送和接收函数
调用
网上应该有不少你用的型号的例程,可以搜索阅读
那得看你用的什么芯片 在C语言函数里初始化芯片接口 比如你要向p2.0口发送数据 就写
p2^0=0x01