我正在玩Qt,我想在两个命令之间创建一个简单的暂停.但是它似乎没有让我使用Sleep(int mili);
,我找不到任何明显的等待功能.
我基本上只是制作一个控制台应用程序来测试一些类代码,这些代码稍后将包含在一个合适的Qt GUI中,所以现在我不打算打破整个事件驱动的模型.
我正在学习如何在Java中使用线程.我写了一个实现Runnable的类,它可以并发运行到另一个线程.主线程处理侦听串行端口,而第二个线程将处理将数据发送到同一端口.
public class MyNewThread implements Runnable {
Thread t;
MyNewThread() {
t = new Thread (this, "Data Thread");
t.start();
}
public void run() {
// New Thread code here
}
Run Code Online (Sandbox Code Playgroud)
第一个线程开始第二个像这样:
public class Main {
public static void main(String[] args) throws Exception{
new MyNewThread();
// First thread code there
}
}
Run Code Online (Sandbox Code Playgroud)
这有效,但我的编译器标记了一个警告说:在构造函数中启动一个新线程是危险的.为什么是这样?
这个问题的第二部分是:如果我在一个线程中运行一个循环(串口侦听线程),我在第二个线程中键入一个exit命令.如何获得第一个终止线程?谢谢.
我已经编程了很长一段时间,我才真正意识到.我之前创建了许多函数,它们将字符串作为char数组返回(或至少指向它们的指针).
有一天有人指出,当我的函数返回时,我的函数指向的char数组已超出范围,我现在基本上指向一个随机的内存(一个令人讨厌的悬空指针).
我暂时没有注意到这一点,因为输出到控制台时的char数组似乎没有损坏(可能是因为没有时间覆盖该数据).当我返回一个通过读取经常损坏的串口生成的字符串缓冲区(char数组)时,我注意到了这一点.
那么,我该怎么做才能做到最好?
我的错误代码如下:
#include <cstdlib>
#include <iostream>
using namespace std;
char* myBadFunction(){
char charArray[] = "Some string\n";
char* charPointer = charArray;
return charPointer;
}
int main(int argc, char** argv) {
cout << myBadFunction();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道我应该在调用函数之前在程序中分配内存或创建一个全局变量来放入返回的字符串,但是如果我的被调用函数被许多不同的程序使用,那么它应该如何知道传递的缓冲区的大小提前进入它,何时删除此内存?
以下代码也没有正确地执行我想要的操作:
#include <cstdlib>
#include <iostream>
using namespace std;
void fillArray(char* charPointer){
char charArray[] = "Some string\n"; // Create string
charPointer = charArray; // Not correct, want to fill predefined array with created string
return;
}
int main(int argc, char** argv) { …
Run Code Online (Sandbox Code Playgroud) 我有一个用户输入的字符串,我想搜索它并用我的替换字符串替换任何出现的单词列表.
import re
prohibitedWords = ["MVGame","Kappa","DatSheffy","DansGame","BrainSlug","SwiftRage","Kreygasm","ArsonNoSexy","GingerPower","Poooound","TooSpicy"]
# word[1] contains the user entered message
themessage = str(word[1])
# would like to implement a foreach loop here but not sure how to do it in python
for themessage in prohibitedwords:
themessage = re.sub(prohibitedWords, "(I'm an idiot)", themessage)
print themessage
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用,我确定我不明白python for循环是如何工作的.
我需要将QChar转换为wchar_t
我尝试过以下方法:
#include <cstdlib>
#include <QtGui/QApplication>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
QString mystring = "Hello World\n";
wchar_t myArray[mystring.size()];
for (int x=0; x<mystring.size(); x++)
{
myArray[x] = mystring.at(x).toLatin1();
cout << mystring.at(x).toLatin1(); // checks the char at index x (fine)
}
cout << "myArray : " << myArray << "\n"; // doesn't give me correct value
return 0;
}
Run Code Online (Sandbox Code Playgroud)
哦,在有人建议使用.toWCharArray(wchar_t*array)函数之前,我已经尝试过了,它基本上做了与上面相同的事情,并没有按照它应该传输字符.
如果你不相信我,下面是代码:
#include <cstdlib>
#include <QtGui/QApplication>
#include <iostream>
using namespace std;
int main(int argc, char** argv) { …
Run Code Online (Sandbox Code Playgroud) 你好我有一个泵类需要使用一个成员变量,该变量是一个指向包含端口地址的wchar_t数组的指针,即:"com9".
问题是,当我在构造函数中初始化此变量时,我的编译器会标记折旧的转换警告.
pump::pump(){
this->portNumber = L"com9";}
Run Code Online (Sandbox Code Playgroud)
这工作正常但每次编译时的警告都很烦人,让我觉得我做错了什么.
我尝试创建一个数组,然后像这样设置成员变量:
pump::pump(){
wchar_t port[] = L"com9";
this->portNumber = port;}
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,这使我的portNumber指向'F'.
显然,我的另一个概念问题.
感谢您对我的无聊问题的帮助.
编辑:
作为请求,portNumber的定义是:
class pump
{
private:
wchar_t* portNumber;
}
Run Code Online (Sandbox Code Playgroud)
感谢答案,它现在已更改为:
class pump
{
private:
const wchar_t* portNumber;
}
Run Code Online (Sandbox Code Playgroud) 我是python的新手,它处理变量和列表中变量数组的方式对我来说很陌生.我通常会将一个文本文件读入一个向量,然后通过确定向量的大小将最后三个文件复制到一个新的数组/向量中,然后使用for循环将最后一个size-3的复制函数循环到一个新数组中.
我不明白循环如何在python中工作所以我不能这样做.
到目前为止我有:
#read text file into line list
numberOfLinesInChat = 3
text_file = open("Output.txt", "r")
lines = text_file.readlines()
text_file.close()
writeLines = []
if len(lines) > numberOfLinesInChat:
i = 0
while ((numberOfLinesInChat-i) >= 0):
writeLine[i] = lines[(len(lines)-(numberOfLinesInChat-i))]
i+= 1
#write what people say to text file
text_file = open("Output.txt", "w")
text_file.write(writeLines)
text_file.close()
Run Code Online (Sandbox Code Playgroud) 一个类似但不同的问题:
我有一个生成字符串的 IRC 客户端。这个 IRC 客户端使用一个钩子来调用一个方法 (somone_said) 每当有人说的话。我想通过套接字将此字符串发送到我的 Flash 客户端。
我有一个在 flash 中工作的客户端和一个在 python 中的服务器,但问题是它阻塞:1)在侦听客户端连接时 2)在等待生成下一条消息时
这会阻止 IRC 客户端响应其他输入。
我想我需要在一个单独的线程中创建我的套接字,但这又产生了三个问题。1)我的someone_said事件驱动方法如何访问套接字2)如果有人在没有服务器客户端连接(在侦听时)或客户端关闭连接时说了些什么怎么办。3)如何检查线程是否处于活动状态,如果没有打开一个新线程?
我的阻塞服务器代码是这样的:
# Echo server program
import socket
import sys
HOST = None # Symbolic name meaning all available interfaces
PORT = 7001 # Arbitrary non-privileged port
s = None
def startListening():
print "starting to listen"
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC,
socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except socket.error as …
Run Code Online (Sandbox Code Playgroud) 背景资料:
我一直在编写代码来控制通过USB线连接的设备,但是模拟RS-232串口.
有问题的设备是Arduino微控制器控制的伺服摇摄和倾斜阶段(但这并不重要).
我已经设法使用C++语言将字符写入USB模拟串行端口,并使用以下代码在NetBeans IDE中设置g ++编译器:
#include <cstdlib>
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
/*
* 'open_port()' - Open serial port 1.
*
* Returns the file descriptor on success or -1 on error.
*/
int
open_port(void)
{
int fd; /* …
Run Code Online (Sandbox Code Playgroud) 我在java框架中有一个按钮,按下它时会从文本字段中读取一个值,并将该字符串用作尝试连接到串行设备的端口名称.
如果此连接成功,则该方法返回true,否则返回false.如果它返回true,我希望框架消失.然后将出现在其他类中指定的一系列其他帧以及控制串行设备的选项.
我的问题是:按钮连接到动作监听器,按下此方法被调用.如果我尝试使用frame.setVisible(true); 方法java抛出一个抽象按钮错误,因为我有效地告诉它在按钮按下方法退出之前消失包含按钮的框架.删除frame.setVisible(true); 允许程序正确运行但是我留下了一个不再有用的延迟连接框架.
按下按钮后如何让框架消失?
package newimplementation1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author Zac
*/
public class ConnectionFrame extends JPanel implements ActionListener {
private JTextField textField;
private JFrame frame;
private JButton connectButton;
private final static String newline = "\n";
public ConnectionFrame(){
super(new GridBagLayout());
textField = new JTextField(14);
textField.addActionListener(this);
textField.setText("/dev/ttyUSB0");
connectButton = new JButton("Connect");
//Add Components to this panel.
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
add(textField, c);
c.fill = GridBagConstraints.BOTH;
c.weightx …
Run Code Online (Sandbox Code Playgroud) 我正在与计算机控制的泵进行一些串行端口通信,而我用来通信的createfile函数要求将串行端口名称解析为wchar_t指针。
我还使用QT创建表单并以QString形式获取com端口名称。
该QString转换为char数组,并指向如下:
char* Dialog::GetPumpSerialPortNumber(){
QString mystring;
mystring = ui->comboBox_2->currentText();
char * mychar;
mychar = mystring.toLatin1().data();
return mychar;
Run Code Online (Sandbox Code Playgroud)
现在,我需要设置端口号,该端口号以wchar_t *的形式存储在泵对象中。我通过调用以下函数来做到这一点:
void pump::setPortNumber(wchar_t* portNumber){
this->portNumber = portNumber;
}
Run Code Online (Sandbox Code Playgroud)
因此,如何将我的char *(mychar)更改为wchar_t *(portNumber)?
谢谢。
我想要一种方法来存储最多三个字符串.当我得到一个新的,我想将它添加到列表的底部,并从列表的顶部删除一个(最旧的一个).
我知道这可以在带有双端队列的python中完成,但不知道如何在AS3中实现它或者它是否已经存在.谷歌搜索在googlecode上发现了一些代码,但它没有编译.
我正在尝试计算char数组中的字符数,包括直到字符串结尾的空格.
以下编译但不返回正确的值,我正在尝试使用指针算法来通过我的数组进行交互.
int numberOfCharsInArray(char* array) {
int numberOfChars = 0;
while (array++ != '\0') {
numberOfChars++;
}
return numberOfChars;
}
Run Code Online (Sandbox Code Playgroud)
非常感谢.
显然我试图从cstring获得相当于length()但使用简单的char数组.
当然,如果我的原始数组不是null终止,这可能会导致一个非常大的值返回(我猜).