struct GeneralBatchIICData
{
uint8_t uiWRMode;//0:写入 1:读取
uint16_t uiSalvid;//器件地址
uint64_t uiReg;//reg
uint64_t uiValue;//value
uint16_t usType;//IIC模式 0x0808为8位地址8位值 0x1608为16位地址8位值 最大0x3232
int iDelayUs;//延时 单位us
bool bAck;//是否需要ACK
GeneralBatchIICData(){
bAck = true;
}
};
typedef std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> >
u8string;
DWORD CEmbeddedInterface::GeneralBatchIIC(int iCamType, int iCount, GeneralBatchIICData data[], int* iErrorIndex)
{
//组包
{
std::list<u8string> packetList;
for (int i = 0; i < iCount; i++)
{
u8string miniPacket;
GeneralBatchIICData& currentData = data[i];
unsigned int uiRegSize = 0, uiDataSize = 0;
unsigned char ucReg[8] = { 0 };
unsigned char ucData[8] = { 0 };
unsigned char ucSleep[4] = { 0 };
switch (currentData.usType & 0xff00) {
case 0x1600:
uiRegSize = 2;
ucReg[0] = (unsigned char)((currentData.uiReg >> 8) & 0xFF);
ucReg[1] = (unsigned char)(currentData.uiReg & 0xFF);
break;
case 0x3200:
uiRegSize = 4;
ucReg[0] = (unsigned char)((currentData.uiReg >> 24) & 0xFF);
ucReg[1] = (unsigned char)((currentData.uiReg >> 16) & 0xFF);
ucReg[2] = (unsigned char)((currentData.uiReg >> 8) & 0xFF);
ucReg[3] = (unsigned char)(currentData.uiReg & 0xFF);
break;
case 0x0800:
default:
uiRegSize = 1;
ucReg[0] = (unsigned char)(currentData.uiReg & 0xFF);
break;
}
switch (currentData.usType & 0xff) {
case 0x16:
uiDataSize = 2;
ucData[0] = (unsigned char)((currentData.uiValue >> 8) & 0xFF);
ucData[1] = (unsigned char)(currentData.uiValue & 0xFF);
break;
case 0x32:
uiDataSize = 4;
ucData[0] = (unsigned char)((currentData.uiValue >> 24) & 0xFF);
ucData[1] = (unsigned char)((currentData.uiValue >> 16) & 0xFF);
ucData[2] = (unsigned char)((currentData.uiValue >> 8) & 0xFF);
ucData[3] = (unsigned char)(currentData.uiValue & 0xFF);
break;
case 0x08:
default:
uiDataSize = 1;
ucData[0] = (unsigned char)(currentData.uiValue & 0xFF);
break;
}
int iSleep10Ns = max(2100, currentData.iDelayUs * 100);
ucSleep[0] = (unsigned char)((iSleep10Ns >> 24) & 0xFF);
ucSleep[1] = (unsigned char)((iSleep10Ns >> 16) & 0xFF);
ucSleep[2] = (unsigned char)((iSleep10Ns >> 8) & 0xFF);
ucSleep[3] = (unsigned char)(iSleep10Ns & 0xFF);
if (currentData.uiWRMode == 0)
{
//写入
int iPacketSize = 1 + 1 + 1 + uiRegSize + uiDataSize + 4;
miniPacket += (unsigned char)(iPacketSize & 0xFF);
if (currentData.bAck)
miniPacket += u8string(1, 0x00 | 0x02);
else
miniPacket += u8string(1, 0x00);
miniPacket += (uint8_t)currentData.uiSalvid;
miniPacket += u8string(ucReg, uiRegSize);
miniPacket += u8string(ucData, uiDataSize);
miniPacket += u8string(ucSleep, 4);
}
else if (currentData.uiWRMode == 1)
{
//读取
int iPacketSize = 1 + 1 + 1 + 1 + 4 + 4;
miniPacket += (unsigned char)(iPacketSize & 0xFF);
if (currentData.bAck)
miniPacket += u8string(1, 0x01 | 0x02);
else
miniPacket += u8string(1, 0x01);
miniPacket += (uiRegSize - 1) + (uiDataSize << 4);
miniPacket += (uint8_t)(currentData.uiSalvid | 0x01);
miniPacket += u8string(4 - uiRegSize, 0x00);
miniPacket += u8string(ucReg, uiRegSize);
miniPacket += u8string(ucSleep, 4);
}
else
{
AddString(iCamType, "index:%d,uiWRMode异常", i);
return 0x1000;
}
packetList.push_back(miniPacket);
}
u8string totalPacket;
totalPacket += (unsigned char)((iCount >> 8) & 0xFF);
totalPacket += (unsigned char)((iCount) & 0xFF);
std::list<u8string>::iterator it;
for (it = packetList.begin(); it != packetList.end(); ++it)
totalPacket += *it;
if (totalPacket.size() > 60000)
{
AddString(iCamType, "数据超过协议包最大上限60000");
return CurrencyErrCode_ParaErr;
}
//发包
int iOnceSendSize = 1000;//每次发送的字节数
int iTotalPacketIndex = 0;//总包中的Index
while (true)
{
bool bLastPacket = true;//是否为最后一个包 最后一个包发送0xff作为标志
unsigned char ucCommand[1024] = { 0 };
ucCommand[0] = 0x80;
ucCommand[1] = 0xac;
if (iCamType == 0)
ucCommand[4] = 2;
else
ucCommand[4] = 1;
int iMiniPacketIndex = 0;//分包的Index
while (iTotalPacketIndex < totalPacket.size())
{
ucCommand[6 + iMiniPacketIndex] = totalPacket[iTotalPacketIndex];
iMiniPacketIndex++;
iTotalPacketIndex++;
if (iTotalPacketIndex == totalPacket.size())
break;
if (iTotalPacketIndex % iOnceSendSize == 0)
{
bLastPacket = false;
break;
}
}
if (bLastPacket)
{
int uiDataLength = iMiniPacketIndex + 2;
ucCommand[2] = (unsigned char)((uiDataLength >> 8) & 0xFF);
ucCommand[3] = (unsigned char)((uiDataLength) & 0xFF);
ucCommand[5] = 0xFF;
}
else
{
int uiDataLength = iOnceSendSize + 2;
ucCommand[2] = (unsigned char)((uiDataLength >> 8) & 0xFF);
ucCommand[3] = (unsigned char)((uiDataLength) & 0xFF);
ucCommand[5] = iTotalPacketIndex / iOnceSendSize - 1;
}
int iErrorCode = EmbeddedBulkWriteCommunication(iCamType, ucCommand);
if (iErrorCode)
return iErrorCode;
if (bLastPacket)
break;
}
}
//收包
{
std::map<int, u8string> packetMap;
u8string totalPacket;
unsigned char ucWriteCommand[1024] = { 0 };
ucWriteCommand[0] = 0x80;
ucWriteCommand[1] = 0xad;
ucWriteCommand[2] = 0x00;
ucWriteCommand[3] = 0x01;
if (iCamType == 0)
ucWriteCommand[4] = 2;
else
ucWriteCommand[4] = 1;
unsigned char ucReadCommand[1024] = { 0 };
int iGetResult = 0;
while (true)
{
memset(ucReadCommand, 0, 1024);
int iErrorCode = EmbeddedBulkReadCommunication(iCamType, ucWriteCommand, ucReadCommand);
if (iErrorCode)
return iErrorCode;
iGetResult = ucReadCommand[5];//0:为已完成 1:为未完成 其他为失败
if (iGetResult == 0x01)
{
Sleep(2);
continue;
}
int iPacketSize = (ucReadCommand[2] << 8) + ucReadCommand[3] - 3;
int iPacketIndex = ucReadCommand[6];
packetMap[iPacketIndex] = u8string((ucReadCommand)+7, iPacketSize);
if (iPacketIndex == 0xff)
break;
}
//解包
std::map<int, u8string>::iterator it;
for (it = packetMap.begin(); it != packetMap.end(); ++it)
totalPacket += it->second;
int iTotalPacketIndex = 0;//总包中的Index
//获取第几个IIC指令异常
if (totalPacket.size() < 2)
{
AddString(iCamType, "返回值数据长度校验失败");
return CurrencyErrCode_IICErr;
}
if (iErrorIndex != nullptr)
*iErrorIndex = (((unsigned int)totalPacket[0]) << 8) + (((unsigned int)totalPacket[1]) << 0);
iTotalPacketIndex += 2;
//解析读取的IIC数据
for (int i = 0; i < iCount; i++)
{
GeneralBatchIICData& currentData = data[i];
if (currentData.uiWRMode == 1)
{
unsigned int uiDataSize = 0;
switch (currentData.usType & 0xff) {
case 0x16:
uiDataSize = 2;
break;
case 0x32:
uiDataSize = 4;
break;
case 0x08:
default:
uiDataSize = 1;
break;
}
if ((iTotalPacketIndex + uiDataSize - 1) > totalPacket.size())
{
AddString(iCamType, "返回值数据长度校验失败");
return CurrencyErrCode_IICErr;
}
switch (currentData.usType & 0xff) {
case 0x16:
currentData.uiValue = (((unsigned int)totalPacket[iTotalPacketIndex]) << 8)
+ (((unsigned int)totalPacket[iTotalPacketIndex + 1]) << 0);
break;
case 0x32:
currentData.uiValue = (((unsigned int)totalPacket[iTotalPacketIndex]) << 24)
没有合适的资源?快使用搜索试试~ 我知道了~
motor-slave-iic-back
共491个文件
h:101个
d:68个
o:68个
需积分: 5 0 下载量 69 浏览量
2025-01-13
08:11:47
上传
评论
收藏 194.22MB RAR 举报
温馨提示
motor-slave-iic-back
资源推荐
资源详情
资源评论
收起资源包目录
motor-slave-iic-back (491个子文件)
MOTOR_TEST_V090302.axf 2.09MB
MOTOR_TEST_V0903.axf 2.07MB
MOTOR_TEST_V090301.axf 2.07MB
MOTOR_TEST_V0902.axf 2.07MB
MOTOR_TEST_V0901.axf 2.05MB
MOTOR_TEST_V0100.axf 2.04MB
MOTOR_TEST.axf 2.04MB
MOTOR_V9.axf 2MB
TMR_v8.axf 1.99MB
CloseLoop.axf 1.96MB
MOTOR_TEST_V090303.axf 1.93MB
CloseLoop_sct.Bak 555B
MOTOR_TEST_V0901_sct.Bak 494B
TMR_v0321_sct.Bak 494B
TMR_v8_sct.Bak 479B
MOTOR_TEST_V090302.bin 68KB
MOTOR_TEST_V090301.bin 64KB
MOTOR_TEST_V0903.bin 64KB
MOTOR_TEST_V0902.bin 63KB
MOTOR_TEST_V0901.bin 62KB
MOTOR_TEST_V0100.bin 61KB
MOTOR_V9.bin 54KB
TMR_v8.bin 53KB
MOTOR_TEST_V090303_slave.bin 39KB
MOTOR_TEST_V090303_slave_10s.bin 39KB
stm32h7xx_hal_tim.c 254KB
stm32h7xx_hal_i2c.c 227KB
stm32h7xx_hal_adc.c 161KB
stm32h7xx_hal_uart.c 158KB
stm32h7xx_hal_rcc_ex.c 125KB
stm32h7xx_hal_tim_ex.c 103KB
stm32h7xx_hal_adc_ex.c 102KB
stm32h7xx_hal_pwr_ex.c 77KB
stm32h7xx_hal_dma.c 68KB
stm32h7xx_hal_rcc.c 65KB
stm32h7xx_hal_flash_ex.c 64KB
stm32h7xx_hal_mdma.c 63KB
stm32h7xx_hal_pcd.c 61KB
stm32h7xx_ll_usb.c 60KB
stm32h7xx_hal.c 44KB
stm32h7xx_hal_flash.c 37KB
usbd_cdc.c 35KB
stm32h7xx_hal_uart_ex.c 35KB
stm32h7xx_hal_pwr.c 33KB
FRA_S.c 32KB
stm32h7xx_hal_exti.c 26KB
stm32h7xx_hal_dma_ex.c 25KB
moni_i2c.c 23KB
usbd_ctlreq.c 22KB
usbd_conf.c 21KB
rohm_fra.c 20KB
tim.c 19KB
stm32h7xx_hal_gpio.c 19KB
stm32h7xx_hal_cortex.c 19KB
system_stm32h7xx.c 16KB
stm32h7xx_hal_hsem.c 15KB
usbd_core.c 15KB
FRA.c 15KB
new_protocol.c 14KB
adc.c 13KB
main.c 12KB
stm32h7xx_hal_i2c_ex.c 12KB
usbd_cdc_if.c 11KB
usbd_desc.c 11KB
stm32h7xx_hal_iwdg.c 11KB
stm32h7xx_hal_pcd_ex.c 10KB
i2c.c 9KB
stm32h7xx_it.c 9KB
SLAVE_I2C.c 8KB
balltest.c 8KB
current.c 8KB
crc_check.c 7KB
usart.c 5KB
myi2c.c 5KB
motor_tmr.c 5KB
usbd_ioreq.c 5KB
moni_spi.c 5KB
af_ois.c 5KB
key.c 4KB
live_test.c 4KB
gpio.c 3KB
led.c 3KB
usb_device.c 3KB
DW9800.c 3KB
stm32h7xx_hal_msp.c 2KB
iwdg.c 2KB
mp8859.c 2KB
DW9786S.c 2KB
Delay.c 2KB
mp6508.c 1KB
at24c256.c 369B
crcLib.cpp 16KB
stm32h7xx_hal_tim.crf 1.52MB
stm32h7xx_hal_i2c.crf 1.5MB
main.crf 1.5MB
stm32h7xx_hal_dma.crf 1.49MB
moni_i2c.crf 1.49MB
fra_s.crf 1.49MB
stm32h7xx_hal_uart.crf 1.49MB
new_protocol.crf 1.49MB
共 491 条
- 1
- 2
- 3
- 4
- 5
资源评论
- #完美解决问题
- #运行顺畅
- #内容详尽
- #全网独家
- #注释完整
wifibotπ
- 粉丝: 2
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- ssm校园快递一站式服务系统+jsp.zip
- ssm校园教务系统+vue.zip
- ssm校园美食交流系统+vue.zip
- 西门子s7 200smart与力士乐VFC3610变频器通讯原创可直接用于生产的程序,程序带注释,并附送触摸屏程序,有接线方式和设置,通讯地址说明等 程序采用轮询,可以后续根据要求适当修改后扩展
- ssm小型企业办公自动化系统的设计和开发+vue.zip
- ssm校园活动管理平台+vue.zip
- ssm小学生课外知识学习网站+vue.zip
- ssm物流管理系统设计与实现+jsp.zip
- ssm线上学习网站+vue.zip
- ssm线上旅行信息管理系统ssm+vue.zip
- ssm网上医院预约挂号系统+jsp.zip
- ssm网上花店设计+vue.zip
- 基于stm32的自动调速风扇(只是资料程序,无实物) 1)使用的芯片为STM32最小系统板来作为中心控制器 (2)驱动电机用L298N芯片驱动直流电机 (3)传感器模块使用热释电人体红外传感模块H
- ssm网上服装销售系统+jsp.zip
- ssm网络游戏公司官方平台设计与实现+jsp.zip
- ssm网络安全宣传网站设计+jsp.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功