C++ Com端口C++;读取0xFF

C++ Com端口C++;读取0xFF,c++,C++,我正在尝试从com端口读/写。 当我打开Com端口时,我使其不重叠。 一切正常,但当我读取0xFF字节时,它会将其视为EOF并完成读取。 我可以进行非重叠读取0xFF吗 这是我的密码: //Opening the com port: hComm = CreateFile( s.c_str(), // COM PORT GENERIC_READ | GENERIC_WRITE, //

我正在尝试从com端口读/写。 当我打开Com端口时,我使其不重叠。 一切正常,但当我读取0xFF字节时,它会将其视为EOF并完成读取。 我可以进行非重叠读取0xFF吗

这是我的密码:

//Opening the com port:

hComm = CreateFile( s.c_str(),                      // COM PORT
                    GENERIC_READ | GENERIC_WRITE,   //
                    0,                              // exclusive access
                    NULL,                           // no security
                    OPEN_EXISTING,                  // must be a port
                    0 ,                             // async i/o
                    NULL);                          // 
//Port init:
void initPort(int baud)
{
    uart_baud = baud;
    DCB dcb;
    dcb.DCBlength = sizeof(DCB);
    GetCommState(hComm, &dcb); // read current config
    dcb.BaudRate = baud;
    dcb.ByteSize = 8;
    dcb.StopBits = ONESTOPBIT;
    dcb.Parity = NOPARITY;
    dcb.fParity = FALSE;

    SetCommState(hComm, &dcb);
}

//Reading:(PS: private: char rx_packet[1024]; int rx_size;)
int readByte(int timeout)
{
    COMMTIMEOUTS CommTimeOuts;

    CommTimeOuts.ReadIntervalTimeout = 1;
    CommTimeOuts.ReadTotalTimeoutMultiplier  = timeout;
    CommTimeOuts.ReadTotalTimeoutConstant = 0;
    CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
    CommTimeOuts.WriteTotalTimeoutConstant   = 0;

    SetCommTimeouts(hComm, &CommTimeOuts);

    char  byte;
    DWORD bytes = 0;

    if (ReadFile(hComm, &byte, 1, &bytes, NULL))
    {
        return bytes == 1 ? byte : -1;
    }
    return -1;
}

void readPacket(void)
{
    int data_read;
    bool first_read = true;
    rx_size = 0;
    DWORD dwEventMask;
    DWORD ERR = 0;

    if (!SetCommMask(hComm, EV_RXCHAR)) return;
    if (!WaitCommEvent(hComm, &dwEventMask, NULL)) return;

    while (rx_size < MAX_PACKET_SIZE)
    {
        data_read = readByte(first_read ? 200 : 50);
        first_read = false;

        if (data_read == -1)return;

        rx_packet[rx_size] = (char)data_read;
        rx_size++;
    }
}

//Writing port:
bool writeByte(char byte)
{
    DWORD bytes = 0;
    WriteFile(hComm, &byte, 1, &bytes, NULL);
    return bytes == 1 ? true : false;
}

void RvcCommUART::writePacket(BYTE *data , UINT16 size)
{
    int tx_index = 0;
    while (tx_index < size)
    {
        if (writeByte(data[tx_index]) == false) return;

        tx_index++;
    }
}

//打开com端口:

hComm=CreateFile(s.c_str(),//COM端口
一般的读,一般的写//
0,//独占访问
NULL,//没有安全性
OPEN_EXISTING,//必须是端口
0,//异步i/o
空);//
//端口初始化:
无效初始端口(整数波特)
{
uart_波特=波特;
DCB-DCB;
dcb.DCBlength=sizeof(dcb);
GetCommState(hComm,&dcb);//读取当前配置
dcb.波特率=波特率;
dcb.ByteSize=8;
dcb.StopBits=一个StopBits;
dcb.奇偶性=无奇偶性;
dcb.fParity=FALSE;
设置通信状态(hComm和dcb);
}
//读取:(PS:private:char rx_数据包[1024];int rx_大小;)
int readByte(int超时)
{
通信超时通信超时;
CommTimeOuts.ReadIntervalTimeout=1;
CommTimeOuts.ReadTotalTimeout乘数=超时;
CommTimeOuts.ReadTotalTimeoutConstant=0;
CommTimeOuts.WriteTotalTimeout乘数=0;
CommTimeOuts.WriteTotalTimeoutConstant=0;
设置通信超时(hComm和通信超时);
字符字节;
DWORD字节=0;
if(ReadFile(hComm,&byte,1,&bytes,NULL))
{
返回字节==1?字节:-1;
}
返回-1;
}
void readPacket(void)
{
int数据读取;
bool first_read=真;
rx_大小=0;
DWORD dwEventMask;
德沃德误差=0;
如果(!SetCommMask(hComm,EV_RXCHAR))返回;
if(!WaitCommEvent(hComm,&dwEventMask,NULL))返回;
while(接收数据包大小<最大数据包大小)
{
数据读取=读取字节(第一次读取?200:50);
第一次读取=错误;
if(data_read==-1)返回;
rx_数据包[rx_大小]=(字符)数据读取;
rx_size++;
}
}
//写入端口:
布尔写字节(字符字节)
{
DWORD字节=0;
WriteFile(hComm,&byte,1,&bytes,NULL);
返回字节==1?真:假;
}
void RvcCommUART::writePacket(字节*数据,UINT16大小)
{
int-tx_指数=0;
while(tx_索引<大小)
{
if(writeByte(data[tx_index])==false)返回;
tx_索引++;
}
}

您的
char
似乎是有符号的(其符号依赖于实现),因此
0xFF
-1


使用
unsigned char
表示“字节”。

您的
char
似乎是有符号的(其符号依赖于实现),因此
0xFF
-1


使用
unsigned char
表示“字节”。

在FileRead函数中,字节是接收字节,字节是函数正在读取的字节数。。。这就是将其用作DWORD的方式;它读取的字节值发送到ReadPacket这就是问题所在,当我从char字节更改为unsigned char字节时,它起作用了,非常感谢FileRead函数字节是接收字节,字节是函数读取的字节数。。。这就是将其用作DWORD的方式;它读取的字节值会发送到readpacket这就是问题所在,当我从char字节更改为unsigned char字节时,它起作用了,非常感谢