2012-09-24 12 views
5

से एक पीएलसी के लिए एक बूलियन मान भेजा जा रहा है मैं एक पीएलसी के साथ एक संबंध से डेटा को पढ़ने के लिए बनाने के लिए सक्षम था। अब एक समस्या है और यह है कि मुझे पीएलसी से डेटा संशोधित करने के लिए एक विधि लिखनी है। इसे प्राप्त करने के लिए, मुझे पीएलसी को दो मान भेजना होगा: एक int मूल्य और एक बुलियन मूल्य। मुझे net.wimpi.modbus पैकेज से कक्षाओं के माध्यम से int मूल्य हल किया गया है। लेकिन जब यह बुलियन मूल्य की बात आती है तो मुझे कोई संकेत नहीं है कि क्या करना है।एंड्रॉयड

अगर किसी एक ही समस्या अब मैं क्या के रूप में, आप कृपया मुझे एक संदर्भ जहां मैं एक समाधान या मेरी समस्या को हल करने के लिए एक बहुत अच्छी ट्यूटोरियल के एक लिंक मिल सकता भेज सकता था? किसी ने this question में लिंक के एक जोड़े तैनात लेकिन यह मेरे ट्यूटोरियल कि पीएलसी के साथ संचार और कैसे पीएलसी के डेटा के इलाज के लिए के साथ क्या करना ज्यादा नहीं है करने के लिए भेजता है।

संपादित

मैं एक मेडिकॉन M340 पीएलसी के संबंध बनाया है, और कनेक्शन के लिए मैं net.wimpi.modbus पैकेज की कक्षाओं का उपयोग करें। मैं वर्गों ModbusTCPTransaction और TCPMasterConnection के माध्यम से अपने कोड में कनेक्शन नहीं बनाया है, और मैं वर्गों ReadMultipleRegistersRequest और ReadMultipleRegistersResponse के माध्यम से मूल्यों पढ़ें।

कोड मैं कनेक्शन के लिए बनाया:

private InetAddress m_Address; 
private ModbusTCPTransaction m_Transaction = null; 
private TCPMasterConnection m_Connection = null; 

int port = Modbus.DEFAULT_PORT; 
private Activity activity; 


public ModbusConnection(Activity activity, String ip) 
{ 
    this.activity = activity; 

    try { 
     m_Address = InetAddress.getByName(ip); 
    } catch (UnknownHostException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } // the slave'saddress 
} 

public void getTransaction(InetAddress inet) throws Exception 
{ 
    /*Variables for the reading of the PLC data*/ 
    int port = Modbus.DEFAULT_PORT; 

    /*Data initialization for the reading of the PLC data*/ 
    m_Connection = new TCPMasterConnection(inet); 
    m_Connection.setPort(port); 
    m_Connection.connect(); 
    m_Transaction = new ModbusTCPTransaction(m_Connection); 
} 

और मूल्यों को पढ़ने के लिए, मैं अगले कोड हर समय कहते हैं। मैं केवल पढ़ने दक्ष और लेखन पूर्णांक, स्ट्रिंग और शब्द है कि मैं से एक पीएलसी को घोषित ऑफसेट पढ़ के माध्यम से मूल्यों फ्लोट:

private ReadMultipleRegistersResponse readValues(int offset, int count) 
{ 
    ReadMultipleRegistersRequest rReq = null; // the request 
    ReadMultipleRegistersResponse rRes = null; // the response 

    try { 

     rReq = new ReadMultipleRegistersRequest(offset, count); 
     m_Transaction.setRequest(rReq); 
     m_Transaction.execute(); 
     rRes = (ReadMultipleRegistersResponse) m_Transaction.getResponse(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
     Log.i("AsyncTask", "doInBackground: Exception"); 
    } 
    return rRes;  
} 

संपादित 2

मुझे लगता है कि मैं पूरा मैं क्या चाहता था।

private ReadCoilsResponse readBytes(int offset, int count) 
{ 
    ReadCoilsRequest rReq = null; // the request 
    ReadCoilsResponse rRes = null; // the response 

    try { 

     rReq = new ReadCoilsRequest(offset, count); 
     m_Transaction.setRequest(rReq); 
     m_Transaction.execute(); 
     rRes = (ReadCoilsResponse) m_Transaction.getResponse(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
     Log.i("AsyncTask", "doInBackground: Exception"); 
    } 
    return rRes;  
} 

    public void writeBytes(int wordNumber, BitVector b) { 
    try {    
     WriteMultipleCoilsRequest wReq = null; // 
     WriteMultipleCoilsResponse wRes = null; // 

     wReq = new WriteMultipleCoilsRequest(211, b); 
     m_Transaction.setRequest(wReq); 
     m_Transaction.execute();  
    } catch (Exception e) { 
     e.printStackTrace(); 
     Log.i("AsyncTask", "doInBackground: Exception"); 
    } 
} 

इसके अलावा:

ReadCoilsRequest ReadCoilsResponse WriteMultipleCoilsRequest WriteMultileCoilsResponse

मैं क्या PLC में पढ़ सकते हैं और कॉयल लिखने के लिए दो विधियों है: वहाँ 4 वर्गों है कि मैं कॉयल पढ़ने के लिए उपयोग कर रहे हैं

public BitVector readBitVector(int offset, int count) 
{ 
    BitVector myBitVector; 

    ReadCoilsResponse rRes = readBytes(offset, count); 
    rRes = (ReadCoilsResponse) m_Transaction.getResponse(); 
    myBitVector = rRes.getCoils(); 

    return myBitVector; 
} 
:, मैं कॉयल वर्गों का उपयोग BitVector चर को पढ़ने के लिए एक विधि बनाया

इस के बाद, मैं क्या मेरी कोड में net.wimpi.modbus.util पैकेज से BitVector वर्ग से एक देशी समारोह उपयोग कर रहा है 1 या 0 करने के लिए थोड़ा और स्थापित करने के लिए प्रयोग किया है:

test.setBit(2, true); 

नोट: यह यह याद रखना महत्वपूर्ण है कि जब भी आप पीएलसी को मूल्यों को पढ़ना या लिखना चाहते हैं, तो ऐसा करने का सबसे अच्छा तरीका पीएलसी से कनेक्शन बंद करना है।

+1

संकेत: आप SO पर अपने प्रश्नों के शीर्षक से "समस्याएं" छोड़ सकते हैं। यदि आप इसे यहां पोस्ट करते हैं, तो यह निहित है कि कुछ समस्याएं हैं जिन्हें हल करने की आवश्यकता है। –

+1

क्या आप हमें बता सकते हैं, आपने पहले ही क्या प्रयास किया है। और पीएलसी क्या है? आपने कनेक्शन कैसे बनाया? यह बूलियन को संभालने के तरीके को प्रभावित कर सकता है। – Fildor

+0

@ फ़िल्डर मैंने अभी अपना प्रश्न संपादित किया है और आपने मुझसे पूछे गए सवालों के जवाब दिए हैं –

उत्तर

2

मेरे निश्चित जवाब है: आप बिट्स के रूप में रजिस्टर के इलाज के लिए किया है।

intValue = m_Data[1].getValue(); 
intValue = intValue | 2; 
m_Data[1].setValue(intValue); 

दूसरी पंक्ति बिट मैं अपने पीएलसी में लिखना चाहते हैं को संशोधित करता है: तो अगर आप एक रजिस्टर अपने कोड में किसी पूर्णांक मूल्य में प्रतिनिधित्व के दूसरे बिट लिखना चाहते हैं, तो आप इस तरह कुछ करना होगा ।