小编For*_*vin的帖子

USB到串行UART的位冲击

我刚刚购买了UM232R USB串行UART开发模块,该模块使用FT232RL芯片通过USB模拟类似UART的接口.
我实际上只是为了一个非常简单的目的而买了这个复杂的模块:触发一个我自己构建的非常简单的LED电路.所以我想要的是"咬一下"模块的第一个可以进位的引脚"CB0"(引脚23)[参见数据表中的第8/9页].使用C++或AHK(或者Python,即使我真的不知道它),它并不重要.它需要在Windows上运行.

到目前为止我所尝试的内容:
我找到了一个关于如何对FTDI设备进行bit-bang的精彩教程. 但首先,我安装了VCP驱动程序,或者更准确地说明了表格右侧的"设置可执行文件".这不仅安装了VCP驱动程序,还安装了D2XX驱动程序.然后我下载了D2XX驱动程序作为zip(一个用于Windows).

好吧:

  • 我创建了一个新的Visual C++项目(带有预编译头的Win32控制台应用程序).
  • 我在项目文件夹中解压缩了D2XX驱动程序.
  • 我将ftd2xx.h头文件添加到项目中.
  • 我从上面提到的教程中获取了这段代码并将其修改为:

(我实际上只是添加了windows.h,stdafx.h并修改为#include <ftd2xx.h> #include "ftd2xx.h")

/* 8-bit PWM on 4 LEDs using FTDI cable or breakout.
   This example uses the D2XX API.
   Minimal error checking; written for brevity, not durability. */

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "ftd2xx.h"

#define LED1 0x08  /* CTS (brown wire on FTDI cable) …
Run Code Online (Sandbox Code Playgroud)

c++ serial-port visual-c++ uart ftdi

5
推荐指数
1
解决办法
1887
查看次数

cURL:处理多个异步请求

我从来没有在c ++中做过任何多线程或异步的事情,到目前为止我只使用cURL来做单个同步请求.
为了更好地可视化我正在尝试做的事情,我编写了一个简单的Javascript,它可以用C++中的cURL做我想做的事情.

function AddRequest( method, url, data, id ) {
    var httpObj = new ActiveXObject("Msxml2.XMLHTTP.6.0"); //new XMLHttpRequest();
    httpObj.onreadystatechange = function() {
        if (httpObj.readyState == 4)
            ResponseCallback( httpObj, id );
    };
    httpObj.Open( method, url, true );
    httpObj.Send( data );

}

function ResponseCallback( httpObj, id ) {
    WScript.Echo( id ); //alert( id );
    WScript.Echo( httpObj.ResponseText ); //alert( httpObj.ResponseText );
}

//It could now be used like this:

AddRequest("GET","http://example.com/","",1);
AddRequest("GET","https://www.facebook.com","",2);
WScript.Echo( "all requests sent" ); //alert( "all requests sent" );
//these requests …
Run Code Online (Sandbox Code Playgroud)

c++ curl asynchronous libcurl httprequest

5
推荐指数
1
解决办法
3494
查看次数

检查日期是今天、昨天还是过去 7 天

我知道这个线程:优雅地检查给定的日期是否是昨天
但我只是专门寻找一个 JavaScript 解决方案。如果可能的话,一个简短的。我真的想不出一个 100% 可靠的方法..

到目前为止,我是这样做的:

function FormatDate(someDtUTC) {
    var someDt = new Date(someDtUTC.getTime() + someDtUTC.getTimezoneOffset() * 60 * 1000);
    var dtNow = new Date();
    if (dtNow.getUTCFullYear() == someDt.getUTCFullYear() && dtNow.getUTCMonth() == someDt.getUTCMonth()) {
        if (dtNow.getUTCDate() == someDt.getUTCDate())
            var dateString = "Today, " + Ext.Date.format(someDt, 'G:i'); // Today, 15:32
        else if (dtNow.getUTCDate() - 1 == someDt.getUTCDate())
            var dateString = "Yesterday, " + Ext.Date.format(someDt, 'G:i'); //Yesterday, 13:26
        else if (dtNow.getUTCDate() - someDt.getUTCDate() < 7)
            var dateString = Ext.Date.format(someDt, 'l, …
Run Code Online (Sandbox Code Playgroud)

javascript time date

5
推荐指数
2
解决办法
2万
查看次数

C#Selection使用字符串排序

好的,我一直在使用这段代码对整数进行选择排序:

public void selectSort(int [] arr)
{
    //pos_min is short for position of min
    int pos_min,temp;

    for (int i=0; i < arr.Length-1; i++)
    {
        pos_min = i; //set pos_min to the current index of array

        for (int j=i+1; j < arr.Length; j++)
        {
            if (arr[j] < arr[pos_min])
            {
                //pos_min will keep track of the index that min is in, this is needed when a swap happens
                pos_min = j;
            }                                          
        }

        //if pos_min no longer equals i than a smaller …
Run Code Online (Sandbox Code Playgroud)

c# sorting string list selection-sort

5
推荐指数
1
解决办法
3404
查看次数

无论循环内执行多长时间,每x秒运行一次代码

我正试图让某个歌曲的LED闪烁.这首歌正好是125 bpm.
我写的代码似乎首先工作,但运行的时间越长,LED闪烁和下一个节拍之间的时间差就越大.LED似乎眨了一下太慢了.

我认为这是因为lastBlink有点依赖于之前发生的眨眼以保持同步,而不是使用一个静态初始值来同步...

unsigned int bpm = 125;
int flashDuration = 10;
unsigned int lastBlink = 0;
for(;;) {
    if (getTickCount() >= lastBlink+1000/(bpm/60)) {
        lastBlink = getTickCount();
        printf("Blink!\r\n");
        RS232_SendByte(cport_nr, 4); //LED ON
        delay(flashDuration);
        RS232_SendByte(cport_nr, 0); //LED OFF
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ loops

5
推荐指数
1
解决办法
279
查看次数

浏览器扩展:在浏览器操作弹出窗口和后台脚本之间发送消息(带响应)

首先,我不是在寻找长期连接。我特别在寻找一种发送消息和直接响应这些消息的方法。

当您在 content-script 和 background-script 之间发送消息时,它非常简单,因为您使用 chrome.tabs API 从/向内容脚本发送/接收消息。并且要从/向后台脚本发送/接收消息,您可以使用 chrome.runtime API。

但是浏览器操作弹出窗口有点不同,因为两者都在后台上下文中运行。所以我想他们都必须使用 chrome.runtime API。

但这意味着我必须chrome.runtime.onMessage在浏览器操作弹出窗口和后台脚本中收听。所以基本上我会在后台脚本中接收从弹出窗口发送的消息,但也会在弹出窗口本身中接收消息。反过来也是一样。

所以是的,这真的行不通:

/////////////background-script//////////////
//Send message from background-script to browser-action-popup
chrome.runtime.sendMessage({msg:"This is a message sent from the background-script to the browser-action-popup"})
  .then(response => { //Receive response from the browser-action-popup
      console.log(response.msg)
  })

//Receive messages from browser-action-popup
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse) {
    sendResponse({msg:"This is a response message sent from the background-script"})
    return true
})
Run Code Online (Sandbox Code Playgroud)

...

///////////browser-action-popup/////////////
//Send message from browser-action-popup to background-script
chrome.runtime.sendMessage({msg:"This is a message sent from the browser-action-popup …
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome-extension firefox-addon-webextensions

5
推荐指数
1
解决办法
2827
查看次数

解析大于内存的 JSON 字符串

我正在使用的平台具有非常严格的内存限制,我正在尝试找到一种方法来解析大的 JSON 字符串,而无需最多将超过几百个字节加载到内存中。JSON 字符串存储在更大的芯片(闪存)上的文件中。

有两件事我真的找不到好的解决方案:

  1. 通过指定“路径”(如foo["bar"][2].
    (如果结果是一个数组/对象,那么我们应该只返回它是一个数组/对象的事实,也可能返回它是否为空。)
  2. 迭代 JSON 中的任何对象/数组。

所以基本上我需要函数,当调用时,逐步解析 json 并且只保存我们实际需要继续解析的部分。

对于界面,我认为不可能有类似的东西exampleJson["aa"].2.["gg],但我设法非常接近:exampleJson["aa"].2.["gg"]()。这将导致调用一个函数,然后该函数可以轻松访问 {'aa',2,'gg'} 并从文件中读取/解析 json。

到目前为止,这是我的代码,但我真的不知道如何继续:https :
//repl.it/HfwS/2

-- Looks complicated, but is pretty simple. Using meta tables we create a json interface that can almost be accessed as if it was a lua table.
-- E.g. example["aa"][2]["gg"]() ; the only difference is that we have to use parentheses at the end
-- The problematic part starts where it says `THIS …
Run Code Online (Sandbox Code Playgroud)

memory lua parsing json lua-5.1

5
推荐指数
1
解决办法
671
查看次数

如何在node.js中同时使用多个代理(代理)

我正在使用一个 HTTP 请求库,它可以使用http.Agent实例来通过特定代理路由您的请求。让我举一个例子:

const SocksProxyAgent = require('socks-proxy-agent')
const HttpProxyAgent = require('http-proxy-agent') // not used
const axios = require('axios')

// requires Tor service to be running
const torProxyAgent = new SocksProxyAgent('socks://circuit1@127.0.0.1:9050')
// not used
const otherProxyAgent = new HttpProxyAgent('http://admin:1234@localhost:8888')

const axiosConfig = {
    httpsAgent: torProxyAgent,
    httpAgent: torProxyAgent
}

const axiosInstance = axios.create(axiosConfig)

axiosInstance.get('https://ifconfig.io/ip')
  .then(res => {
    console.log('Public IP address:', res.data)
  }).catch(err => {
    console.log(err)
  })
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我指定了多个代理 (torProxyAgentotherProxyAgent),但我只能使用一个。

因此,我正在寻找一种超级代理,我可以将其传递给我的 HTTP 请求库,然后该超级代理通过将所有请求路由到第一个代理,然后通过第二个代理,然后将任意数量的普通代理链接在一起。到第三个,依此类推……

这可能吗?有现成的解决方案吗?

是否有一种方法可以使用Proxy类创建一个假代理,将所有方法调用传递给一个代理,然后传递给第二个代理?

编辑: …

proxy http socks http-proxy node.js

5
推荐指数
1
解决办法
6689
查看次数

从 EventEmitter 继承是反模式吗?

如果您希望您的类支持事件,那么从 EventEmitter 继承似乎是常见的做法。例如,Google 为Puppeteer执行此操作,WebSocket 模块执行此操作,mongoose 执行此操作,...仅举几例。

但这真的是好的做法吗?我的意思是它看起来确实漂亮干净,但从 OOP 的角度来看,这似乎是错误的。例如:

const EventEmitter = require('events')
class Rectangle extends EventEmitter {
    constructor(x,y,w,h) {
        super()
        this.position = {x:x, y:y}
        this.dimensions = {w:w, h:h}
    }
    setDimensions(w,h) {
        this.dimensions = {w:w, h:h}
        this.emit('dimensionsChanged')
    }
}
Run Code Online (Sandbox Code Playgroud)

看起来 Rectangle 的核心是一个 EventEmitter,尽管事件功能是次要的。

如果您决定Rectangle现在需要从一个名为 的新类继承怎么办Shape

class Shape {
    constructor(x,y) {
        this.position = {x:x, y:y}
    }
}

class Rectangle extends Shape {
    constructor(x,y,w,h) {
        super(x,y)
        this.dimensions = {w:w, h:h}
    }
} …
Run Code Online (Sandbox Code Playgroud)

javascript anti-patterns node.js eventemitter es6-class

5
推荐指数
1
解决办法
665
查看次数

从 Spotify 获取歌词数据

我已经知道网址是https://spclient.wg.spotify.com/color-lyrics/v2/track/${TRACK_ID}?format=json&vocalRemoval=false

它需要两个标头。app-platform: WebPlayerauthorization: Bearer TOKEN

因此,使用curl我可以获得这样的歌词信息:

$ TRACK_ID=3z8h0TU7ReDPLIbEnYhWZb
$ BEARER_TOKEN=xxxxxxxxxxxxxx
$ curl "https://spclient.wg.spotify.com/color-lyrics/v2/track/${TRACK_ID}?format=json&vocalRemoval=false" -H "app-platform: WebPlayer" -H "authorization: Bearer ${BEARER_TOKEN}"
{
  "lyrics": {
    "syncType": "LINE_SYNCED",
    "lines": [
      {
        "startTimeMs": "110",
        "words": "Is this the real life? Is this just fantasy?",
        "syllables": [],
        "endTimeMs": "0"
      },
      {
        "startTimeMs": "6990",
        "words": "Caught in a landslide, no escape from reality",
        "syllables": [],
        "endTimeMs": "0"
      },
      ...
Run Code Online (Sandbox Code Playgroud)

实际的问题是如何以编程方式获取所需的不记名令牌?我已尝试使用此网站上的“获取令牌”按钮请求令牌:https://developer.spotify.com/console/get-track/

但该令牌似乎仅适用于官方 API。对于歌词 API,我总是收到该令牌的以下响应:

{
  "error": {
    "status": 403,
    "message": "Client …
Run Code Online (Sandbox Code Playgroud)

spotify web-scraping

5
推荐指数
1
解决办法
8104
查看次数