没有来自串口的反应

时间:2014-05-07 03:07:53

标签: c++ serial-port visual-studio-2013

我应该如何将9个二进制文件发送到串口?

这有什么问题吗?我没有得到串口的反应。我该怎么做写文件?

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

using namespace std;

int main(){



unsigned char data[9];

data[0] = { 0x00 };
data[1] = { 0x5A };
data[2] = { 0x56 };
data[3] = { 0xFF };
data[4] = { 0x04 };
data[5] = { 0x00 };
data[6] = { 0x00 };
data[7] = { 0x00 };
data[8] = { 0xB3 };




DWORD buffer = 0;
HANDLE port;
DCB dcb = { 0 };
port = CreateFile("COM1",
    GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);
//DCB dcb;
DWORD m_baudrate = 9600, m_bytesize = 8, m_parity = 0, m_stopbit = 1, ok, m_useRtsCts = 0;
dcb.DCBlength = sizeof(dcb);
GetCommState(port, &dcb);
dcb.BaudRate = m_baudrate;
dcb.ByteSize = (BYTE)m_bytesize;
dcb.Parity = m_parity;
dcb.StopBits = m_stopbit;
if (m_useRtsCts)
    dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fNull = FALSE;
dcb.fAbortOnError = FALSE;
ok = SetCommState(port, &dcb);
if (port){

    BuildCommDCB("COM1:9600,n,8,1", &dcb);
    cout << "Hello" << endl;
    PurgeComm(port, PURGE_RXCLEAR | PURGE_TXCLEAR);
    WriteFile(port, data, sizeof(data), &buffer, 0);

}
else{
    cout << "World" << endl;
}

CloseHandle(port);
system("Pause");
return 0;

}

我可以发送数据,但二进制文件无法打开灯。打开所有内容的十六进制代码是

00 5A 56 FF 03 00 00 00 B2

在我使用CommUart asistant打开到端口之后,然后事情才能起作用。谁能告诉我那里的开放端口有什么问题?谢谢!

谁能告诉我出了什么问题?

但是使用这组代码我可以正常运行吗?我可以在那里知道我的设置在哪里错了。

#include <iostream>
#include <corewindow.h>

using namespace std;

int main(){

//unsigned char data[9] = {0,90,86,255,3,0,0,0,178};

unsigned char data[9];

data[0] = { 0x00 };
data[1] = { 0x5A };
data[2] = { 0x56 };
data[3] = { 0xFF };
data[4] = { 0x03 };
data[5] = { 0x00 };
data[6] = { 0x00 };
data[7] = { 0x00 };
data[8] = { 0xB2 };




DWORD buffer = 0;
HANDLE port;
DCB dcb = { 0 };
port = CreateFile("COM1",
    GENERIC_READ | GENERIC_WRITE,
    0,
    0,
    OPEN_EXISTING,
    0,
    0);
if (port == (HANDLE)-1)
{
    return 0;
}

SetupComm(port, 1024, 1024);

FillMemory(&dcb, sizeof(dcb), 0);
dcb.DCBlength = sizeof(dcb);
BuildCommDCB("9600,n,8,1", &dcb);


if (!SetCommState(port, &dcb))
{
    return 0;
}

COMMTIMEOUTS to;
memset(&to, 0, sizeof(to));
to.ReadIntervalTimeout = 100;
to.ReadTotalTimeoutMultiplier = 10;
to.ReadTotalTimeoutConstant = 10;
SetCommTimeouts(port, &to);
PurgeComm(port, PURGE_TXCLEAR | PURGE_RXCLEAR);
if (port){


    cout << "Hello" << endl;
    WriteFile(port, data, sizeof(data), &buffer, 0);


    system("pause");
    data[0] = { 0x00 };
    data[1] = { 0x5A };
    data[2] = { 0x56 };
    data[3] = { 0xFF };
    data[4] = { 0x04 };
    data[5] = { 0x00 };
    data[6] = { 0x00 };
    data[7] = { 0x00 };
    data[8] = { 0xB3 };

    WriteFile(port, data, sizeof(data), &buffer, 0);


}
else{
    cout << "World" << endl;
}

CloseHandle(port);
system("pause");
return 0;
}

3 个答案:

答案 0 :(得分:1)

代码无法运行。它不应该编译。

Writefile的第一个参数至少是文件句柄,第三个参数是要写入的字节数。

 WriteFile(
        hcomm,
        "the string",
        strlen ("the string"),

此外,您可能需要设置串口的特性。这可以使用命令行或控制面板完成,但在打开文件后,程序通常可以自己处理细节:

    DCB dcb;
    dcb.DCBlength = sizeof(dcb);
    GetCommState(m_hCom, &dcb);   /* retrieve current settings */
    dcb.BaudRate = m_baudrate;  /* alter bits per second */
    dcb.ByteSize = (BYTE)m_bytesize;   /* bits per character */
    dcb.Parity = m_parity;
    dcb.StopBits = m_stopbit;
    if (m_useRtsCts)
            dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
    dcb.fDtrControl = DTR_CONTROL_DISABLE;   // no handshake
    dcb.fNull = FALSE;              /* don't strip NULs */
    dcb.fAbortOnError = FALSE;      /* keep going after errors */
    ok=SetCommState(hcomm, &dcb);

附录
要将九个二进制值写为字符值,这些技术中的任何一种都可以使用。所有都依赖于将二进制数表示为字节:

char data[] = "\000\132\124\000\003\000\000\000\261";
WriteFile(hcomm, data, sizeof data, NULL, NULL);

char data[] = {0, 0132, 0124, 0, 3, 0, 0, 0, 0261};
WriteFile(hcomm, data, sizeof data, NULL, NULL);

char data[] = {0, 0x5a, 0x54, 0, 3, 0, 0, 0, 0xb1};
WriteFile(hcomm, data, sizeof data, NULL, NULL);

答案 1 :(得分:0)

设置心灵感应模式,我假设TS想要发送9位数据。实现它的典型方法是使用奇偶校验作为第9个数据位。主要思想是使奇偶校验错误表示第9位设置。详细信息取决于可用的硬件广告驱动程序。

如果uart支持MARK / SPACE平价,那么这样的方法就可以了:

send_9_bits(unsigned char data, bool ninth_bit) {
    if(ninth_bit) set_parity_mark();
    else          set_parity_space();
    uart_write(data);
}

用接收端分析奇偶校验错误状态:

unsigned short receive_9_bits() {
    unsigned short data = uart_read();
    bool ninth_bit;
    if (even_parity(data & 0x00ff) {
        ninth_bit = PARERR(data >> 8)? 1: 0;
    } else {
        ninth_bit = PARERR(data >> 8)? 0: 1;
    }
    return (data & 0x0ff) | (ninth_bit << 8);
}

答案 2 :(得分:0)

波特率为0时,串口无法工作。波特率,字节大小,奇偶校验和停止位必须全部设置为与您正在通信的设备使用的设置相匹配。并且串行硬件仅支持某些标准值。