1 2 3 | #include <SoftwareSerial.h> SoftwareSerial dxif(8, 9); // pin8 is RX, pin9 is TX |
1 2 3 4 | void setup() { dxif.begin (57143); dxif.setTimeout (50); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | bool sendpos( uint8_t id, uint16_t pos) { uint8_t buf[9] = { 0xff, 0xff, // header id, // id 5, // inst 3, // size 30, // address ( uint8_t )(pos & 0xff), ( uint8_t )(pos >> 8), 0 }; for ( int i = 2; i < 8; i++) buf[8] += buf[i]; // calc sum buf[8] = ~buf[8]; while (dxif.available ()) dxif.read (); // clear buffer dxif.write (buf, 9); // send inst packet if (dxif.readBytes (buf, 6) == 6) // read stat packet return (buf[2] == id) && (buf[3] == 2) &&(buf[4] == 0); return false ; } void loop() { sendpos (1, 0); // Set Goal Position to 0 delay (1000); // Delay 1 sec sendpos (1, 1023); // Set Goal Position to 1023 delay (1000); // Delay 1 sec } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include <stdio.h> #include <conio.h> #include "dxlib.h" #define OFFSETTIME (50) // タイムアウトオフセット[ms] void main ( void ) { TDeviceID dev; char inputcomport[10], mycomport[20]= "\\\\.\\" ; int bdiv=-1, baud, i, yn; uint16_t val16; uint8_t val8; printf ( "input com name=" ); gets (inputcomport); printf ( "use [%s]\n" ,inputcomport); strcat (mycomport, inputcomport); if ((dev = DX_OpenPort (mycomport, 3000000))) { DX_SetTimeOutOffset (dev, OFFSETTIME); printf ( "Open success\n" ); for (bdiv=-1;bdiv<250;bdiv++) { // ボーレートの変更 if (bdiv >= 0) DX_SetBaudrate (dev, (baud=2000000 / (bdiv + 1))); else DX_SetBaudrate (dev, (baud=3000000)); printf ( "\rbaud=%7d " ,baud); uint32_t num = 253; TDxAlarmStatus stat[255]; // Ping2で検索 if (DX_Ping2 (dev, &num, stat, NULL)) { printf ( "%d device found\n" , num); // 検索で見つかったデバイスを列挙 for (i = 0; i < num; i++) { // アドレス16(Status Return Level)を読み出し if (DX_ReadWordData (dev, stat[i].id, 0, &val16, NULL)) { printf ( "Found ID=%d stat:$%04X modelno:$%04X\n" , stat[i].id, stat[i].Status, val16); } else { // 読み出しに失敗したらおそらくStatus Return Levelが0だろう printf ( "Found ID=%d stat:$%04X modelno:???? \n" , stat[i].id, stat[i].Status); printf ( "Do you want to reset Status Return Level ? (y/n)->" ); yn = getch (); putch( yn); if (yn == 'y' || yn == 'Y' ) { printf ( "\nupdate...." ); DX_WriteByteData (dev, stat[i].id, 16, 2, NULL); Sleep (800); if (DX_ReadByteData (dev, stat[i].id, 16, &val8, NULL)) { if (val8 == 2) printf ( "success\n" ); else printf ( "fail\n" ); } else printf ( "fail\n" ); } printf ( "\n" ); } } } } DX_ClosePort (dev); } else { printf ( "Open error\n" ); } } |