请帮我检查检查,串口发送的问题,分不够,可以继续加

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 ;
}
...全文
145 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dxqxiaoqiang 2005-08-24
  • 打赏
  • 举报
回复
是不是不知道怎么初始化?
你首先贴出来你的485的协议,之后再说一下遇到的问题,大家好给你分析。
qwa 2005-08-19
  • 打赏
  • 举报
回复
没有人懂吗
loveenic2 2005-08-19
  • 打赏
  • 举报
回复
你是想问什么问题? 上边的程序运行时出现什么问题?

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧