c - 如何在 C 语言中通过串行端口发送 0xff 字节?

标签 c windows serial-port

所以我想通过串行电缆发送一个 0xff 字节,触发计算机通过以太网电缆发回数据。代码如下

#include <stdio.h>
#include <stdlib.h>
#include<windows.h>
#include<winsock2.h>
#include <string.h>
#include <time.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define BUFLEN 4097 //Max length of buffer
#define PORT 9090 //The port on which to listen for incoming data

int main()
{
    BOOL Status;
    HANDLE ComPort;
    char str[20] = "\\\\.\\COM";
    char input[4] = "";
    printf("Enter COM port: ");
    fgets(input,4,stdin);
    input[strcspn(input, "\n")] = 0;
    strncat(str,input, 5);
    ComPort = CreateFile((const char*)str,                //port name
                      GENERIC_READ | GENERIC_WRITE, //Read/Write
                      0,                            // No Sharing
                      NULL,                         // No Security
                      OPEN_EXISTING,// Open existing port only
                      0,            // Non Overlapped I/O
                      NULL);        // Null for Comm Devices

  if (ComPort == INVALID_HANDLE_VALUE){
      printf("\nError in opening serial port");
      exit(EXIT_FAILURE);}
  else{
      printf("opening serial port successful");
  }
    DCB dcbSerialParams = { 0 }; // Initializing DCB structure
    dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
    Status = GetCommState(ComPort, &dcbSerialParams);

    dcbSerialParams.BaudRate = CBR_115200;  // Setting BaudRate = 115200
    dcbSerialParams.ByteSize = 8;         // Setting ByteSize = 8
    dcbSerialParams.StopBits = ONESTOPBIT;// Setting StopBits = 1
    dcbSerialParams.Parity   = NOPARITY;  // Setting Parity = None

    SetCommState(ComPort, &dcbSerialParams);
    COMMTIMEOUTS timeouts = { 0 };

    timeouts.ReadIntervalTimeout         = MAXWORD; // in milliseconds
    timeouts.ReadTotalTimeoutConstant    = 10000; // in milliseconds
    timeouts.ReadTotalTimeoutMultiplier  = MAXWORD; // in milliseconds
    timeouts.WriteTotalTimeoutConstant   = 0; // in milliseconds
    timeouts.WriteTotalTimeoutMultiplier = 0; // in milliseconds

    char lpBuffer[] = {255};
    DWORD dNoOFBytestoWrite;         // No of bytes to write into the port
    DWORD dNoOfBytesWritten;     // No of bytes written to the port
    dNoOFBytestoWrite = sizeof(lpBuffer);

    SOCKET s;
    struct sockaddr_in server, si_other;
    int slen , recv_len;
    char buf[BUFLEN];
    WSADATA wsa;

    slen = sizeof(si_other) ;

    //Initialise winsock
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        exit(EXIT_FAILURE);
    }

    //Create a socket
    if((s = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
    {
        printf("\nCould not create socket : %d" , WSAGetLastError());
        exit(EXIT_FAILURE);
    }

    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons( PORT );

    //Bind
    if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
    {
        printf("\nBind failed with error code : %d" , WSAGetLastError());
        exit(EXIT_FAILURE);
    }
    printf("\nBinding successful");
    DWORD timeout = 0.2 * 1000;//0.2 second ethernet timeout
    setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof timeout);

    FILE * fPtr;
    char filename[50] = "fpgadat.bin";
    fPtr = fopen((const char*) filename, "wb");//Open file in wb (write) mode.

    // fopen() return NULL if last operation was unsuccessful
    if(fPtr == NULL)
    {
        // File not created hence exit
        printf("Unable to create file.\n");
        exit(EXIT_FAILURE);
    }

    while(1)
    {
        time_t start = time(NULL);
        Status = WriteFile(ComPort,        // Handle to the Serial port
                   lpBuffer,     // Data to be written to the port
                   dNoOFBytestoWrite,  //No of bytes to write
                   &dNoOfBytesWritten, //Bytes written
                   NULL);

    if(Status == 0){
        printf("\nFailed to write to serial port");
        return 0;
    }else{
    printf("\nData successfully written to serial port");
    }
        printf("\nWaiting for data from fpga...");
        fflush(stdout);

        memset(buf,'\0', BUFLEN);//clear the buffer by filling null, it might have previously received data
        //try to receive some data, this is a blocking call
        if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == SOCKET_ERROR)
        {
            printf("\nEthernet connection time-out");
            break;

        }

    fwrite(buf , 1 , sizeof(buf) , fPtr );
    printf("%.2f\n", (double)(time(NULL) - start));
    }
    //close file descriptors
    fclose(fPtr);
    CloseHandle(ComPort);
    closesocket(s);
    WSACleanup();

}

我似乎总是在以太网上遇到超时错误。我认为这是因为我没有发送必要的 0xff 字节来触发第二台计算机通过以太网发回数据。可能是什么问题?

最佳答案

在使用此代码之前,我不确定您在做什么,但我建议使用 Com0com、PortMon 或 Wire Shark 来监视您的 Com 端口和以太网。它会让你的生活更轻松。

此外,在您的问题中,您应该告诉我们代码的结果是什么,以便我们更好地了解发生了什么。

链接https://www.xanthium.in/Serial-Port-Programming-using-Win32-API 与您正在尝试执行的操作相同。

您确定选择了正确的 com 端口吗?

您的代码显示您正在尝试写入连接到另一台正在监听串行端口的 PC 的串行端口。您确定另一台 PC 正在使用相同的波特率和控制线进行监听吗? (它可能设置为默认值。)您应该首先尝试在两台 PC 上使用 Terminal.exe 或类似的东西,看看两台 PC 之间的串行通信是否正常工作。接下来,您应该创建一个仅发送到 com 端口的应用程序,并使用终端模拟器在另一台 PC 上监听,看看它是否到达。

关于c - 如何在 C 语言中通过串行端口发送 0xff 字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59176419/

相关文章:

c - 每行重复输出

c - 将 double 转换为 float 后值不正确

windows - 为什么我没有将ntdll正确加载到windbg中,但却下载了windbg x86?

vb6 - 如何枚举计算机上可用的 COM 端口?

c++ - 使用 Windows API 操作 RS 端口

char 数组分组在一个数组中

c - 是一个==*一个??关于指针的查询

c# - 在打开加密文件系统的情况下从 C# 创建一个新目录

java - IntelliJ IDEA 2016.1(64) : Unsupported java version Cannot start under Java 1. 7.0_79-b15 : Java 1. 需要8或更高版本

serial-port - 实现可靠通信的简单算法