我有一个代码,在F0发送一个字节为200波特,然后在10400发送5个字节。如何连续循环? [英] I Have A Code That Sends A Byte At F0 At 200 Baud And Then 5 Bytes At 10400.How I Loop It Continously?

查看:100
本文介绍了我有一个代码,在F0发送一个字节为200波特,然后在10400发送5个字节。如何连续循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个以200 bps发送字节OF然后应该在10400发送5个字节的代码,我需要连续循环这些数据,怎么办呢?我使用两个while循环一个用于在200发送单个字节然后再用另一个用于在10400发送5个字节的数据,当我启动程序时我可以看到OF被传输并且只有当我按下一个键时,我看到5个字节被转移,我想一次性完成所有这些。怎么做。这是随附的代码。



I have written a code that sends the byte OF at 200 bps and then should send 5 bytes at 10400, i need to loop this data continuously, how can this be done? i use two while loop one for sending single byte at 200 and then again another for sending 5 bytes of data at 10400, when i start the program i can see the OF being transmitted and only when i press a key, i see the 5 bytes being transferred, i want all these in a single shot. How to do it. Here is the attached code.

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <process.h>
#define true 1

int main()
{
   // int _kbhit();
   // int _getch();

   // Defining hexadecimal bytes to be sent a 200 bps for generation of low high sequence//
   unsigned char bytes_to_send[1];
   bytes_to_send[0] = 0xF0; /* 11110000b */
   unsigned char databytes_send[5];
   databytes_send[0] = 0x81;
   databytes_send[1] = 0x11;
   databytes_send[2] = 0xF1;
   databytes_send[3] = 0x11;
   databytes_send[4] = 0x04;

   /*int GetPressedKey()
   {
      int ret = 0;
      if (_kbhit())
      {
         ret = _getch();
      }
      return ret;
    }*/
   // Declare variables and structures//
   HANDLE hSerial;
   DCB dcbSerialParams = {0};
   COMMTIMEOUTS timeouts = {0};

   // Open the available serial port number//
   fprintf(stderr, "Opening serial port...");
   hSerial = CreateFile("COM4", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
   if (hSerial == INVALID_HANDLE_VALUE)
   {
      fprintf(stderr, "Check for port connections\n");
      return 1;
   }
   else
      fprintf(stderr, "OK\n");
   // Set device parameters (200 baud, 1 start bit, 1 stop bit, no parity)
   dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
   if (GetCommState(hSerial, &dcbSerialParams) == 0)
   {
      fprintf(stderr, "Error getting device state\n");
      CloseHandle(hSerial);
      return 1;
   }

   // Set baudrate to send bytes at 200 bps//
   dcbSerialParams.BaudRate = 200;
   dcbSerialParams.ByteSize = 8;
   dcbSerialParams.StopBits = ONESTOPBIT;
   dcbSerialParams.Parity = NOPARITY;
   if (SetCommState(hSerial, &dcbSerialParams) == 0)
   {
      fprintf(stderr, "Error setting device parameters\n");
      CloseHandle(hSerial);
      return 1;
   }

   // Set COM port timeout settings//
   timeouts.ReadIntervalTimeout = 50;
   timeouts.ReadTotalTimeoutConstant = 50;
   timeouts.ReadTotalTimeoutMultiplier = 10;
   timeouts.WriteTotalTimeoutConstant = 50;
   timeouts.WriteTotalTimeoutMultiplier = 10;
   if (SetCommTimeouts(hSerial, &timeouts) == 0)
   {
      fprintf(stderr, "Error setting timeouts\n");
      CloseHandle(hSerial);
      return 1;
   }
   while(true)
   {
      // Send specified text (remaining command line arguments)
      DWORD bytes_written = 0;
      fprintf(stderr, "sending bytes...\n");

      while(1)
      {
         //Transmit slow initialization data
         if (!WriteFile(hSerial, bytes_to_send, 1,&bytes_written, NULL))
            // break;

         fprintf(stderr, "%d bytes written\n", bytes_written);

            //check for key press event
            /*if (GetPressedKey()) */
              // break;
      }


      // Open the available serial port number//


      // Set device parameters (200 baud, 1 start bit, 1 stop bit, no parity)
      dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
      if (GetCommState(hSerial, &dcbSerialParams) == 0)
      {
         fprintf(stderr, "Error getting device state\n");
         CloseHandle(hSerial);
         return 1;
      }
      // Set baudrate to send bytes at 10400 bps 8 N 1//

      dcbSerialParams.BaudRate = 10400;
      dcbSerialParams.ByteSize = 8;
      dcbSerialParams.StopBits = ONESTOPBIT;
      dcbSerialParams.Parity = NOPARITY;
      if (SetCommState(hSerial, &dcbSerialParams) == 0)
      {
         fprintf(stderr, "Error setting device parameters\n");
         CloseHandle(hSerial);
         return 1;
      }

      // Set COM port timeout settings//
      timeouts.ReadIntervalTimeout = 50;
      timeouts.ReadTotalTimeoutConstant = 50;
      timeouts.ReadTotalTimeoutMultiplier = 10;
      timeouts.WriteTotalTimeoutConstant = 50;
      timeouts.WriteTotalTimeoutMultiplier =10;
      if (SetCommTimeouts(hSerial, &timeouts) == 0)
      {
         fprintf(stderr, "Error setting timeouts\n");
         CloseHandle(hSerial);
         return 1;
      }

      while(1)
      {
         //Transmit fast initialization data
         if (!WriteFile(hSerial, databytes_send, 5, &bytes_written, NULL))
            // break;

         fprintf(stderr, "%d bytes written\n", bytes_written);

         //Check for key press event
         /*   if (GetPressedKey())
         */
             break;
      }

      CloseHandle(hSerial);

      if (!WriteFile(hSerial, bytes_written,6, &bytes_written, NULL))
      {
         fprintf(stderr, "End of transmission\n");
         CloseHandle(hSerial);
         return 1;
      }
      fprintf(stderr, "%d bytes written\n", bytes_written);

      // Close serial port
      fprintf(stderr, "Closing serial port...");
      if (CloseHandle(hSerial) == 0)
      {
         fprintf(stderr, "Error\n");
         return 1;
      }
      fprintf(stderr, "OK\n");

      // exit normally
      return 0;
   }
}

推荐答案

如果没有按下任何键,那么 GetPressedKey 返回零,因此循环继续。您应该删除这两个函数,因为它们除了延迟代码之外什么也不做。
If no key has been pressed then GetPressedKey returns zero, so the loop continues. You should remove these two functions, as they do nothing except delay your code.


除了解决方案1(这是一个很好的建议,因为它将不再需要按键),你可以把你的整个过程包含在另一个根级别的循环中:

In addition to solution 1 (which is a good advice because it will remove the need to press a key), you can enclose your whole process in another, root level while loop:
while (true) {
   // Send specified text (remaining command line arguments)
   DWORD bytes_written = 0;
   fprintf(stderr, "sending bytes...\n");
   // ...
}



这将照顾持续运行的要求。

如果你想在循环中取消这个根级别,你最终可以进行按键测试(重新使用你的GetPressedKey方法)。



希望这有帮助。


This will take care of the continuously-running requirement.
You can eventually make a key-press test if you want to be able to cancel this root-level while loop (reusing your GetPressedKey method for the occasion).

Hope this helps.


这篇关于我有一个代码,在F0发送一个字节为200波特,然后在10400发送5个字节。如何连续循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆