请帮我检查检查,串口发送的问题,分不够,可以继续加
qwa 2005-08-19 09:48:48 1、 接口电气协议:RS-485、单工
2、 波特率:4800bps
3、 数据格式:11位/帧
数据长度 :1 Byte(8 Bit)
起 始 位:1 Bit
奇偶校验位:发outputData时为1,发outputData1时为0
停 止 位:1 Bit
HWND handlePort_;
handlePort_ = CreateFile("COM1", // 端口设备: 默认 "COM1"
GENERIC_READ | GENERIC_WRITE, // 设备打开模式: 允许读写
0, // 不共享
NULL, // 默认安全设置
OPEN_EXISTING, // 打开方式:打开已经存在的端口
0, // 默认
NULL);
_DCB config_;
if (GetCommState(handlePort_,&config_) == 0)
{
ShowMessage("Get configuration port has problem.");
return;
}
config_.fBinary = 1;
config_.BaudRate = 4800;
config_.fParity = true;
config_.Parity = ODDPARITY;
config_.StopBits = ONESTOPBIT;
config_.ByteSize = 8;
if (SetCommState(handlePort_,&config_) == 0)
{
ShowMessage("Set configuration port has problem.");
return ;
}
unsigned long length;
char outputData[] = "0x77";
if (WriteFile(handlePort_, // 文件句柄
outputData, // 数据缓冲区指针
1, // 字节数
&length,NULL) == 0) // 接收成功发送数据长度的指针
{
ShowMessage("Reading of serial communication has problem.");
return;
}
ShowMessage(length);
config_.Parity = 0;
if (SetCommState(handlePort_,&config_) == 0)
{
ShowMessage("0Set configuration port has problem.");
return ;
}
char outputData1[] = "0x77,0x33,0x12,0x00";
if (WriteFile(handlePort_, // 文件句柄
outputData1, // 数据缓冲区指针
4, // 字节数
&length,NULL) == 0) // 接收成功发送数据长度的指针
{
ShowMessage("Reading of serial communication has problem.");
return;
}
if(CloseHandle(handlePort_) == 0) // 调用该函数关闭串口
{
ShowMessage("Port Closeing isn''t successed.");
return ;
}