繁体   English   中英

C ++从外部设备读取数据-连接:OPTO-USB

[英]C++ reading data from external device - connection: OPTO-USB

我一直在寻找有关将Sylvac制作的千分表与用C ++编写的程序连接的一些信息(我不确定是否这样称呼)。 通过OPTO-USB电缆(编号926.6621.10)进行连接。 数据格式:X.XX(以毫米为单位)。

我尝试使用MSDN函数: CreateFileReadFile 没有错误,也没有任何值。 传输设置应该正确(我在电缆附带的CD上找到了它们)。

有人知道如何从百分表读取数据吗? 我试图以几种方式转换数据。 每个功能下的“ cout”用于查找程序停止的位置。 我是使用Internet上的一些代码编写的。

编辑于2014-01-10

我的程序仍然有问题。 千分表在显示屏上显示“ 0,04”,因此我希望程序中的“ buffor”变量具有相同的值,但没有任何作用。 甚至“读取”也为零。

我的代码:

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    LPBYTE buffor = new BYTE[64];
    *buffor = 1;
    HANDLE file;
    COMMTIMEOUTS timeouts;
    DWORD read;
    DCB port;
    char port_name[128] = "\\\\.\\COM3";
    cout << "pierwszy: " << endl;

    // open the comm port.
    file = CreateFile((port_name), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    if ( file == INVALID_HANDLE_VALUE ) {
        cout << "1 1";
        return 1;
    }
    else
    {
        // get the current DCB, and adjust a few bits to our liking.
        memset(&port, 0, sizeof(port));
        port.DCBlength = sizeof(port);
        if ( !GetCommState(file, &port))
            cout << " 2 ";
        if (!BuildCommDCB(("baud=4800 parity=e data=7 stop=2"), &port))
            cout << " 3 ";
        if (!SetCommState(file, &port))
            cout << " 4 ";

        port.fDtrControl = DTR_CONTROL_ENABLE;
        port.fRtsControl = RTS_CONTROL_ENABLE;

        BOOL success;
        success = GetCommTimeouts(file, &timeouts);
        if (!success)
        {
            cout << "error";
            CloseHandle(file);
            return 1;//INVALID_HANDLE_VALUE;
        }
        timeouts.ReadIntervalTimeout = 1000;
        timeouts.ReadTotalTimeoutConstant = 1000;
        timeouts.ReadTotalTimeoutMultiplier = 0;
        timeouts.WriteTotalTimeoutConstant = 1000;
        timeouts.WriteTotalTimeoutMultiplier = 0;
        success = SetCommTimeouts(file, &timeouts);
        if (!success)
        {
            cout << "error";
            CloseHandle(file);
            return 1;//INVALID_HANDLE_VALUE;
        }

        if (!EscapeCommFunction(file, CLRDTR))
           cout << " 7 ";
        Sleep(200);
        if (!EscapeCommFunction(file, SETDTR))
           cout << " 8 ";

        // check for data on port and display it on screen.
        if(ReadFile(file, &buffor, sizeof(buffor), &read, NULL))
        {
            cout << "buffor: " << buffor << endl;
            cout << "read = " << read << endl;
            for (int i = 0; i < read; i++ )
                cout << i+1 << ". " << buffor[i] << endl;

            cout << "." << endl;
        }
        else
            cout << "error read";

        delete buffor;
        CloseHandle(file);
    }

    system("pause");
    return 0;
}

我将结果放在控制台中: console

预期变量:buffor-> 0,04; 读取->读取字节数

输出变量:buffor ???? 没有?; 读取= 0

借助注释中提供的其他信息,我们可以开始对此进行诊断。 对您来说,实际的问题是,在“ odczytane”一词后会打印垃圾。 顺便说一句,这些信息应该在第一篇文章中。

首先,您需要确保将程序设置为执行阻止读取。 您已经设置了一些非常短的读取超时,这意味着每当您尝试从文件中读取数据时,程序都将很快放弃。 您的程序可能在准备好任何数据之前就放弃了。 尝试通过执行类似于以下代码的操作,将每个时间增加到1000 ms:

    BOOL success;
    success = GetCommTimeouts(port, &timeouts);
if (!success)
{
    fprintf(stderr, "Error: Unable to get comm timeouts.  Error code 0x%x.\n", GetLastError());
    CloseHandle(port);
    return INVALID_HANDLE_VALUE;
}
timeouts.ReadIntervalTimeout = 1000;
timeouts.ReadTotalTimeoutConstant = 1000;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 1000;
timeouts.WriteTotalTimeoutMultiplier = 0;
success = SetCommTimeouts(port, &timeouts);
if (!success)
{
    fprintf(stderr, "Error: Unable to set comm timeouts.  Error code 0x%x.\n", GetLastError());
    CloseHandle(port);
    return INVALID_HANDLE_VALUE;
}

您可能需要根据设备的详细信息来延长这些时间。

这里的另一个基本问题是您没有仔细考虑ReadFile返回的数据格式。

ReadFile是用于读取任何类型的数据(即二进制)的通用函数。 据我所知,它不会在数据末尾附加一个空终止符。 (不应该这样,因为您可能希望读取0个字节。)您需要在调用ReadFile之后查看read的值,以了解读取了多少字节。

诸如printf和cout之类的东西需要传递一个以空值结尾的字符串。

打印sizeof(read)总是给您4,因为read仅被定义为32位整数( DWORD )。

综合所有这些知识,这是我们可以调用ReadFile并打印结果的一种合理方法。 我不知道返回的数据是ASCII还是二进制,所以我只打印每个字节的数字值:

if(ReadFile(file, buffor, sizeof(buffor), &read, NULL))
{
  cout << "bytes read:" << read << endl;
  for (int i = 0; i < read; i++)
  {
    cout << buffor[i] << " " << endl;
  }
}
else
{
  cout << "ReadFile failed" << endl;
}

我尚未仔细研究此特定串行设备的工作方式,因此,如何设置它可能会导致其不发送数据。

如果仍然遇到问题,请将程序简化为少于100行的单个文件,并提供完整的代码以及输出和预期输出。 http://www.sscce.org/

我一直在寻找解决方案很长时间,直到找到为止。 答案很简单! 有两种方法可以将数据从该千分表传输到计算机:

  1. 您可以使用函数SetCommMask设置事件掩码,然后等待使用函数WaitCommEvent声明的事件。

  2. 您可以使用WriteFile将某些字符发送到千分表,然后使用ReadFile读取。

我的代码:

if(WriteFile(Sensor, " ", 1, &read, NULL))
    ReadFile(Sensor, buffer, sizeof(buffer), &read, NULL);

缓冲区是LPBYTE所以我必须对其进行转换:

char buf[4];

for (int i = 0; i < 4; i++)
    buf[i] = buffer[i];

double buff = 0;
buff = atof(buf);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM