Time 无法通过USB将计算机时间同步到Arduino
我想将时间从我的电脑同步到arduino。我正在使用他们的时间库,但它不起作用 我怎样才能让arduino与我的计算机上的时间相同?我目前正在使用mac电脑 他们的文件说: 在unix系统上,可以使用shell命令设置时间:Time 无法通过USB将计算机时间同步到Arduino,time,serial-port,arduino,processing,Time,Serial Port,Arduino,Processing,我想将时间从我的电脑同步到arduino。我正在使用他们的时间库,但它不起作用 我怎样才能让arduino与我的计算机上的时间相同?我目前正在使用mac电脑 他们的文件说: 在unix系统上,可以使用shell命令设置时间: TZ_adjust=-8;echo T$($(日期+%s)+6060$TZ_调整))>/dev/tty.usbserial-A8008pym 我在终点站试过了 >export TZ_adjust=-8; echo T$(($(date +%s)+6060$TZ_adj
TZ_adjust=-8;echo T$($(日期+%s)+6060$TZ_调整))>/dev/tty.usbserial-A8008pym
我在终点站试过了
>export TZ_adjust=-8; echo T$(($(date +%s)+6060$TZ_adjust)) > /dev/tty.usbmodemfd131
我被拒绝了
我做错了什么?
有没有更简单的方法将arduino上的时间与我的电脑同步
代码
#include <Time.h>
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER 'T' // Header tag for serial time sync message
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message
void setup() {
Serial.begin(9600);
setSyncProvider( requestSync); //set function to call when sync required
Serial.println("Waiting for sync message");
}
void loop(){
if(Serial.available() )
{
processSyncMessage();
}
if(timeStatus()!= timeNotSet)
{
digitalWrite(13,timeStatus() == timeSet); // on if synced, off if needs refresh
digitalClockDisplay();
}
delay(1000);
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
void processSyncMessage() {
// if time sync available from serial port, update time and return true
while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
char c = Serial.read() ;
Serial.print(c);
if( c == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
c = Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
setTime(pctime); // Sync Arduino clock to the time received on the serial port
}
}
}
time_t requestSync()
{
Serial.print(TIME_REQUEST,BYTE);
return 0; // the time will be sent later in response to serial mesg
}
#包括
#定义时间\u MSG\u LEN 11//TIME sync to PC是标头,后跟十位ascii数字的unix时间
#定义时间\头'T'//串行时间同步消息的头标记
#定义时间请求7//ASCII贝尔字符请求时间同步消息
无效设置(){
Serial.begin(9600);
setSyncProvider(requestSync);//设置需要同步时调用的函数
Serial.println(“等待同步消息”);
}
void loop(){
if(Serial.available())
{
processSyncMessage();
}
if(timeStatus()!=timeNotSet)
{
digitalWrite(13,timeStatus()==timeSet);//如果同步,则打开;如果需要刷新,则关闭
数字时钟显示器();
}
延迟(1000);
}
void digitalClockDisplay(){
//数字时钟显示时间
Serial.print(hour());
打印数字(分钟();
打印数字(第二个());
连续打印(“”);
Serial.print(day());
连续打印(“”);
Serial.print(month());
连续打印(“”);
序列号。打印(年份());
Serial.println();
}
无效打印位数(整数位数){
//数字时钟显示实用功能:打印前冒号和前导0
连续打印(“:”);
如果(数字<10)
Serial.print('0');
串行打印(数字);
}
void processSyncMessage(){
//如果串行端口提供时间同步,则更新时间并返回true
而(Serial.available()>=TIME\u MSG\u LEN){//TIME消息由一个头和十个ascii数字组成
char c=Serial.read();
连续打印(c);
如果(c==TIME_头){
时间=0;
对于(inti=0;i如果(c>='0'&&cConrad和我在以下情况下发现了解决方案:
@sachleen权限被拒绝。很抱歉疏忽了。您可以在命令前尝试使用sudo?@DavidK警告:不正确使用sudo命令可能会导致数据丢失或删除重要的系统文件。请在使用sudo时仔细检查键入内容。键入“man sudo”有关详细信息,请输入密码,或键入Ctrl-C中止。密码:sudo:export:command not found-bash:/dev/tty.usbserial-A8008pym:Permission deniedOk尝试以下命令sudo su获取根终端,然后再次尝试该命令。export TZ_adjust=-8;正在创建一个名为TZ_adjust的变量并将其设置为-8供在命令末尾使用。因此,如果再次失败,请尝试:sudo echo T$($(日期+%s)+6060$-8))>/dev/tty.usbmodemfd131
To set the variable to EST
TZ_adjust=-5;
sudo echo "T$(($(date +%s)+60*60*$TZ_adjust))" >/dev/tty.usbmodemfa131