c++ - 使用 SCSI 传递的 ioctrl

标签 c++ c linux scsi

使用 Windows,我可以使用以下简化代码轻松地与我的 USB 设备通信:

DWORD dwJunk;    // discard results from DeviceIOControl()
int   iReply;
char  cBuffer[100];
// cBuffer is initialized here.
HANDLE hDevice; // handle to the drive to be examined 
CString sDrive = _T(\\\\.\\H:); // drive H: for this test
hDevice = CreateFile(sDrive,            // drive to open
   GENERIC_READ | GENERIC_WRITE,        // read and write access to the drive
   FILE_SHARE_READ | FILE_SHARE_WRITE,  // share mode           
   NULL,                                // default security attributes
   OPEN_EXISTING,                       // disposition
   0,                                   // file attributes
   NULL);                               // do not copy file attributes

iReply = DeviceIoControl(hDevice, IOCTL_SCSI_PASS_THROUGH_DIRECT, &cBuffer, sizeof(cBuffer), &cBuffer, sizeof(cBuffer), &dwJunk, (LPOVERLAPPED)NULL);

我试图在 Linux 中做同样的事情,但无法找出 ioctrl() 参数,或者更好地放置结构。代码片段将非常感激。谢谢。

最佳答案

不幸的是,我使用您的链接修改的代码没有返回任何结果。这是我使用的剥离代码。 ioctl() 返回没有错误:

#define DEF_TIMEOUT 5000 // 5 seconds
char cDiskName[] = "/dev/sg3";
int fd = open(cDiskName, O_RDWR);
if (fd < 0)
{
  printf("Open error: %s, errno=%d (%s)\n", cDiskName, errno, strerror(errno));
  return 1;
}

unsigned char turCmbBlk[] = {0x00, 0, 0, 0, 0, 0};
struct sg_io_hdr io_hdr;

unsigned char cIOBuffer[100];

// buffer initialization code omitted

memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
io_hdr.interface_id = 'S';
io_hdr.cmd_len = sizeof(turCmbBlk);
io_hdr.mx_sb_len = sizeof(cIOBuffer);
io_hdr.dxfer_direction = SG_DXFER_NONE;
io_hdr.cmdp = turCmbBlk;
io_hdr.sbp = cIOBuffer;
io_hdr.timeout = DEF_TIMEOUT;
if (ioctl(fd, SG_IO, &io_hdr) < 0)
{
  printf("ioctl error: errno=%d (%s)\n", errno, strerror(errno));
}

// Code returned here without any errors but cIOBuffer remains unchanged.

也许调用需要不同的请求代码?

关于c++ - 使用 SCSI 传递的 ioctrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14798798/

相关文章:

c - 打印 inet_ntoa 函数中的段错误

c++ - 迁移到VS2013比较运算符错误C2678 :binary '==' no operator

python - 通过嵌入式 Python 调用 C++ 代码

c++ - 我需要一种流式传输字符串的替代方法。 C++

c - 理解 C 命名空间

c - c中变量的类型

linux - 无法理解进程的 strace

xml - 如何使用 SED (linux) 从 XML 文件中删除 CDATA

c - 在 Linux 上从 C 程序直接访问没有 FS 的硬盘

c++ - 在 Windows 上从源构建点云库的 boost 时出错