TimeMachine

导航

怎样读串口

1。打开并配置串口

// 初始化串口
// 参数: port 串口号
// BaudRate 速率
BOOL CSerial::InitSerialPort()
{
CString csPortName;
DWORD dwCfgSize;//串口配置数据结构的大小
csPortName = AfxGetApp()->GetProfileString("串口选择","端口号",NULL);
if(csPortName.GetLength() < 3) csPortName = "COM1";

//打开串口
m_hComPort = CreateFile(csPortName,
GENERIC_READ | GENERIC_WRITE,
0, // 串口设备必须是独占使用
NULL, // no security attrs
OPEN_EXISTING, //串口设备必须使用OPEN_EXISTING设置
0, // 输入输出不能重叠
NULL // hTemplate对于串口设备必须是NULL
);

if (m_hComPort == INVALID_HANDLE_VALUE)
{
return FALSE;
}

//获得当前的串口默认配置
GetCommConfig(m_hComPort,&m_CFG,&dwCfgSize);

//以下是固定的串口初始值
m_CFG.dcb.BaudRate = 4800;
m_CFG.dcb.ByteSize = 8;
m_CFG.dcb.fInX = 0;
m_CFG.dcb.fOutX = 0;
m_CFG.dcb.fOutxCtsFlow = 0;
m_CFG.dcb.fRtsControl = 0;
m_CFG.dcb.fParity = 0;
m_CFG.dcb.Parity = 0;
m_CFG.dcb.StopBits = 2;
/*以下是预留的可更改串口初始值
m_CFG.dcb.BaudRate = AfxGetApp()->GetProfileInt("串口配置","BaudRate",0);
m_CFG.dcb.ByteSize = AfxGetApp()->GetProfileInt("串口配置", "ByteSize",0);
m_CFG.dcb.fInX = AfxGetApp()->GetProfileInt("串口配置", "fInX",0);
m_CFG.dcb.fOutX = AfxGetApp()->GetProfileInt("串口配置", "fOutX",0);
m_CFG.dcb.fOutxCtsFlow = AfxGetApp()->GetProfileInt("串口配置", "fOutxCtsFlow",0);
m_CFG.dcb.fParity = AfxGetApp()->GetProfileInt("串口配置", "fParity",0);
m_CFG.dcb.fRtsControl = AfxGetApp()->GetProfileInt("串口配置", "fRtsControl",0);
m_CFG.dcb.Parity = AfxGetApp()->GetProfileInt("串口配置", "Parity",0);
m_CFG.dcb.StopBits = AfxGetApp()->GetProfileInt("串口配置", "StopBits",0);
*/
//调用串口配置对话框
// CommConfigDialog(csPortName,NULL,&m_CFG);

// ;
if(SetCommConfig(m_hComPort,&m_CFG,sizeof(m_CFG)))
{
//保存当前串口的配置
AfxGetApp()->WriteProfileInt("串口配置", "BaudRate",m_CFG.dcb.BaudRate);
AfxGetApp()->WriteProfileInt("串口配置", "ByteSize",m_CFG.dcb.ByteSize);
AfxGetApp()->WriteProfileInt("串口配置", "DCBlength",m_CFG.dcb.DCBlength);
AfxGetApp()->WriteProfileInt("串口配置", "EofChar",m_CFG.dcb.EofChar);
AfxGetApp()->WriteProfileInt("串口配置", "ErrorChar",m_CFG.dcb.ErrorChar);
AfxGetApp()->WriteProfileInt("串口配置", "EvtChar",m_CFG.dcb.EvtChar);
AfxGetApp()->WriteProfileInt("串口配置", "fAbortOnError",m_CFG.dcb.fAbortOnError);
AfxGetApp()->WriteProfileInt("串口配置", "fBinary",m_CFG.dcb.fBinary);
AfxGetApp()->WriteProfileInt("串口配置", "fDsrSensitivity",m_CFG.dcb.fDsrSensitivity);
AfxGetApp()->WriteProfileInt("串口配置", "fDtrControl",m_CFG.dcb.fDtrControl);
AfxGetApp()->WriteProfileInt("串口配置", "fDummy2",m_CFG.dcb.fDummy2);
AfxGetApp()->WriteProfileInt("串口配置", "fErrorChar",m_CFG.dcb.fErrorChar);
AfxGetApp()->WriteProfileInt("串口配置", "fInX",m_CFG.dcb.fInX);
AfxGetApp()->WriteProfileInt("串口配置", "fNull",m_CFG.dcb.fNull);
AfxGetApp()->WriteProfileInt("串口配置", "fOutX",m_CFG.dcb.fOutX);
AfxGetApp()->WriteProfileInt("串口配置", "fOutxCtsFlow",m_CFG.dcb.fOutxCtsFlow);
AfxGetApp()->WriteProfileInt("串口配置", "fOutxDsrFlow",m_CFG.dcb.fOutxDsrFlow);
AfxGetApp()->WriteProfileInt("串口配置", "fParity",m_CFG.dcb.fParity);
AfxGetApp()->WriteProfileInt("串口配置", "fRtsControl",m_CFG.dcb.fRtsControl);
AfxGetApp()->WriteProfileInt("串口配置", "fTXContinueOnXoff",m_CFG.dcb.fTXContinueOnXoff);
AfxGetApp()->WriteProfileInt("串口配置", "Parity",m_CFG.dcb.Parity);
AfxGetApp()->WriteProfileInt("串口配置", "StopBits",m_CFG.dcb.StopBits);
AfxGetApp()->WriteProfileInt("串口配置", "wReserved",m_CFG.dcb.wReserved);
AfxGetApp()->WriteProfileInt("串口配置", "wReserved1",m_CFG.dcb.wReserved1);
AfxGetApp()->WriteProfileInt("串口配置", "XoffChar",m_CFG.dcb.XoffChar);
AfxGetApp()->WriteProfileInt("串口配置", "XoffLim",m_CFG.dcb.XoffLim);
AfxGetApp()->WriteProfileInt("串口配置", "XonChar",m_CFG.dcb.XonChar);
AfxGetApp()->WriteProfileInt("串口配置", "XonLim",m_CFG.dcb.XonLim);
return TRUE;
}
else return FALSE;
// EscapeCommFunction(hComPort,SETDTR); // 设置DTR(PIN 4)信号为0,为RS-232芯片供电
// EscapeCommFunction(hComPort,SETRTS); // 设置DTR(PIN 7)信号为0,为RS-232芯片供电

}
1。打开并配置串口

// 初始化串口
// 参数: port 串口号
// BaudRate 速率
BOOL CSerial::InitSerialPort()
{
CString csPortName;
DWORD dwCfgSize;//串口配置数据结构的大小
csPortName = AfxGetApp()->GetProfileString("串口选择","端口号",NULL);
if(csPortName.GetLength() < 3) csPortName = "COM1";

//打开串口
m_hComPort = CreateFile(csPortName,
GENERIC_READ | GENERIC_WRITE,
0, // 串口设备必须是独占使用
NULL, // no security attrs
OPEN_EXISTING, //串口设备必须使用OPEN_EXISTING设置
0, // 输入输出不能重叠
NULL // hTemplate对于串口设备必须是NULL
);

if (m_hComPort == INVALID_HANDLE_VALUE)
{
return FALSE;
}

//获得当前的串口默认配置
GetCommConfig(m_hComPort,&m_CFG,&dwCfgSize);

//以下是固定的串口初始值
m_CFG.dcb.BaudRate = 4800;
m_CFG.dcb.ByteSize = 8;
m_CFG.dcb.fInX = 0;
m_CFG.dcb.fOutX = 0;
m_CFG.dcb.fOutxCtsFlow = 0;
m_CFG.dcb.fRtsControl = 0;
m_CFG.dcb.fParity = 0;
m_CFG.dcb.Parity = 0;
m_CFG.dcb.StopBits = 2;
/*以下是预留的可更改串口初始值
m_CFG.dcb.BaudRate = AfxGetApp()->GetProfileInt("串口配置","BaudRate",0);
m_CFG.dcb.ByteSize = AfxGetApp()->GetProfileInt("串口配置", "ByteSize",0);
m_CFG.dcb.fInX = AfxGetApp()->GetProfileInt("串口配置", "fInX",0);
m_CFG.dcb.fOutX = AfxGetApp()->GetProfileInt("串口配置", "fOutX",0);
m_CFG.dcb.fOutxCtsFlow = AfxGetApp()->GetProfileInt("串口配置", "fOutxCtsFlow",0);
m_CFG.dcb.fParity = AfxGetApp()->GetProfileInt("串口配置", "fParity",0);
m_CFG.dcb.fRtsControl = AfxGetApp()->GetProfileInt("串口配置", "fRtsControl",0);
m_CFG.dcb.Parity = AfxGetApp()->GetProfileInt("串口配置", "Parity",0);
m_CFG.dcb.StopBits = AfxGetApp()->GetProfileInt("串口配置", "StopBits",0);
*/
//调用串口配置对话框
// CommConfigDialog(csPortName,NULL,&m_CFG);

// ;
if(SetCommConfig(m_hComPort,&m_CFG,sizeof(m_CFG)))
{
//保存当前串口的配置
AfxGetApp()->WriteProfileInt("串口配置", "BaudRate",m_CFG.dcb.BaudRate);
AfxGetApp()->WriteProfileInt("串口配置", "ByteSize",m_CFG.dcb.ByteSize);
AfxGetApp()->WriteProfileInt("串口配置", "DCBlength",m_CFG.dcb.DCBlength);
AfxGetApp()->WriteProfileInt("串口配置", "EofChar",m_CFG.dcb.EofChar);
AfxGetApp()->WriteProfileInt("串口配置", "ErrorChar",m_CFG.dcb.ErrorChar);
AfxGetApp()->WriteProfileInt("串口配置", "EvtChar",m_CFG.dcb.EvtChar);
AfxGetApp()->WriteProfileInt("串口配置", "fAbortOnError",m_CFG.dcb.fAbortOnError);
AfxGetApp()->WriteProfileInt("串口配置", "fBinary",m_CFG.dcb.fBinary);
AfxGetApp()->WriteProfileInt("串口配置", "fDsrSensitivity",m_CFG.dcb.fDsrSensitivity);
AfxGetApp()->WriteProfileInt("串口配置", "fDtrControl",m_CFG.dcb.fDtrControl);
AfxGetApp()->WriteProfileInt("串口配置", "fDummy2",m_CFG.dcb.fDummy2);
AfxGetApp()->WriteProfileInt("串口配置", "fErrorChar",m_CFG.dcb.fErrorChar);
AfxGetApp()->WriteProfileInt("串口配置", "fInX",m_CFG.dcb.fInX);
AfxGetApp()->WriteProfileInt("串口配置", "fNull",m_CFG.dcb.fNull);
AfxGetApp()->WriteProfileInt("串口配置", "fOutX",m_CFG.dcb.fOutX);
AfxGetApp()->WriteProfileInt("串口配置", "fOutxCtsFlow",m_CFG.dcb.fOutxCtsFlow);
AfxGetApp()->WriteProfileInt("串口配置", "fOutxDsrFlow",m_CFG.dcb.fOutxDsrFlow);
AfxGetApp()->WriteProfileInt("串口配置", "fParity",m_CFG.dcb.fParity);
AfxGetApp()->WriteProfileInt("串口配置", "fRtsControl",m_CFG.dcb.fRtsControl);
AfxGetApp()->WriteProfileInt("串口配置", "fTXContinueOnXoff",m_CFG.dcb.fTXContinueOnXoff);
AfxGetApp()->WriteProfileInt("串口配置", "Parity",m_CFG.dcb.Parity);
AfxGetApp()->WriteProfileInt("串口配置", "StopBits",m_CFG.dcb.StopBits);
AfxGetApp()->WriteProfileInt("串口配置", "wReserved",m_CFG.dcb.wReserved);
AfxGetApp()->WriteProfileInt("串口配置", "wReserved1",m_CFG.dcb.wReserved1);
AfxGetApp()->WriteProfileInt("串口配置", "XoffChar",m_CFG.dcb.XoffChar);
AfxGetApp()->WriteProfileInt("串口配置", "XoffLim",m_CFG.dcb.XoffLim);
AfxGetApp()->WriteProfileInt("串口配置", "XonChar",m_CFG.dcb.XonChar);
AfxGetApp()->WriteProfileInt("串口配置", "XonLim",m_CFG.dcb.XonLim);
return TRUE;
}
else return FALSE;
// EscapeCommFunction(hComPort,SETDTR); // 设置DTR(PIN 4)信号为0,为RS-232芯片供电
// EscapeCommFunction(hComPort,SETRTS); // 设置DTR(PIN 7)信号为0,为RS-232芯片供电

}
2。读数据
// 读出串口通讯的数据
// 参数: data 读入的数据
// number 读入的数据长度
// 返回: 0 失败
// 1 成功
//线程退出码;0,未进入;1,读数据成功退出;2,超时退出;3,中断退出。4,正在读数据中
//6,读数据失败.
BOOL CSerial::ReadData(BYTE *data, DWORD number,BOOL *StopFlag,DWORD far*exitCode)
{
COMSTAT CommStat; //端口状态数据
DWORD Errors;
DWORD ReadTimes = 10,j=0;

DWORD m_Number;
DWORD READ_NUM = 0;
DWORD oldNum = 0,newNum = 0;

// HANDLE hReadEvent;
// hReadEvent = CreateEvent(NULL,TRUE,FALSE,"ReadEvent");

*exitCode = 4;
do {
//如果高级中断过来,终止线程
if(*StopFlag){
*exitCode = CommStat.cbInQue;
return FALSE;
}
if(ClearCommError( m_hComPort, &Errors, &CommStat)==0)
{
*exitCode = CommStat.cbInQue;
return FALSE; //读入端口状态数据失败
}
newNum = CommStat.cbInQue;
WaitForSingleObject(hReadEvent,100);
if(ClearCommError( m_hComPort, &Errors, &CommStat)==0)
{
*exitCode = CommStat.cbInQue;
return FALSE; //读入端口状态数据失败
}

oldNum = CommStat.cbInQue;

if(oldNum == newNum && oldNum != 0)
{
//返回的数据已经读入串口缓冲
READ_NUM=0;
READ_NUM+=CommStat.cbInQue;
if(READ_NUM > 512) return FALSE;
if(ReadFile(m_hComPort, (char far *)data, READ_NUM , &m_Number,NULL)==0)
{
*exitCode = CommStat.cbInQue;
return FALSE; //读入数据失败
}
else{
*exitCode = READ_NUM;
*exitCode = CommStat.cbInQue;
return TRUE;
}
}
j++;
}
while (j<ReadTimes);//等待6秒
*exitCode = 2;
*exitCode = CommStat.cbInQue;
return FALSE; //等待超时
}
3。发送数据

//发送一个字节
// 返回: 0 失败
// 1 成功
BOOL CSerial::Send_1_BYTE(BYTE data)
{
DWORD m_Number;
if(WriteFile(m_hComPort,&data, 1 , &m_Number,NULL))
return TRUE; //输出数据成功
else
return FALSE; //输出数据失败
}

// 发送数据
// 参数: dat:等待发送的数据
// number:需要发送的位数 |
// 返回: 0 失败
// 1 成功
BOOL CSerial::SendData(BYTE *SendBuff, WORD Length)
{
for(WORD i=0;i < Length ; i ++)
{
if( !Send_1_BYTE(SendBuff[i]))
return FALSE;
}
return TRUE;
}
                                                                                                  ----------------打盹的神仙

posted on 2005-03-16 20:37  饽饽  阅读(4814)  评论(4编辑  收藏  举报