输入未从ComPort注册

时间:2016-05-31 22:36:14

标签: windows batch-file arduino serial-port

好的,现在这个真的让我很难过。我正在使用Arduino和距离传感器在计算机上进行某些操作。我在我的arduino上做的是当我的手在一定距离输出1到COM4。这是我的Arduino代码

#define trigPin 12
#define echoPin 13
#define voltage 11
void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(voltage, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  digitalWrite(voltage, HIGH);
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);  distance = (duration/2) / 29.1;

  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance > 18) {
    int var = 0;
    Serial.print(var);
  } else {
    int var = 1;
    Serial.print(var);
  }
}

我有一个批处理文件然后读取COM4,然后在输入为1时执行某些操作。这是批处理代码

@echo off
set var=0
echo "hi"
mode COM4 BAUD=115200 PARITY=n DATA=8
:start
set "key="
for /F "delims=" %%K in ('xcopy /W "%~F0" "%~F0" ^< COM4 2^> NUL') do (
   if not defined key set "key=%%K"
)
set /P "line=" < COM4
set "line=%key:~-1%"
echo %line%
if "%line%" == "1" ( 
    if "%var%" == "0" (
        goto monitoroff
    )
    if "%var%" == "1" (
        goto monitoron
    )
)
goto start
:monitoron
for /l %%x in (1, 1, 50) do (
    echo %%x
    "C:\Users\%USERNAME%\Documents\nircmd-x64\nircmd.exe" monitor on
)
set var=0
goto start 
:monitoroff
"C:\Users\%USERNAME%\Documents\nircmd-x64\nircmd.exe" cmdwait 1 monitor off
set var=1
goto start 

这是问题出现的地方。

之后,我的代码冻结了
for /F "delims=" %%K in ('xcopy /W "%~F0" "%~F0" ^< COM4 2^> NUL') do (
   if not defined key set "key=%%K"
)

这让我感到困惑,因为当我在我的arduino

上运行此代码时
void setup() {
   Serial.begin(115200);

}

void loop() {
    int var = 0;
    Serial.print(var);
}

我的批处理文件echos 0,一切运行顺利。我没看到两个Arduino代码之间的区别。任何帮助都会很棒

0 个答案:

没有答案