|
楼主 |
发表于 2015-9-24 08:19:31
|
显示全部楼层
dcopyboy 发表于 2015-9-22 11:05
该模块,日常使用是非常稳定的。楼主可以贴出代码看看,我估计是出在 缓冲处理 不当上,我刚开始的时候,返 ...
应该不会是缓冲区的问题吧,发送无数个查询开关状态的命令都没问题有时个只发送一次控制开关的命令就挂掉了。。
PS:这论坛验证码好难输。。。 - #define slaveID 1
- #include <EtherCard.h>
- #include <IPAddress.h>
- static byte mymac[] = {0x74, 0x69, 0x69, 0x2D, 0x30, 0x31};
- static byte myip[] = { 192, 168, 1, 253};
- static byte gwip[] = { 192, 168, 1, 1 };
- static byte dnsip[] = { 192, 168, 1, 1 };
- static byte mask[] = { 255, 255, 255, 0 };
- byte Ethernet::buffer[700];
- bool useDHCP = true;
- static uint8_t io_num[] = {6, 7, 8, 9};
- static bool io_flag[] = {false, false, false, false};
- uint16_t calculateCRC(const char* _regs, uint16_t arraySize)
- {
- unsigned int temp, temp2, flag;
- temp = 0xFFFF;
- for (unsigned char i = 0; i < arraySize; i++)
- {
- temp = temp ^ *(_regs + i);
- for (unsigned char j = 1; j <= 8; j++)
- {
- flag = temp & 0x0001;
- temp >>= 1;
- if (flag)
- temp ^= 0xA001;
- }
- }
- temp2 = temp >> 8;
- temp = (temp << 8) | temp2;
- temp &= 0xFFFF;
- return temp;
- }
- uint8_t responseData(uint8_t ID, uint8_t function, uint16_t address, uint8_t *data, uint8_t len, char *result)
- {
- uint8_t MaxLen = sizeof(char) * (1 + 1 + 1 + len + 2);
- *(result + 0) = ID;
- *(result + 1) = function;
- *(result + 2) = len;
- int n = 3;
- for (int i = 0; i < len; i++)
- {
- *(result + (n++)) = data[i];
- }
- uint16_t crc16 = calculateCRC(result, n);
- *(result + (n++)) = crc16 >> 8;
- *(result + (n++)) = crc16 & 0xff;
- return MaxLen;
- }
- void responseError( uint8_t function, uint8_t wrongNumber, uint16_t sport, const uint8_t *dip, uint16_t dport)
- {
- char frame[5];
- uint16_t crc16;
- frame[0] = slaveID;
- frame[1] = function + 0x80;
- frame[2] = wrongNumber;
- crc16 = calculateCRC(frame, 3);
- frame[3] = crc16 >> 8;
- frame[4] = crc16 & 0xFF;
- ether.sendUdp(frame, 5 , sport, dip, dport );
- }
- void udpSerialPrint(uint16_t port, uint8_t ip[4], uint16_t src_port, const char *data, uint16_t len) {
- IPAddress src(ip[0], ip[1], ip[2], ip[3]);
- if (data[0] == slaveID && len >= 8)
- {
- if (data[1] == 0x03)
- {
- uint16_t address = (data[2] << 8) + (data[3] & 0xff);
- uint16_t datalen = (data[4] << 8) + (data[5] & 0xff);
- if (address + datalen <= sizeof(io_flag))
- {
- uint16_t crc16_1 = calculateCRC(data, len - 2);
- uint16_t crc16_2 = (data[6] << 8) + (data[7] & 0xff);
- if (crc16_1 == crc16_2)
- {
- uint8_t respdata[datalen];
- for (int i = 0 ; i < datalen; i++)
- {
- respdata[i] = io_flag[address + i];
- }
- char a[] = {};
- uint8_t l = (responseData(slaveID, data[1], address, respdata, datalen, a));
- ether.sendUdp(a, l , port, ip, src_port );
- }
- else
- {
- responseError(0x00, 0x03, port, ip, src_port);
- Serial.println("Crc16 Err");
- }
- }
- else
- {
- responseError(0x00, 0x03, port, ip, src_port);
- Serial.println("Address not is Range");
- }
- }
- else if (data[1] == 0x06)
- {
- uint16_t address = (data[2] << 8) + (data[3] & 0xff);
- uint16_t datavalue = (data[4] << 8) + (data[5] & 0xff);
- if (address < sizeof(io_flag))
- {
- io_flag[address] = (bool)datavalue;
- if (io_flag[address])
- {
- digitalWrite(io_num[address], HIGH);
- }
- else
- {
- digitalWrite(io_num[address], LOW);
- }
- ether.sendUdp( data, len, port, ip, src_port );
- }
- else
- {
- responseError(0x00, 0x03, port, ip, src_port);
- Serial.println("Address not is Range");
- }
- }
- else
- {
- responseError(0x00, 0x01, port, ip, src_port);
- Serial.println("Function not is 0x03 or 0x06");
- }
- }
- else
- {
- responseError(0x00, 0x01, port, ip, src_port);
- Serial.println("Number not is 0x01");
- }
- }
- void setup () {
- Serial.begin(57600);
- for (int i = 0 ; i < sizeof(io_flag); i++) {
- pinMode(io_num[i], OUTPUT);
- digitalWrite(io_num[i], LOW);
- }
- if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0) {
- Serial.println( "Failed to access Ethernet controller");
- }
- if (useDHCP)
- {
- if (!ether.dhcpSetup())
- {
- Serial.println("Failed to get configuration from DHCP");
- }
- else
- {
- Serial.println("DHCP configuration done");
- }
- }
- else
- {
- if (!ether.staticSetup(myip, gwip, dnsip, mask)) {
- Serial.println("Failed to set IP address");
- }
- }
- ether.printIp("IP Address:\t", ether.myip);
- ether.printIp("Netmask:\t", ether.netmask);
- ether.printIp("Gateway:\t", ether.gwip);
- ether.udpServerListenOnPort(&udpSerialPrint, 5000);
- }
- void loop() {
- ether.packetLoop(ether.packetReceive());
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|