c调用Windows API串口通信解决办法

c调用Windows API串口通信
想要向串口发送命令,显示串口的返回结果,但是我的程序读串口读出来的数据不是我想要的!!!!有对这方面熟悉的前辈指点下,谢谢!!)


C/C++ code

#include <stdio.h>
#include <windows.h>
int main(void)
{   
    int i,w;
    char readBuffer[100];
    char lpOutBuffer[6]={0x23,0x30,0x31,0x30,0x0d,0x0a};\\向串口发出的命令  输入#010---->返回(>+03.267)
    DWORD dwBytesWrite=100;
    COMSTAT ComStat;
    DWORD dwErrorFlags;
    TCHAR str[100];
    DWORD nWantRead=100;
    DWORD nReadRead;
    //LPOVERLAPPED m_OverlappedRead;
    int s;
    DWORD i_size=1024;
    DWORD o_size=1024;
    HANDLE hCom=CreateFile("COM1",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    if(hCom == INVALID_HANDLE_VALUE)
           {
                MessageBox(NULL,"打开串口失败!!","OK",MB_OK);
           }
    MessageBox(NULL,"打开串口成功!!","连接7017",MB_OK);
    if(!SetupComm(hCom,1024,1024))
    {
        printf("设置缓冲区失败!!\n");
        //CloseHandle(hCom);
        return -1;
    }
    printf("设置缓冲区成功!!\n");
    COMMTIMEOUTS TimeOuts;
     TimeOuts.ReadIntervalTimeout=100;
     TimeOuts.ReadTotalTimeoutMultiplier=100;
     TimeOuts.ReadTotalTimeoutConstant=1000;
     TimeOuts.WriteTotalTimeoutMultiplier=50;
     TimeOuts.WriteTotalTimeoutConstant=2000;
     SetCommTimeouts(hCom,&TimeOuts);
     PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
     /////////////////////////////////////////////
     //设置波特率
     DCB dcb;

    GetCommState(hCom, &dcb);
    dcb.BaudRate = CBR_9600;    //波特率
    dcb.ByteSize = 8;     // number of bits/byte, 4-8 指定端口当前使用的数据位
    dcb.Parity = 0;     // 0-4=no,odd,even,mark,space 指定端口当前使用的奇偶校验方法
    dcb.StopBits = 1;    //指定端口当前使用的停止位数
    SetCommState(hCom,&dcb); 
    //写数据 
   w=WriteFile(hCom,lpOutBuffer,dwBytesWrite,& dwBytesWrite,NULL);
    if(!w)
    {
          MessageBox(NULL,"写串口失败!!","连接7017",MB_OK);
          return;
    }
    MessageBox(NULL,"写串口成功!!","连接7017",MB_OK);
    Sleep(100);
    PurgeComm(hCom,PURGE_TXCLEAR);
    ClearCommError(hCom,&dwErrorFlags,&ComStat);
    //读缓冲区的数据 
    s=ReadFile(hCom,readBuffer,50,&nReadRead, NULL);
    printf("%d\n",s);
    if(s==0)
    {
         MessageBox(NULL,"读取串口失败!!","连接7017",MB_OK);
         return; 
    }
    else
    MessageBox(NULL,"读取串口成功!!","连接7017",MB_OK);
    for(i=0;i<25;i++)
    {
        printf("%d\n",readBuffer[i]);
    }
    CloseHandle(hCom);
    system("PAUSE");
    }



------解决方案--------------------
w=WriteFile(hCom,lpOutBuffer,dwBytesWrite,& dwBytesWrite,NULL);
lpOutBuffer只有6个字节,dwBytesWrite值为100,会不会有问题.
------解决方案--------------------
char lpOutBuffer 改为 byte lpOutBuffe