gpt4 book ai didi

c# - 在 visual studio 中将串行端口应用程序从 C# 转换为 C (ERROR_INVALID_PARAMETER) 87

转载 作者:太空宇宙 更新时间:2023-11-04 03:05:11 27 4
gpt4 key购买 nike

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

我可以通过以下设置使用 Docklight( super 终端程序)发送数据,并且可以验证我是否正在通过 Itronix LogicPort 逻辑分析仪接收数据。波特率:9600奇偶性:无字节大小:8停止位:1

我的 C 程序有 2 个问题:1) 有时 CreateFile 返回 INVALID_HANDLE_VALUE另一个我似乎无法解决的问题是获取通讯状态()设置通讯状态()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;
}

最佳答案

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

关于c# - 在 visual studio 中将串行端口应用程序从 C# 转换为 C (ERROR_INVALID_PARAMETER) 87,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6231699/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com