在Visual Studio中将串口应用程序从C#转换为C(ERROR_INVALID_PARAMETER)87

时间:2011-06-03 19:25:01

标签: c# c visual-studio-2008 serial-port

我正在尝试用C编写一个应用程序,通过COM端口与设备进行通信。

我能够使用Docklight(类似程序的超级终端)使用以下设置发送数据,我可以通过Itronix LogicPort逻辑分析器验证我是否正在接收数据。 波特率:9600 奇偶校验:无 字节大小:8 停止位:1

我的C程序遇到2个问题: 1)有时CreateFile返回INVALID_HANDLE_VALUE 而我似乎无法解决的另一个问题是 GetCommState() SetCommState() SetCommTimeouts() 总是返回ERROR_INVALID_PARAMETER 87(0x57)

由于我无法解决这个问题,我在C#中尝试了一个程序并且运行正常,但是我的程序必须在C中才能与我的其余代码进行通信。该程序还通过套接字与C中的其他程序通信。

在设备管理器中,我要与之通话的设备在端口(COM& LPT)下列为COM6

任何帮助将不胜感激!

以下是两个程序的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {

    byte[] TxBuffer = new byte[20];

    SerialPort CommPort = new SerialPort("COM6", 9600, Parity.None, 8, StopBits.One);

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        CommPort.Open();
        TxBuffer[0] = (byte)'|';
        TxBuffer[1] = 0;
        TxBuffer[2] = 0;
        TxBuffer[3] = 0;
        TxBuffer[4] = 1;
        TxBuffer[5] = 100;
        TxBuffer[6] = 1;
        TxBuffer[7] = 6;
        TxBuffer[8] = (byte)'|';
        TxBuffer[9] = 1;

    }

    private void button1_Click(object sender, EventArgs e)
    {
        CommPort.Write(TxBuffer, 0, 10);
    }
  }
}

,C代码是:

某处

 struct serial_dev {
         HANDLE hSerial;
 };        

  struct serial_dev sdev;  //global

       //somewhere in main
   if (init_serial(&sdev, com_file) == -1)
       return -1;
   write_ser(&sdev, buffer, buf_size);
   close_serial(&sdev);

       // in serial.c

    int init_serial(struct serial_dev *sdev, char *port_name)
    {
        DCB dcbSerialParams;
        COMMTIMEOUTS timeouts;
        int error = 0;

        memset(&dcbSerialParams, 0, sizeof(dcbSerialParams));
        memset(&timeouts, 0, sizeof(timeouts));

        sdev->hSerial = CreateFile("COM6", GENERIC_READ | GENERIC_WRITE, 0, NULL,  OPEN_EXISTING, 0, NULL);

        if (sdev->hSerial == INVALID_HANDLE_VALUE) 
        {
            error = GetLastError();
            close_serial(sdev);
            fprintf(stderr, "error: could not open serial port (error:%d)\n", error);
            return -1;
        }


        memset(&dcbSerialParams, 0, sizeof(dcbSerialParams));
        dcbSerialParams.DCBlength = sizeof(dcbSerialParams);

        if (!GetCommState(sdev->hSerial, &dcbSerialParams)) 
        {
            close_serial(sdev);
            error = GetLastError();
            fprintf(stderr, "error: could not get serial port state (%d)\n", error);
            return -1;
        }

        dcbSerialParams.BaudRate = CBR_9600;
        dcbSerialParams.ByteSize = 8;
        dcbSerialParams.StopBits = ONESTOPBIT;
        dcbSerialParams.Parity = NOPARITY;

        if (!SetCommState(sdev->hSerial, &dcbSerialParams)) 
        {
            close_serial(sdev);
            error = GetLastError();
            fprintf(stderr, "error: could not set serial port state (%d)\n", error);
            return -1;
        }


        timeouts.ReadIntervalTimeout = 50;
        timeouts.ReadTotalTimeoutConstant = 200;
        timeouts.ReadTotalTimeoutMultiplier = 10;
        timeouts.WriteTotalTimeoutConstant = 50;
        timeouts.WriteTotalTimeoutMultiplier = 10;

        if (!SetCommTimeouts(sdev->hSerial, &timeouts)) 
        {
            close_serial(sdev);
            error = GetLastError();
            fprintf(stderr, "error: could not set serial port timeouts (%d)\n", error);
            return -1;
        }



        return 0;
    }

    void close_serial(struct serial_dev *sdev)
    {
            CloseHandle(sdev->hSerial);
    }

    int write_ser(struct serial_dev *sdev, void *buf, size_t len)
    {
        DWORD written;
        BOOL success;


        success = WriteFile(sdev->hSerial, buf, len, &written, NULL);
        if(!success || written == 0)
        {
            printf("error writing to serial\n");
            return -1;
        }
        printf("writing to serial OK!\n");
        return written;
    }

1 个答案:

答案 0 :(得分:2)

我知道了......我从我的项目设置中删除了“UNICODE”和“_UNICODE”预处理器宏,并将其设置为“未设置”找到答案:http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1a77c595-10a5-4f14-8512-c67535c53a08/