我在 Linux 中使用串行端口进行编码。
并且通信的要求是5ms inter-byte time。
而且它要求我在 write() 调用之前根据字节的值更改每个字节的奇偶校验模式(偶数和奇数)。
所以我的代码如下(我简单描述了代码)
void setWakeupMode(int fd, bool mode) {
struct termios tio;
bzero(&tio, sizeof(tio));
tcgetattr(fd, &tio);
if (mode == false) {
tio.c_cflag &= ~PARODD;
} else if (mode == true) {
tio.c_cflag |= PARODD;
}
if(tcsetattr(fd, TCSADRAIN, &tio) < 0){
perror("tcsetattr Error");
}
}
int main(){
unsigned char a[2] = {0x01, 0x0F};
write(fd, a, 1);
setWakeupMode(fd, true);
write(fd, a+1, 1);
}
但是代码不满足字节间时间导致将近20ms。
所以我尝试打印每个系统调用之间的准确时间,如下所示。
int main(){
unsigned char a[2] = {0x01, 0x0F};
printf("write1 start : %s", /*time*/);
write(fd, a, 1);
printf("write1 end : %s", /*time*/);
setWakeupMode(fd, true);
printf("write2 start : %s", /*time*/);
write(fd, a+1, 1);
printf("write2 end : %s, /*time*/);
}
这是结果
write1 start : 34.755201
write1 end : 34.756046
write2 start : 34.756587
write2 end : 34.757349
这个结果突然满足5ms字节间时间,导致1ms字节间时间。
所以我尝试了几种方法。
最后我认识到,只有当我在 tcsetattr() 之前打印一些内容时,字节间时间才能满足。
例如,如果我删除 printf("write1 end : %s,/*time*/); 如下所示
int main(){
unsigned char a[2] = {0x01, 0x0F};
printf("write1 start : %s", /*time*/);
write(fd, a, 1);
// printf("write1 end : %s", /*time*/); //remove this
setWakeupMode(fd, true);
printf("write2 start : %s", /*time*/);
write(fd, a+1, 1);
printf("write2 end : %s", /*time*/);
}
结果出奇的不同,看write1 start和write2 start的间隔,
它是 18ms。
write1 start : 40.210111
write2 start : 40.228332
write2 end : 40.229187
如果我使用 std::cout 而不是 printf,结果是一样的。
为什么会出现这种奇怪的情况?
----------------------------编辑------------ --------------------
因为我看到了一些答案,所以有些人误解了我的问题。
我不担心 printf() 开销。
简单的说。
write() 但是write()的间隔必须在5ms以内write() 之前,我必须使用 tcsetattr()18ms,几乎在tcsetattr()处被阻塞。tcsetattr() 之前调用 printf() 或 std::cout,间隔会减少到 1 ~2 毫秒。也就是说,不知何故,在 tcsetattr() 之前调用 printf 会使 tcsetattr() 更快地从阻塞中返回。
------------------------更新---------------- ----------
我在这个问题上取得了进展。
我说过我必须放置 printf() 或 std::cout 来缩短 tcsetattr() 上的阻塞时间。
但它并没有打印出影响那个问题的东西。
它只是需要一些延迟,也就是说,如果我在调用 tcsetattr() 之前放置 usleep(500),它也会影响字节间时间减少 1~2ms,从 tcsetattr() 返回更快。
我假设,如果我用 TCSADRAIN 标志调用 tcsetattr(),它会等到串行缓冲区中的所有数据都已传输,然后更改为我想要的设置。
这可能会造成一些延迟。
但是如果我专门调用delay,在我调用tcsetattr()之前,buffer状态已经为空(因为在delay期间,串口buffer中的数据被传输),所以有没有阻塞。
这是我假设的情况,可能吗?
最佳答案
this is the scenario i assume, is it possible?
你一定是对的。木屑写道:
uart_wait_until_sent() in
drivers/tty/serial/serial_core.cexplains why the interval between characters is quantized when a "drain" is involved: the serial port driver is polled for the TIOCSER_TEMP (transmitter empty) status using a delay with only millisecond resolution.
这几乎是正确的,除了轮询周期分辨率不是毫秒,而是jiffies:
while (!port->ops->tx_empty(port)) {
msleep_interruptible(jiffies_to_msecs(char_time));
…
因此,如果 HZ 为 100 且 char_time 为 1(最小值),则每 10 毫秒检查一次状态。如您所写:
but if i calling specifically delay, before i call
tcsetattr(), the buffer state is already empty(because during the delay time, the data in serial buffer is transmitted), so that there is no blocking.
换句话说,如果你延迟这个过程足够长的时间,当它到达发射器空测试时 !port->ops->tx_empty(port) 发射器已经是空的,不 sleep 发生;否则它至少会休眠 1 分钟。
关于c++ - 带有 TCSADRAIN 标志的 tcsetattr() 阻塞时间很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27499501/
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时
使用rspec-rails3.0+,测试设置分为spec_helper和rails_helper我注意到生成的spec_helper不需要'rspec/rails'。这会导致zeus崩溃:spec_helper.rb:5:in`':undefinedmethod`configure'forRSpec:Module(NoMethodError)对thisissue最常见的回应是需要'rspec/rails'。但这是否会破坏仅使用spec_helper拆分rails规范和PORO规范的全部目的?或者这无关紧要,因为Zeus无论如何都会预加载Rails?我应该在我的spec_helper中做
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
假设我有一个类A,里面有一些方法。假设stringmethodName是这些方法之一,我已经知道我想给它什么参数。它们在散列中{'param1'=>value1,'param2'=>value2}所以我有:params={'param1'=>value1,'param2'=>value2}a=A.new()a.send(methodName,value1,value2)#callmethodnamewithbothparams我希望能够通过传递我的哈希以某种方式调用该方法。这可能吗? 最佳答案 确保methodName是一个符号,而
因为我现在正在做一些时间测量,我想知道是否可以在不使用Benchmark类或命令行实用程序time的情况下测量用户时间或系统时间。使用Time类只显示挂钟时间,而不显示系统和用户时间,但是我正在寻找具有相同灵active的解决方案,例如time=TimeUtility.now#somecodeuser,system,real=TimeUtility.now-time原因是我有点不喜欢Benchmark,因为它不能只返回数字(编辑:我错了-它可以。请参阅下面的答案。)。当然,我可以解析输出,但感觉不对。*NIX系统的time实用程序也应该可以解决我的问题,但我想知道是否已经在Ruby中实