假设我需要生成变量来保存用户的一些输入(我不知道它们有多少).如果不使用Array,ArrayList(和其他类型的列表和地图),可我的代码生成(可以说)String有相似的名字(变量X倍String var001,String var002,String var003,等)?如果是,请提供示例代码.
我有两个设备,我想通过串行接口连接,但它们有不兼容的连接.为了解决这个问题,我将它们连接到我的PC上,我正在开发一个C#程序,它将把COM端口X上的流量路由到COM端口Y,反之亦然.
该程序连接到两个COM端口.在数据接收事件处理程序中,我读入传入的数据并将其写入另一个COM端口.为此,我有以下代码:
private void HandleDataReceived(SerialPort inPort, SerialPort outPort)
{
byte[] data = new byte[1];
while (inPort.BytesToRead > 0)
{
// Read the data
data[0] = (byte)inPort.ReadByte();
// Write the data
if (outPort.IsOpen)
{
outPort.Write(data, 0, 1);
}
}
}
Run Code Online (Sandbox Code Playgroud)
只要传出的COM端口以高于传入COM端口的波特率运行,该代码就能正常工作.如果传入的COM端口比传出的COM端口快,我开始丢失数据.我不得不纠正这样的代码:
private void HandleDataReceived(SerialPort inPort, SerialPort outPort)
{
byte[] data = new byte[1];
while (inPort.BytesToRead > 0)
{
// Read the data
data[0] = (byte)inPort.ReadByte();
// Write the data
if (outPort.IsOpen)
{
outPort.Write(data, 0, 1);
while (outPort.BytesToWrite > 0); //<-- Change …Run Code Online (Sandbox Code Playgroud) 我想让Android USB主机模式正常工作; 但是我需要将波特率设置为56000.我只找到了这段代码:
UsbDeviceConnection myConnection;
myConnection.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0);//baudrate 9600
Run Code Online (Sandbox Code Playgroud)
在参考文献中我没有找到任何关于此的内容.
非常感谢!
我即将使用Python开始一个程序,它主要进行轮询,它将不断从串口读取(通过PySerial)并从文件描述符中读取,该文件描述符将不时发生变化.我开始寻找到的threading模块,但后来我发现保存更多和更多使用的建议multiprocessing模块来代替.
我不熟悉Python,主要来自C背景.Python中的线程方法有哪些技术优势?
在C中,线程共享数据而不是必须设置一些IPC来进行通信,这似乎与Python相同?
我的用例:
Main process (or thread?) -
start & initialize
|
V
spaw child----------------------> start & initialize
| |
V V
while (1) <------------+ wait for data<------+
| | | |
V | V |
read file descriptors | read from |
| | serial port<-----+ |
V | | | |
value changed? ------No--+ V | |
| ^ message done?--No-+ |
V | | |
Report …Run Code Online (Sandbox Code Playgroud) 我最近对编译器,标准库和内核的内部工作感兴趣.在我搜索标准C库的源代码时,我遇到了Glibc.但它在Glibc的官方网站上说的是:the library which defines the ''system calls'' and other basic facilities such as open, malloc, printf, exit...
所以我猜Glibc实际上并没有提供标准C库的源代码,而是为这些函数提供系统调用,然后内核负责处理它们,对不对?
我想更多地了解这些事情.例如,如何做的sin,printf以及strlen,功能得到C程序执行?如果Glibc只提供系统调用,那些函数的实际源代码在哪里?内核如何执行它们?哪里可以找到执行这些功能的内核部分的源代码?
在尝试理解更大的问题时,我正在进行一些测试.这是我的测试环境:
head.h:
#define MAX_BUFSIZE 500
typedef struct {
int head;
int tail;
int status;
int active;
void * dev[MAX_BUFSIZE];
char free[MAX_BUFSIZE];
int count;
} msg_fifo_t;
extern msg_fifo_t TxBufx[];
extern msg_fifo_t Rx_Buf[];
Run Code Online (Sandbox Code Playgroud)
test.c:
#include <stdio.h>
#include "head.h"
//msg_fifo_t TxBufx[10]; // This is the important line
int main(int argc, char * argv[])
{
// This part isn't really important...
printf("Hello Test\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以我使用这些文件并运行了三个测试来查看我得到的尺寸 -
测试#1(代码如上):
> gcc -Os test.c
> ls -al a.out
-rwxrwxr-x 1 mike …Run Code Online (Sandbox Code Playgroud) 我正在尝试更多地了解可执行文件的"常见"部分,我注意到objdump在编译代码时,我可以看到只在对象文件(*.o而不是可执行文件)上放置在公共代码中的变量.
这是为什么?
//test.c
int i[1000];
int main(){return 0;}
Run Code Online (Sandbox Code Playgroud)
构建命令:
> gcc -g0 -fcommon -c test.c
> gcc -g0 -fcommon test.c
Run Code Online (Sandbox Code Playgroud)
objdump显示i在符号表的公共部分中:
> objdump -x test.o
...
SYMBOL TABLE:
...
00000fa0 O *COM* 00000020 i
Run Code Online (Sandbox Code Playgroud)
除非我在可执行文件上运行它:
> objdump -x a.out
...
SYMBOL TABLE:
...
0804a040 g O .bss 00000fa0 i
Run Code Online (Sandbox Code Playgroud)
如果我使用-fno-common标志重建目标文件,而不是.bss像在可执行文件上那样显示在段中.最终的可执行文件没有这个"COMMON"部分吗?
我正在玩弄一些打开,阅读和修改文本文件的代码.一个快速(简化)的例子是:
#include <stdio.h>
int main()
{
FILE * fp = fopen("test.txt", "r+");
char line[100] = {'\0'};
int count = 0;
int ret_code = 0;
while(!feof(fp)){
fgets(line, 100, fp);
// do some processing on line...
count++;
if(count == 4) {
ret_code = fprintf(fp, "replaced this line\n");
printf("ret code was %d\n", ret_code);
perror("Error was: ");
}
}
fclose(fp);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在在Linux上,使用gcc(4.6.2)编译此代码,并修改文件的第5行.在使用Visual C++ 2010编译的Windows7上运行的相同代码运行并声称已成功(报告返回代码为19个字符并perror说"无错误")但无法替换该行.
在Linux上,我的文件具有完全权限:
-rw-rw-rw- 1 mike users 191 Feb 14 10:11 test.txt
Run Code Online (Sandbox Code Playgroud)
据我所知,它在Windows上也是如此:
test.txt(右键单击) - >属性 - >安全性 …
想象一下,我有一个由36个顶点组成的6x6正方形(即每行6个顶点和每列6个顶点),看起来像这样:
• • • • • •
• • • • • •
• • • • • •
• • • • • •
• • • • • •
• • • • • •
Run Code Online (Sandbox Code Playgroud)
每个顶点都与1,2,3或4个近邻点连接 - 所以我们基本上得到一个带有顶点和边的图.我的问题如下:我想要一个机器人通过那个"迷宫"的边缘,直到找到某个顶点放置某个物体.一旦找到该对象,就应该以最快的方式回到起点.
现在,我不太清楚如何实现这一点,所以我的问题是:在C中保存有关顶点和边的信息的最佳结构是什么?(邻接矩阵对我来说似乎效率低,因为36x36非常大).而且,使用这些信息,我怎样才能找到最快的方式?